babycoder
babycoder

Reputation: 180

Getting Boolean values if a number is a prime number in JavaScript

I am trying to solve this problem where I need to print true or false on the console depending on the number (true if a number is Prime and false if it is not). I seem to have the solution but I know there is a mistake somewhere because I get different answers if I change the boolean values of the 2 isPrime variables.

Here is the question: Return true if num is prime, otherwise return false.

Hint: a prime number is only evenly divisible by itself and 1 Hint 2: you can solve this using a for loop.

Note: 0 and 1 are NOT considered prime numbers

Input Example:

1 7 11 15 20

Output Example:

false true true false false

My code:

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

}

isPrime(11);

Upvotes: 1

Views: 3900

Answers (2)

Antoni4
Antoni4

Reputation: 2693

You are very close. I will build on top of your solution. You need to return false as soon as you detect that the number is not prime. This avoids us making additional redundant calculations.

You also did not take into account the first hint.

Hint - Note: 0 and 1 are NOT considered prime numbers

For this you can simply return false if number is 0 or 1.

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

Also in your code you are using a variable isPrime to keep track of boolean values while initialising it with an empty string. This is not correct. Javascript allows this because it's weakly typed, but this is misleading. So in future if you define a variable for boolean value don't initialise it with a string ;)


To address your comment further. If you do want to stick with having a flag in your code, then you can initialise isPrime with true instead of an empty string.

That way you can get rid of the else part in the for loop, making the code shorter. I used code provided by @Commercial Suicide (by the way he did say that there are more elegant solutions) as an example:

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

const numbers = [1, 7, 11, 15, 20];
const booleans = [false, true, true, false, false];

numbers.forEach((item, i) => {
  if (isPrime(item) === booleans[i]) {
    console.log("CORRECT");
  } else {
    console.log("WRONG");
  }
})

You can then go a step further and remove the flag variable.

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

But now it should become even more clear that those flags are actually redundant, as I pointed out initially. Simply return false as soon as you detect that the number is not prime to avoid further unnecessary loop runs. From my experience in javascript over the years I can see that we are heading towards functional programming mindset. It helps to avoid variable mutations generally.

Upvotes: 2

P.S.
P.S.

Reputation: 16384

It's because your isPrime variable can be overriding many times, simple flag (for example) will prevent this issue. I have also added check for 0, 1 and 2. There are more elegant ways to do that, but I wanted to keep your logic, here is an example with some tests:

function isPrime(num) {
  let flag = true;
  let isPrime = '';
  if (num === 0 || num === 1) return false;
  if (num === 2) return true;
  for (let i = 2; i <= Math.sqrt(num); i++) {
    if (flag) {
      if(num % i === 0) {
        isPrime = false;
        flag = false;
      } else {
        isPrime = true;
      }
    }
  }
  return isPrime;
}

const numbers = [1, 7, 11, 15, 20];
const booleans = [false, true, true, false, false];

numbers.forEach((item, i) => {
  if (isPrime(item) === booleans[i]) {
    console.log("CORRECT");
  } else {
    console.log("WRONG");
  }
})

Upvotes: 2

Related Questions