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 Challenge Solution

Kevin Lassar
Kevin Lassar
22,440 Points

My Solution for this challenge...

/dice.js folder/

function dice(sides) { this.sides = 6; this.roll = Math.floor(Math.random() * this.sides) + 1; return this.roll; }

var Dice = new dice();

/*** ui.js folder ***/

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

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

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

2 Answers

Hey there Kevin, not sure if you are looking for feedback here but without making use of the parameter javascript this.sides = sides; your dice can only ever roll within 1-6, even if you run it as javascript dice10 = new dice(10); as it is hard coded to only have javascript this.sides = 6;. Let me know if you have any questions, happy learning!

function dice(sides) {
   this.sides = sides; 
   this.roll = Math.floor(Math.random() * this.sides) + 1; 
   return this.roll; 
}
Kevin Lassar
Kevin Lassar
22,440 Points

Hi Thomas,

You make a great point thank you. I had been playing around with alternative methods to make the challenge work, although i did not realize my solution is now hard coded to max 6. Thank you for the constructive feedback! :)