← Back to challenges

Check if a Number is Prime

JavaScriptHardmathvalidation

Instructions

Create a function that returns true if a number is prime, and false otherwise. A prime number is any positive integer that is evenly divisible by only two divisors: 1 and itself.

The first ten prime numbers are:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29

Examples

isPrime(31) ➞ true

isPrime(18) ➞ false

isPrime(11) ➞ true

Notes

  • A prime number has no other factors except 1 and itself.
  • If a number is odd it is not divisible by an even number.
  • 1 is not considered a prime number.
javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Abbreviating a Sentence