0

I found this code that checks the argument to see if it's a prime number and for the most part I got it until I reached the very end.

const isPrime = num => {
  for(let i = 2; i < num; i++)
    if(num % i === 0) return false;
  return num > 1;
}

what does return num > 1 do?

14
  • 6
    It returns whether num is greater than 1. Commented Aug 17, 2021 at 15:58
  • 1
    It returns the Boolean result of the comparison between num and 1 - true if the number is greater than 1, false otherwise. I’d recommend brushing up on your fundamentals Commented Aug 17, 2021 at 15:59
  • Well is 1 a prime number? Is zero a prime number? math.stackexchange.com/questions/120/… Commented Aug 17, 2021 at 16:00
  • It's an "operator", learn about all of the Javascript operators in the MDN docs Commented Aug 17, 2021 at 16:01
  • 2
    If you understand i < num, what don't you understand about num > 1? Commented Aug 17, 2021 at 16:02

3 Answers 3

1

You would think it could just return true at the end, as that means the for loop did not detect any divisor for num, which is what it means for a number to be prime.

The problem is that it would then also return true for when num is 1. But 1 is not a prime number (by definition), so that would be wrong.

This is the reason why it has return num > 1. This is short for:

if (num > 1) {
    return true;
} else {
    return false;
}

Or:

return num > 1 ? true : false;

But these are really antipatterns, because they translate a boolean expression in ... the same boolean value. You can just return the boolean value that the comparison results in, and that is what return num > 1 does.

Sign up to request clarification or add additional context in comments.

Comments

1

num > 1 returns a Boolean value depending on value for num.

  • true if num is greater than 1
  • false if num is less or equal to 1

let num = 5;

console.log(num > 1); // Logs true

num = 0;

console.log(num > 1); // Logs false

num = 1;

console.log(num > 1); // Logs false

As per the function definition the function is checking the input number is prime or not.

What is a prime number?

A prime number is a number which is divisible only with 1 and that number.

So here in the function the number is consecutively divided by numbers from 2 to that number - 1. If any of the modulus operations return zero, that number is not prime and it returns false.

If nothing return false, then the number should be greater than 1

Please note 1 is not a prime number.

That is why the last comparison is done. To check whether the number is greater than 1. If the number is greater than 1, the number will be prime.

Because if the number is not divisible by any number from 2 to number - 1, that number should be greater than 1 to be prime number.

5 Comments

What if num is equal to 1?
@Barmar false since 1 is not greater than1
I was pointing out that your bullet items don't mention that case.
The question isn't clear, but I suspect the OP is really wondering why this comparison is used in the isPrime() function, not what it means as a JS operation.
@Barmar I have updated my answer definition.
0

This is because 1 is not prime and after the finishing the above loop if compiler reach on this level then number is definitely prime

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.