So,
I’m starting to learn how to code and now I have only one problem.
My code is functioning very well, until he says that the numbers 1 and 0 are prime.
I already tried to add them to exception, change the calculation and still printing that is prime.
Here’s the code:
function testPrimeNumber(num)
{
Number(num)
for (var i = 2; i < num; i++)
{
if (num % i == 0)
{
return ("The number "+num+" isn't prime.");
}
else {}
}
return ("The number "+num+" is prime.");
}
console.log("result for 1:", testPrimeNumber(1));
What am I doing wrong here?
>Solution :
You should test for 0 and 1 at the start of the function. At the moment your code is skipping the for loop (because 1 < 2 and 0 < 2) and going straight to saying the number is prime.