Blog

Prime Number Checker in JavaScript
Posted on June 19, 2019 in Algorithms, JavaScript by Matt Jennings

In JavaScript, Write a Function that ReturnsĀ true or false if a Number is Prime

function checkPrime(num) {
  var primeCheckerArr = [];
  
  for(var i = 2; i <= num; i++) {
    if(Number.isInteger(num / i)) {
      primeCheckerArr.push(i);
    }
  }

  
  if(primeCheckerArr.length > 1) {
    return false;
  }
  else {
    return true;
  }
}
 
console.log(checkPrime(9));
// false
 
console.log(checkPrime(11));
// true

 

Leave a Reply