The isPrime() function returns true if a number is a prime number and it returns false if not. The loop should go through 2 to 500 backward and run it in the isPrime() function. If the number is not a prime number the loop should continue to the next loop iteration. And also, for the number which is a prime number, it should output in the paragraph's textContent, but the continue is not working.
Here is the code:
let i = 500;
const para = document.createElement('p');
function isPrime(num) {
for(let i = 2; i < num; i++) {
if(num % i === 0) {
return false;
}
}
return true;
}
// Add your code here
while( i >= 2 ){
if( isPrime(i) === false){
continue;
} else{
para.textContent = `${i}` + "</br>";
}
i--;
}
And here is the fiddle: https://jsfiddle.net/au9o2ftn/1/
You can simply do this:
let i = 500;
function isPrime(num) {
for (let i = 2; i < num; i++) {
if (num % i === 0) {
return false;
}
}
return true;
}
while (i >= 2) {
if (isPrime(i)) {
console.log(i + " is prime!");
}
i--;
}
without continue...
The problem is that when isPrime(i) in your original code returns true, it continue directly and didn't finish i--. As a result, it becomes a dead loop with infinitely checking isPrime(500).
Or, if you wish to keep continue, put i-- before the continue statement.