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
Your code:
// Add your code here
for (let i = 500; i >= 2; i--) {
if (i !== isPrime(i)) {
const numArray = [i];
const primeNumber = numArray.filter(isPrime);
para.textContent += `${primeNumber} `;
}
}
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];
const primeNumber = numArray.filter(isPrime);
para.textContent += `${primeNumber} `;
}
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.
I genuinely appreciate your effort.
1 Like
I usually think too much for little solutions.
That’s my problem!
I tend to write so much code for simple tasks.
I was scared initially about going into Javascript, but I’m beginning to get comfortable as the day passes, albeit I am still learning which is obvious.
I would appreciate it if you could mentor me personally.
Is it okay?