Although my code below seems valid, I’m trying to use console.log() to figure out what each individual die actually is (as suggested in Hint); however, when I do that, I get “ReferenceError: die1 is not defined at Object.”. Any ideas why?
Sorry, i think I found out by myself. so, if the random number will be floored to 0 - it is not existing on the dice. By adding 1 we make it real. Do i think correct ??
ReferenceError: die1 is not defined
at Object. (/home/ccuser/workspace/js-challenge-fix-broken-code-i/main.js:9:13)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)
at bootstrap_node.js:542:3
const rollTheDice = () => {
// Math.random() gives us a random number from 0 up to, but not including, 1
// We multiplied that by 6 to get a number between 0 and up to, but not including, 6
// But since we actually wanted numbers from 1 to 6, inclusive, we added 1
let die1 = Math.random() * 6 + 1
let die2 = Math.random() * 6 + 1
return die1 + die2
}
console.log(rollTheDice())
I get a random floating point number to loads of decimal places each time I execute.
I then amend the code to add Math.floor to Math.random as we’ve been taught so the code looks like this:
const rollTheDice = () => {
// Math.random() gives us a random number from 0 up to, but not including, 1
// We multiplied that by 6 to get a number between 0 and up to, but not including, 6
// But since we actually wanted numbers from 1 to 6, inclusive, we added 1
let die1 = Math.floor(Math.random()) * 6 + 1
let die2 = Math.floor(Math.random()) * 6 + 1
return die1 + die2
}
console.log(rollTheDice())
The test passes and shows the integer ‘2’ in the console, but every time I execute it returns ‘2’. I’m not getting a random result.
I can’t work out why - my best guess is it is counting die1 and die2 (for a total of 2) for some reason. Anyone able to steer me in the right direction please.
Let’s look at the math. Math.random() is going to return a long foat between 0 and 1. Then Math.floor is going to take that value, and round it down to the next integer which will be 0. Then 0 is multiplied by 6 which results in 0. Then 1 is added, so you end up with 1 + 1. That equals 2 every single time I try it.
How could you alter your code to change that result? You have all of the correct pieces, the operations are just not occurring in the correct order.
Hint: how do we change the order of operations here 6 + 4 * 2 to get 20 instead of 14?
Haha! Yes. Sorted it now - we did this on a previous exercise. The numbers belong inside the parenthesis…
I actually tried putting them inside the Math.random parenthesis when I was trouble shooting but obviously got a syntax error, my brain didn’t take the leap to trying them in the Math.random parenthesis.
Math.Random() function returns a random number in the range of 0 to 1 (inclusive of 0, but not 1), so we multiplying it with *6 because the dice has 6 numbers. Now we need to add 1 because if we wouldnt, it would print numbers from 0 to 5 and we need 1 to 6.