Explain Math.floor(Math.random()*2)


#1



What value does this code assign to the youHit?
Why do we multiply Math.random()*2 ?


var youHit = Math.floor(Math.random()*2);


#2

Math.random generates a random number between 0 and 1. If you now multiply this number by two, all numbers generated below 0.5 are less then 1 (after being multiplied). everything above 0.5 is now bigger then 1. Then you floor the number (throw away everything after the decimal), which means you have two possible numbers: 0 and 1

Or, true (1) and false (0), so this is used to give the user a 50% to hit