OneCompiler

Check if the number is Prime

395

Problem Description
You are given a number n as an argument. You have to implement the function checkForPrime which checks if it's a prime number and returns true or false accordingly. The value of n will be greater than 0 and 1 is not a prime number.

Hint 1: A number is prime if it only has two factors, 1 and itself.

Hint 2: Use a loop to iterate over all numbers from 2 to the number-1, and check if any of these numbers are a factor (use % operator to check for factors).

Sample Input 1
6

Sample Output 1
false

Explanation
Factors of 6 are 1, 2, 3, 6. Thus this is not a prime number.

Sample Input 2
7

Sample Output 2
true

Explanation
Factors of 7 are 1 and 7. Thus this is a prime number.

/**
 * @param {number} n
 * @return {boolean}
 */


function checkForPrime(n) {
  // You only need to implement this function.
}

if (checkForPrime(11) !== true)
  console.log("Test fails: Expected true for input n = 11");
else
  console.log("Sample test case for input n = 11 passed!");

module.exports = checkForPrime