Assessment wanted for - Loops 3 skill test

Hello, could someone please assess this?

Link to my code: https://jsfiddle.net/Keyboardguy/t1b68dgr/

Link to task: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Building_blocks/Test_your_skills:_Loops#loops_3

Hi @Keyboardguy

Well done! That works correctly.

You could make the loop simpler by using if (isPrime(i)) { and putting para.textContent ... inside of it. Then you could also put i-- after the if statement because it has to be call every loop.

Please tell me if this explanation isn’t clear enough. :smile:

Additional question: How could you get rid of the last comma after “2”?

Have a nice day,
Michael

1 Like

Hello,

Sorry for the late reply. The explanation was perfectly clear, thank you! As for the additional question - I came up with this after the while loop:

 para.textContent = `${para.textContent.slice(0,para.textContent.length-2)}`

It’s a bit long, but it works decently.

Thanks, Keyboardguy

1 Like

No reason to be sorry. :slightly_smiling_face:

Yes, that would be fine. Well done :+1:
Another approach (if we know the last elements like in this case) would be to only add ", " if it’s not “2”:

para.textContent += i;
if (i !== 2) {
  para.textContent += ', ';
}