Why does spade still print? !== 'spade'

Hi there,

I just want to make sure I’m understanding the concept correctly because this code ran differently than expected. The condition for the while loop in this lesson is that currentCard !== ‘spade’. However, the string ‘spade’ still prints. Should the code block not run if the condition is not met? Why is this printing to the console?

Because the log still falls in the while loop.

Your order goes like this

  • evaluate loop (card is not spade)
  • if true change state of card (card may be spade)
  • log card
  • evaluate loop until card is not spade

You would have to change a few things if this is not what you want.

2 Likes

while loops end after it executes the whole block

so your current card (spade) is selected then displayed(console.log(spade)) and then the while loop checks the state of currentCard (currentCard !== spade ) and then the while loop stops

to avoid logging ‘spade’ add a conditional in the while block to not log ‘spade’ when currentCard == ‘spade’