6/7 reassign array value


#1

Hello,
I’m having a tough time understanding when asked to “…Then use the randomNumber to reassign currentCard to a new card from the cards array…” in the following code… why a number doesn’t print out on the console? Maybe a better question would be… how does reassigning the value in the array for variable cards work out in a string being printed? Thank you.

var cards = [‘Diamond’, ‘Spade’, ‘Heart’, ‘Club’];
var currentCard = ‘Heart’;

while (currentCard !== ‘Spade’) {
console.log(currentCard);
var randomNumber = Math.floor(Math.random() * 4);

currentCard = cards[randomNumber]; // I don’t understand why this works out in a string being printed instead of a random number??
}

console.log('The program found a: ’ + currentCard + ‘!!!’);


#2

The random number is the index of the string in the array.


#3

Oh I see now :)!! Thank you so much for clarifying. Cheers!!


#4

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