I finished the exercise, but I wanted to append the last item on the list differently, as with the list of cat names example in the “Looping code” article. The easy way to do it was to nest another if(i === 2) condition in there, since 2 is the last prime number on the list, but that seems cheap–I’d like to figure out how to word the condition so that it would theoretically do the same for the last item no matter what it is. How to reword line 46?
Here’s the codepen for my Loops 3 exercise.
Here’s the overall task:
You need to use a loop to go through the numbers 2 to 500 backwards (1 is not counted as a prime number), and run the provided
isPrime()
function on them. For each number that isn’t a prime number, continue on to the next loop iteration. For each one that is a prime number, add it to the paragraph’stextContent
along with some kind of separator.