w3resource logo


CoffeeScript exercises

CoffeeScript function: Check a number is prime or not

CoffeeScript Function : Exercise-15 with Solution

Write a CoffeeScript function that accepts a number as a parameter and check the number is prime or not.

Note : A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself.

Solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>

CoffeeScript Code :

test_prime = (n) ->
  if n == 1
    false
  else if n == 2
    true
  else
    x = 2
    while x < n
      if n % x == 0
        return false
      x++
    true
alert test_prime(37)

Live Demo :

JS Bin on jsbin.com

Post your code through Disqus :



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number