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}

whereas,

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;
}

console.log(rollTheDice());

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

/home/ccuser/workspace/js-challenge-fix-broken-code-i/main.js:9
console.log(die1);
^

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?