 # 6/8 While Loops; The "math.floor" extra Code works how?

So i have no problem with the Code itself, but im interested in how this extra code u just have to copy works.
As far as i get it: Of course u need a random number. But Math.random () with empty brackets will only generate a random number between 0 and 1… so it can also be 0.75?? //Thats right???//
therefore u may multiply it with 4. so may get 0.9 * 4 = 3.6
to round it up as an integer u take the “math.floor”. because then the 3.6 will become 4.
//but since the array got Numbers from 0-3 is the 4 not a failure???
also why cant we just put it “math.random(0, 3)” . Couldnt run this to try it out but should that not be possible too?
and finally i dont get how u are able to get this insde the “card=” statement. Why is it inside the Array brackets?
Thanks for ur help in adcance =).

``````
let cards = ['Diamond', 'Spade', 'Heart', 'Club'];
let currentCard = 'Heart'

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

``````

`floor()` doesn’t round? it floors, which means round down. so floor(3.6) is 3, which is easy to test:

``````console.log(Math.floor(3.6))
``````

possible? yes. Recommend? No

even better would be:

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

now if the array changes, your code still works, making your code less prone to bugs. Hard coding number is best avoided

we can access arrays by index using:

``````arrayName[index]
``````

which is what you do here:

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

accessing array by index should have been covered in the course? You can always revisit exercises you already completed or documentation:

5 Likes

Ur totally right about the arrays, that should have been clear but math.floor. only rounds down, thats new to me (language barriers) and makes sense now to.
Still wonder why math.random(0, 3) didtnt worked but guess its something else than the code.
Ur Idea with cards.length is indeed even better.
Got it know.
Thank you for your help =)

`Math.random(0,3)` wouldn’t work, because it is a function that takes no parameters. It always return pseudo-random number from [0, 1)

Hi Guys,

I am even more confused. I thought the lecture said for the while loops to work the condition must evaluate to ‘true’:

``````while (condition) {
// code block that loops until condition is false
}
``````

This point refers to the above from the exercise: ‘‘Inside the parentheses, we write a condition. As long as the condition evaluates to true, the block of code will loop.’’

Whereas in our code we had to set the following condition to false:

``````let currentCard = 'Club';

``````

I got the entire lecture up until this exercise. Can someone please clarify this for me?

1 Like

`'Club' !== 'Spade'` is true, they do not equal so the condition evaluates to true.

I don’t see the problem?

3 Likes

Thanks for the quick response.

My apologies, I didnt explain the issue properly.

Yes, `'Club' !== 'Spade'` is true because they dont equate but what if i had this `Spade === Spade` isnt this true as well? but the program then does not loop.

`Spade === Spade` is true
`Spade !== Spade` is false

As long as the loop condition evaluates to true, the loop will run.

5 Likes

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