Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript (2015) Introduction to Methods Adding a Method to an Object

Mars Epaecap
Mars Epaecap
5,110 Points

When I put dice.roll() into the console I get "Uncaught ReferenceError: dice is not defined(…)"

When I put dice.roll() into the console I get "Uncaught ReferenceError: dice is not defined(…)"

I get this with

var dice = {
  roll: function diceRoll() {
      var sides = 6;
      var randomNumber = Math.floor(Math.random() * sides) + 1;
      console.log(randomNumber);
  }
};

and

var dice = {
  roll: function {
      var sides = 6;
      var randomNumber = Math.floor(Math.random() * sides) + 1;
      console.log(randomNumber);
  }
};
ywang04
ywang04
6,762 Points

The second one should be:

var dice = {
  roll: function () {
    var sides = 6;
    var randomNumber = Math.floor(Math.random() * sides) + 1;
    console.log(randomNumber);
  }
}

() is missing after function key word.

5 Answers

Van Jayson Pelayo
Van Jayson Pelayo
5,263 Points

Check if your linking your javascript file correctly to your html.

Christian Hals
Christian Hals
11,811 Points

Are you using Chrome?

I had the same problem with this project and I´m able to reproduce it every time by doing the following:

Loading the page, open the console and typing dice.roll() into the command line. I then get the "Uncaught RefrenceError: dice is not defined(...) It seems the page doesn´t load properly on the first attempt (every time for me in Chrome), in Safari however the problem is not present.

I then hit refresh in the browser, type dice.roll() and voila it works:

dice.roll()
VM1843:1 Uncaught ReferenceError: dice is not defined(…)(anonymous function) @ VM1843:1 
Navigated to http://127.0.0.1:51396/index.html
dice.roll()
dice.js:5 4
undefined
´´´
Sean Walcott
PLUS
Sean Walcott
Courses Plus Student 5,506 Points

I'm having the same problem on chrome and firefox, dice.roll just calls the function and doesn't run it.

Jared Fuller
Jared Fuller
12,511 Points

Sean, I was having that problem, and then I saw he did dice.roll() not just dice.roll and that solved it for me on Chrome.

I was getting the same error. In the console, he says to type "dice.roll()", but the function is "diceRoll()" without the . after dice. It worked for me when I used "diceRoll()".

Idelle Diaz
Idelle Diaz
8,591 Points

I had the same error on Firefox and Safari. I realized that I didn't have the log tab clicked and it was only showing me the errors/warnings in the code and not what was printed to the log.

Thank you Idelle! That was the exact issue I was having...in Chrome. I ahd "Filter -> Errors" on...and didn't have "info" selected.