 # While loops #3

var cards = [‘Diamond’, ‘Spade’, ‘Heart’, ‘Club’];
var currentCard = ‘Heart’;
console.log(currentCard);

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

currentCard = cards[randomNumber];
}

``````The Instruction says

Then, create a random number between 0 and 3 and put it in a variable named randomNumber.
Then use the randomNumber to reassign currentCard to a new card from the cards array.

``````

Can someone please explain to me why it is “var randomNumber = Math.floor(Math.random() * 4);” instead of var randomNumber = 3
I don’t understand the parts of Math.floor(Math.random()*4)

this will not generate a random number, now randomNumber will always be 3 so the card chosen will always be Club

`math.random()` will generate a random number between 0 and 1 (0 and 1 itself not included) by multiplying with 4 and then floor (round down) we create a pseudo random number

2 Likes

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