I found this code online. It is not my own. This is a function to test whether a given number is prime or not. The code works in determining whether a number is prime or not. I just don't understand how it works.
function test_prime(n)
{
if (n===1)
{
return false;
}
else if(n === 2)
{
return true;
}else
{
for(var x = 2; x < n; x++)
{
if(n % x === 0)
{
return false;
}
}
return true;
}
}
alert(test_prime(25));
The first if and the else if statement make sense to me. If n is equal to 1 then return false being that 1 is not a prime number. else if n is equal to 2 then return true because 2 is a prime number.
Everything inside the else statement doesn't make sense to me. If you call the function testing for 25, which is not a prime number, 25%x, x=2, equals 1. Then why would the function return false?
I know there is something about the for loop I'm not understanding.