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>
  <meta charset="utf-8">
  <title>JS Bin</title>


CoffeeScript Code :

test_prime = (n) ->
  if n == 1
  else if n == 2
    x = 2
    while x < n
      if n % x == 0
        return false
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.