JavaScript Problem 6 of for loops


#1

Hello, I had a question regarding this problem.

Got the code to work, but dont understand why the random number being generated on an array of 4 items should be written as Math.floor(Math.random * 4). If math.random randomly went to 1, the outcome of the card would be undefined, since 1 * 4 = 4 and there isnt a 4th index in a list of 4 items. Shouldnt it be * 3?

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];
}

console.log(currentCard);

Thanks


#2

Would it?

How often would the outcome be 3? If math,random hits 0.8, what should the outcome be? That is somewhere in the uppermost quarter of the range

Multiplying by 4 means that everything 0.25 or higher is 1, 0.5 or higher 2, 0.75 or higher, 3, 1 or higher 4

(without the overlap I mean)

Assuming that it can hit 1, what are the chances?
And, can it? What's the upper and lower bound on that function, and are those bounds exclusive or inclusive?


#3

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