Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 90,719 Points

I think I understand Constructor Function prototypes, but...

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

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

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

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

This code only seems to work for the 6th sided dice instance, since it checks the prototype. How would I get it to work for the dice10, instance or is this no longer an option? :)

Thanks

dice10.roll() gives me 1-10 random rolls in the console, so it seems fine to me

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,719 Points

Hmm, maybe i didn't delve into the console deep enough.

My main point was the UI, when you click the button the biggest number you get is still 6. :-)

Thanks for your reply.

1 Answer

Sean T. Unwin
Sean T. Unwin
28,660 Points

Hi there Jonathan Grieve,

If you are going by the Workspace for this lesson, the console.log() statement is only run once upon loading dice.js. This may cause some of the confusion you are having.

To better understand how the roll() function is utilized I moved the console.log() statement to reside within ui.js instead of dice.js. More specifically within the button.onclick handler.

I have provided a Snapshot of my Workspace that you can view. Below is the code that I am describing:

ui.js
/* ... Existing code ... */

button.onclick = function() {

/* Roll each of the dice on button click */
  var d6r = d6.roll();
  var d10r = d10.roll();
  var isDiceRollsEqual = (d6r === d10r);
/* Log the variables' value */
  console.log('d6r: ', d6r);
  console.log('d10r: ', d10r);
  console.log(isDiceRollsEqual);

  var result = d6r;

  printNumber(result);
};

I hope that helps clarify things for you. :)

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 90,719 Points

Thanks for your clarification I think I understand a little better now :)