FAQ: Code Challenges: JavaScript Fundamentals - Fix The Broken Code

This community-built FAQ covers the “Fix The Broken Code” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise Fix The Broken Code

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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?

I suppose that something is wrong in test.js file.

hello !!! I wanted to ask a question…Why we are multiplying by 6 and then adding 1 ? why we are not multiplying by 7 straight away?

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 ??

Wouldn’t Math.ceiling work to round the numbers up to 1-6, then you can get rid of the +1? That’s what I tried and it didn’t accept it.

1 Like

The ceiling of 0 is 0, so it will not be a viable option.

x = Math.ceiling(Math.random() * 6)

The solution set to the above is,

{ x | 0 <= x <= 6 }

as in,

{0, 1, 2, 3, 4, 5, 6}


x = Math.floor(Math.random() * 6 + 1)

has a solution set,

{ x | 0 < x <= 6 }

as in,

{1, 2, 3, 4, 5, 6}
1 Like

I was confused and annoyed by this exercise. I couldn’t get anything to log until I did this:

const rollTheDice = () => {
    let die1 = Math.floor(Math.random() * 6 + 1);
    let die2 = Math.floor(Math.random() * 6 + 1);
    return die1 + die2;


The hint was not very helpful, as it recommended logging die1, which generated this error message…


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

1 Like

agreed, there is something wrong with the code in that aspect, but it should technically run because it is still a variable

Could I use Math.round() instead of Math.floor()?

The two are similar, but not the same. Floor never rounds up, but round will. It’s called the 5/4 rule… Up on 5, down on 4.

This can have the effect of extending the intended range. Instead of 10 random numbers, we could get 11, so no, they are not interchangeable.

I have the same codes. But Why every time I run the code, I get the same number? Isn’t it supposed to change since the dies are randomly selected?

Same here.

If I just add:


to the existing code:

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


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


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. :wink:
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.

Thanks again for your help.

1 Like