In the Javascript part 1 course, the last exercise of control flow is show all the prime numbers within the limit.
The script is:
function showPrimes(limit){
for (let number=2; number <= limit; number++{
let isPrime = true;
for (let factor = 2; factor < number; factor++){
if (number % factor === 0){
isPrime = false;
break;
}
}
if (isPrime) console.log(number);
}
}
What I can’t figure out is this part:
if (number % factor === 0)
I’m thinking that factor = 2, and 2 < 3, so factor +1. Iterate it, now all the factors are 2-10.
The next is after number/factor, if there is no remainder, it is not a prime number.
So 3(number)/2(factor), there is a remainder so 3 is a prime number.
The next is what confuse me.
Both number and factor increment by 1, so now 4/3, this also has a remainder, so according to the code, 4 is also a prime number.
What am I thinking wrongly here?