6. While loops, problem on #4


#1

So this code runs correctly and returns the final code block of ‘found a spade’, but it is giving me an error and asking if I reassigned currentCard to be a new card from the cards array. I am not sure why this is, I think I understand the code, currentCard is assigned to Spade and it loops through all the random cards that aren’t Spade. Why would I have to reassign it to a different value?

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

let currentCard = 'Spade';

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

currentCard =
  cards[Math.floor(Math.random() * 4)] 
{
    
    console.log("found a spade");
  }




#2

The loop code block should include all the code, not just logging.

while (currentCard != 'Spade') {
  console.log(currentCard);
  currentCard = cards[Math.floor(Math.random() * 4)] 
}
console.log('found a spade');

Aside

We never write a line break after an assignment operator.

left_side = right_side

#3

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