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

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 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 91,253 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,690 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 91,253 Points

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