MDN Assessment wanted for Loops 3 skill test

I can only display all odd numbers from 2 to 500, I am finding it very hard to display even numbers.

I have tried to convert the loop to an array and then use the filter method to display the odd numbers. It works for only odd numbers but I can wrap my head around how to display the even numbers.

Here is the link to the assessment:

Here is my code:

I think you miss understanding here, The test is want you to display â€śprime numberâ€ť not â€śodd numberâ€ť or â€śeven numberâ€ť. You can search what is prime number

1 Like

consider using while loop here, you have already initializer on top let i = 500, and you donâ€™t need Array in this test.
ex:

while (i >= 2) {
// some code...
i--;
}
1 Like

Hi Rinaldo, thanks for your feedback.
Although I still used the for loop because the while loop keeps looping until I forcefully turn my PC off.
I have turned it off like 3 times already.

Iâ€™ve been able to count down all the prime numbers from 2 to 500.
Again, thanks for the clarification.

I would love if you could visit my code pen to see if Iâ€™m still missing anything and if I am, kindly help me out with the solution

https://codepen.io/Goodybag01/pen/wvEjLxE

Is called infinite loop, it happen because the â€śconditionâ€ť of your loop is always true. you must make sure that the initializer is incremented or, depending on the case, in this test is decremented

while (i >= 2) {

i--; // initializer is decremented
i++; // don't do this, i >= 2 always true. LOOP FOREVER.
}

Look at the bottom, warning info

1 Like

Alright, I appreciate you.
Thank you

1 Like

for (let i = 500; i >= 2; i--) {
if (i !== isPrime(i)) {
const numArray = [i];

}
}

function isPrime() return true or false.
So, in your code you said like this if (i !== true/false) it always evaluate to true, i is number.
You can simplify like this

if (isPrime(i)) {
para.textContent += `\${i} `
}

You donâ€™t need array in this test,
but if you want to stick with it.

for (let i = 500; i >= 2; i--) {
// remove if statement because is useless in here
const numArray = [i];

}

But, donâ€™t use that , you filter just 1 item in the array.

1 Like

Thanks Rinaldo!
I wondered why it always returned true, I now see my mistakes.