JavaScript Treehouse Club - MASH MASH - JavaScript MASH Choice and Random Number Functions

Anas Rida
Anas Rida
8,183 Points

why multiply by a decimal??

why did Joy first call the numbers and then multiplied then by a decimal and then rounded them down?? why not just call the numbers in the array as they are without then multiplying them?? Many thanks

1 Answer

Steven Parker
Steven Parker
171,252 Points

The idea is to pick a random number. The Math.random function always gives a decimal number between 0 and 1, so if you want a wider choice you multiply it by the number of choices. Then, you round down because you want to use this random choice as an array index, and the index cannot be a decimal.

I'm not sure what you mean by "call the numbers in the array" but I can only assume that there is no function that does it.

Anas Rida
Anas Rida
8,183 Points

thank you very much, I was for some reason assuming that the function was supposed to pick a number from the array randomly. Now I understand that the random number is the decimal.