6/7 code won't run


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/learn-javascript/lessons/loops/exercises/while?action=lesson_resume

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
my code will not run. when I click “run”, it keeps on showing as “loading” (the circle/spinning sign) but never completes loading. there is no error message because the code won’t finish running.

```

var cards = [‘Diamond’, ‘Spade’, ‘Heart’, ‘Club’];
var currentCard = ‘Heart’;

while (currentCard !== ‘Spade’); {
console.log(currentCard);

var randomNumber = Math.floor(Math.random() * 4);

currentCard = cards[randomNumber];
}

<do not remove the three backticks above>

#2

Hi @betasolver80801,

Your code is creating an infinite loop, that is why the circle keeps spinning and never complete.

You have an extra semicolon:

while (currentCard !== 'Spade'); {  // <=== look here

Explanation:

Even though it’s just one extra semicolon, what it does will change your code significantly. Basically, the semicolon acts like a full stop in a sentence, causing your code block (after the while condition), the code in the curly bracket { } not executing at all. Hence, the currentCard is always 'Heart' and the loop will keep looping forever.

Remove that semicolon, that should work. Cheers :slight_smile:


Side note: If you have more questions in future, it’s good to know how to format your code in the post so that it’s easier for others to offer their help. You can refer how to format code here. :slight_smile:


#3

oh that makes so much sense! thank you so much!! (also, thanks for the tips on formatting!)


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.