| Viewed 114 times

Check if a Number is Prime in javaScript?


Create a function that outputs true if a number is prime, and false otherwise.

1 Answer
Mohammad Qandeel
Answered
13

This function returns true if a number is prime, and false otherwise

function isPrime(num) {
 for (i=2;i<Math.floor(Math.sqrt(num));i++){
  if (num%i==0){return false}
 }
 return true
}

Example

isPrime(5) // true

isPrime(50) // false

 


Related Questions