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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsI 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
Jonathan Grieve
Treehouse Moderator 91,253 PointsHmm, 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
28,690 PointsHi 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:
/* ... 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
Treehouse Moderator 91,253 PointsThanks for your clarification I think I understand a little better now :)
Dan Oswalt
23,438 PointsDan Oswalt
23,438 Pointsdice10.roll() gives me 1-10 random rolls in the console, so it seems fine to me