How to log to console with a while statement


#1
let cards = ['Diamond', 'Spade', 'Heart', 'Club'];

let currentCard = 'Spade';

while (currentCard !== 'Spade') {console.log('not Spade');};

I am logging to console as the instructions say; why am I not succesfully able to perform these instructions:

In the block of the while loop that you wrote in the previous step, log the value of currentCard to the console. Because the while loop only runs if the card is NOT a Spade, the value of currentCard will only be logged to the console if it is not 'Spade'.

Here is the website:
For Loops

Again, a syntax error.


#2

what do you define as a syntax error?

which step do you get stuck at? step 3? it says:

Below the console.log() statement, add this code:

currentCard = cards[Math.floor(Math.random() * 4)];

currently, the loop is not running, because the current card is a spade. Which is a good thing, because if the loop would run, you would have an infinity loop which would freeze the lesson.


#3

I am writing the correct syntax for console.log and the system is not taking it


#4

that is not a syntax error, a syntax error is when you run your code, and the interpreter throws an error.

Can you also to the other parts in my answer? Your currentCard is spade, spade !== spade is false, so the loop won’t run, so the console.log is never reached

be glad the loop doesn’t run, you would have an infinity loop

Sometimes, you build code for later, which is also the case here. Just continue, you will see


#5
let cards = ['Diamond', 'Spade', 'Heart', 'Club'];

let currentCard = 'Heart';

while (currentCard !== 'Spade') {'not a Spade!'};

console.log(currentCard);

cards[Math.floor(Math.random() * 4)];

My new attempt


#6

so the instructions say to add:

cards[Math.floor(Math.random() * 4)];

under your console.log, let me show you:

let cards = ['Diamond', 'Spade', 'Heart', 'Club'];

let currentCard = 'Spade';

while (currentCard !== 'Spade') {
    console.log('not Spade');
    /* add the code HERE */

};

now we can safely change currentCard, given we no longer have an infinity loop, and the loop will run :slight_smile:

changing the currentCard will cause the loop to run, sometimes you do things, to see them come alive later


#7

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