+ 1

Will someone please explain how the return statement interacts with loops in this code?

For IsPrime, how does the code work so that, in the case of a nonprime, the return false statement is not overwritten by the return true statement below it? function isPrime(num) { for ( var i = 2; i < num; i++ ) { if ( num % i === 0 ) { return false; } } return true; } function display(n) { var arr = [,2 + "</br>"]; for ( var i = 3; i < n; i+=2 ) { if ( isPrime(i) ) { arr.push(i+"</br>"); } } document.write(arr); // use arr result on your own } display(10); //this is not my code. I got it from nanobash at //https://stackoverflow.com/questions/21966000/need-to-generate-prime-numbers-in-javascript // and made some tweaks

15th Nov 2019, 3:51 AM
Michael
1 Antwort
+ 4
it stop the function execution, including the loop it reside in
15th Nov 2019, 3:56 AM
Taste
Taste - avatar