This is an intriguing question with an interesting function for primes.
Alter the code slightly and you can see the value of n%i each time through the loop and learn how it works.
All you need to do is add one line in the for loop:
console.log("n = " + n + " : " + n%i);
Actually, this line is better (more clearly shows output)
console.log(n + "%"+i + " = " + n%i);
It'll give the following for isPrime(7);
7%3 = 1
7%4 = 3
7%5 = 2
I updated the jsbin too.
Now, when you run it you will see the value of n%i each time through the loop.
let isPrime = function(n){
if(n === 2){
return true;
}
if((n < 2) || !(n%2) || !Number.isInteger(n)){
return false;
}
for(let i = 3; i < n - 1; i++){
console.log(n + "%"+i + " = " + n%i);
if(n%i === 0){
return false;
}
}
return true;
}
Here's a link to a jsbin I created :
JS Bin - Collaborative JavaScript Debugging[
^]
You can go there and see isPrime(7) run when you click the Run with JS button.
You need to open the browser's console window (F12 in most browsers) to see the console.log output.