Hello guys~
I was doing the sum up the prime task and saw a code that is easy to read and to understand.
However, I have a question regarding the for loop part.
Below is the correct one
`function isPrimes(num)
{
for (var i=2; i < num; i++)
{
if (num % i=== 0) {
return false;
} // if
}//forloop
return true ;
}//isPrimes
function sumPrimes(num){
var sum = 0;
for (var i=2 ; i <=num ; i++){
if(isPrimes(i)){
sum += i;
}
}
return sum;
}
sumPrimes(10);
`
When I tried to do write it myself, I put return true
inside the for loop, like this
`function isPrimes(num)
{
for (var i=2; i < num; i++)
{
if (num % i === 0)
{return false;
} // if
else{
return true ;
}
}//forloop
}//isPrimes
`
The second function remains the same but the result ofsumPrimes(10)
became 24 instead of 17.
Can anyone explain the logic for me?? Will be very appreciated. Thanks in advance!