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) Constructor Functions and Prototypes Methods with Prototypes

Kyle Vassella
Kyle Vassella
9,033 Points

sides is not defined?

For some reason I my code is returning with the error: dice.js:6 Uncaught ReferenceError: sides is not definedDice.roll @ dice.js:6button.onclick @ ui.js:9

My code, at least on the dice.js, is the exact same as Chalkley's. Can anyone spot what I did wrong? Perhaps the issue lies on ui.js on line 9, as suggested above?

dice.js:

function Dice(sides) {
  this.sides = sides;
}

Dice.prototype.roll = function () {
    var randomNumber = Math.floor(Math.random() * sides) + 1;
    return randomNumber;
}

var dice = new Dice(6);
var dice10 = new Dice(10);

console.log(dice.roll === dice10.roll);

ui.js:

function printNumber(number) {
  var placeholder = document.getElementById("placeholder");
  placeholder.innerHTML = number;
}

var button = document.getElementById("button");

button.onclick = function() {
  var result = dice.roll();
  printNumber(result);
};

1 Answer

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I believe this line is incorrect:

var randomNumber = Math.floor(Math.random() * sides) + 1;

Try this:

var randomNumber = Math.floor(Math.random() * this.sides) + 1;  //note the addition of "this"

Let me know if it helps! :sparkles:

Kyle Vassella
Kyle Vassella
9,033 Points

Yup, that was it. Thanks for the second set of eyes, I was pretty frustrated at the time and somehow missed it. My code wasn't 'the exact same' as his after all. Awkward..