ReferenceError: Why is the variable not defined?

Hello, I’m often dealing with reference errors, when working with functions.

The latest one confused me most, because the code is from codecademy. I only added Math.floor and console.log.

Why do I get the ReferenceError: die1 is not defined?

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

The line console.log(die1); is copied from the Hint. Lesson: https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/fix-the-broken-code

1 Like

Surely a function that never runs wouldn’t make anything happen, and there’s no other code

Do you know any other function which presents its result by setting some variable? For example, when you call Math.floor, do you get the result by looking at some variable?

Say the function was called, where is the variable, where does it exist? What makes you say that it would exist where you happen to be looking for it?

Does the hint say where and when it makes sense to print out the value of that variable? What about at the top of the file before anything else has happened?

2 Likes

Thank you for your reply!

console.log(Math.floor) just gives me: [Function]Math.floor in die console.
That’s okay for me, as Math.floor is a built-in-object and not a variable.

I understand the function like I read it from the top to the bottom:
It creates the variable ‘die1’ and stores ‘Math.floor(Math.random()) * 6 + 1;’ inside. The same for die2.
Then it adds die2 to die1 and returns the sum.
So, ‘let die1’ doesn’t create a new variable, it’s just here to get a number for the mathematical operations inside of the function?

The hint just says: Try using console.log() to figure out what each individual die actually is: console.log(die1).
When I call the function, before I log to the console, I get the same error ‘die1 is not defined’.

My biggest problem to find my mistake here is, that, when I click on solution, I get the same code I had with the same error, when i log to the console.

The solution in that task was to add Math.floor, but shouldn’t I be able to print it then?
I hate it, when I know, that I know the answer, but I just don’t see it.

1 Like

You’re initiating die1 and die2 in the function and trying to call it outside of the function, try the console.log inside the function as well and see if that works.

–EDIT–

I forgot to add:

Take a look at this line of code and note two things.

  1. You do not need to Math.floor() in this exercise, in fact doing so will not allow progression
  2. The variable is setting to Math.random(), which sets to any number between 0 and 1. Multiplied by 6 that sets it to any number between 0 and 6. Adding 1 will make it any number between 1 and 7. I hope this helps!

-lolman

2 Likes

console.log inside the function doesn’t print anything and I need Math.floor to get the task checked.

I’m going to start over the function course. There’s something fundamental I don’t see.

Thank you very much!

1 Like

I just did the course again just to see what requirements need to be met and I was able to pass it. Let me know if you’re able to do so as well.

1 Like

Hmmm you’d get the right range but dice rolls should probably be discrete! (That isn’t tested for though, for whatever reason)

Also, there’s a chance for it to reach and exceed 13, right?

Maybe I’m missing something.

1 Like

That’s what I’m hinting at. The exercise wants you to make it from 1 to 6, so Math.random() * 6 + 1 wouldn’t be the right way to do it, it should be 5 + 1.

–EDIT–

My logic is flawed here, it should be 6 + 1 since any number will be reduced to the next lower whole number with the Math.floor() function.

1 Like

The problem to solve is understanding the situation.
Your mistakes so far have been about not taking into account what a function is/does, and what let does, and also not considering what value should be sent to Math.floor.

There’s no guessing here! The programmer is the one making it right.

1 Like

Then you wouldn’t be able to get 12 (and it should be able to roll 12)

My point: floor is needed

1 Like

How so ionatan? die1 and die2 both have values equal to or between 1 and 6. meaning added together they will produce any number equal to or between 2 and 12.

1 Like

It’d never reach 12, it’d be a decimal (floating point) value in the interval [2, 12)

1 Like

Ah yes I would agree with that. However, the program would not let me proceed with the Math.floor() function surrounding the Math.random().

1 Like

No, because flooring something less than 1 results in …

Ha, you copied their code and inherited their bug didn’t you ^ _ ^

1 Like
    let die1 = Math.random() * 5 + 1
    let die2 = Math.random() * 5 + 1
    return die1 + die2
  	console.log(die1);
}

This passed the lesson for me, while the below failed.

    let die1 = Math.floor(Math.random() * 5 + 1)
    let die2 = Math.floor(Math.random() * 5 + 1)
    return die1 + die2
  	console.log(die1);
}
1 Like

They’re both wrong D:
The max single-dice roll of the second one is 5!

1 Like

Yet it passed? I don’t understand.

1 Like

Right but the tests don’t prove correctness, only failure to spot bugs

1 Like

Explain the logic to me. I’m not saying your wrong, I’m saying I don’t understand.

1 Like

I’d like to say those pass, you said they fail. Not totally sure what you’re asking
(they’re wrong, but the test doesn’t require you to be able to roll 12, so because they have the interval of [2, 10], they pass the test)

edit:
oh. one doesn’t floor at all. well. it needs to. but that isn’t tested for.

1 Like