Math calculation priority


#1


https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/2?curriculum_id=506324b3a7dffd00020bf661#


Just curious as to the reason and logic in the HINT section...

multiply by 5 [Math.random * 5]
round down [Math.floor]
add 1 [+ 1]

I thought that the priority of the mathematics should follow basic functions - i.e. that those in the brackets ( ) would come before the those outside - which would mean:

multiply by 5 [Math.random * 5]
add 1 [+ 1]
round down [Math.floor]

Thanks in advance for some clarity.


var damageThisRound = Math.floor(Math.random() * 5 + 1);
// variable to keep track of damage inflicted

Replace this line with your code.


#2

this is what happens? the floor happens after the +1


#3

Thanks for the reply.
I was confused because the HINT suggests otherwise:

"First we use Math.random() to create a random number from 0 up to 1. For example, 0.5
Then we multiply by 5 to make the random number from 0 up to 5. For example, 0.5 * 5 = 2.5
Next we use Math.floor() to round down to a whole number. For example, Math.floor( 2.5 ) = 2
Finally we add 1 to change the range from between 0 and 4 to between 1 and 5 (up to and including 5)"


#4

that doesn't make any sense, +1 happens first


#5

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