JavaScript While Loops

Hi, so I came across this exercises in JavaScript While Loops

const cards = ['diamond', 'spade', 'heart', 'club'];

// Write your code below

let currentCard;

while (currentCard != 'spade') {

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

console.log(currentCard);

}

I don’t understand why or even how can this code work?

Can someone please give another example for while loop and its purpose, for what situations it is used.

Thanks a bunch :slight_smile:

p.s link to the exercise: https://www.codecademy.com/courses/introduction-to-javascript/lessons/loops/exercises/while

Hi there,

So, this loop is specifying that it will keep randomly looping through the “cards” array until ‘spade’ is logged.

const cards = ['diamond', 'spade', 'heart', 'club']; //(1)

let currentCard; //(2)

while (currentCard != 'spade') { //(3)
  currentCard = cards[Math.floor(Math.random() * 4)]; //(4)
  console.log(currentCard); //(5)

(1) This is the array it will cycle through
(2) This creates a variable with no parameters
(3) This line is setting the condition (as long as this condition is true, it will keep looping)
(4) This line randomly generates a card in the array and then sets the card generated as currentCard’s value
(5) This line will log the randomly generated card to the console–which will then stop the loop when ‘spade’ is generated.

Does that make sense?

1 Like

Thank you, it makes some what more sense…

a Math.random is suppose to generate a number ‘spade’\s index number within the array is 1 and if math.random generates 1 it is somehow automatically equivalent to spade and is not just a number "1 … I can’t see where the 1 = spades thing happened.

1 Like

Let’s breakdown the code:

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

(1). Math.random is generates a random number between 0 and <1 (it never generates equal to 1)
—> as an example, let’s go with 0.9

(2) The randomly generated number (0.9) is then multiplied by 4 (0.9*4=3.6)

(3) Math.floor now takes that multiplied number (3.6) and rounds it down to the nearest whole number (3)

(4) So, now what’s left is cards[3]

If we remember from the lesson, each element within the array is assigned a number starting from 0.

diamond = 0, spade = 1, heart = 2, club = 3

So, in this case, cards[3] would be the value of currentCard and would log club to the console.

1 Like

Thanks Kira :slight_smile: I sincerely appreciate you replying :stuck_out_tongue: I was even dreaming in Loops last night, and this morning it started to make some sense… your explanation is point on… I remember the Math.Random and Math.Floor exercise, I understand what it is doing. And, also the Array Index [0, 1, 2, …] as well… I was struggling to understand why is a random number that is lets say 3 going to be assigned as ‘spade’ without any instructions.

But I think I got it, it’s because currentCard = cards[Math.floor(Math.random() * 4)]; The Math.floor(Math.random() * 4) is inside cards[ ]

I am sorry, but as a newbie syntax is really difficult to follow, at least for me…

2 Likes