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

Rich P
Rich P
2,356 Points

Does This Still Work?

I copied the code exactly and this would only return the result of 1. I even launched the completed workspace by the instructor and previewed that code which had the same problem. I opened the URL in a private browser window in case it was a cache issue and it still won't work. Am I the only one experiencing this problem?

Jeffrey Eng
Jeffrey Eng
2,555 Points

I have the same issue too. I wrote the code exactly as the instructor did, then ran it in my browser. I clicked the roll button and it only returns 1.

3 Answers

Prath M
Prath M
9,631 Points

The ui.js file in this Workspace does not reflect the update to the var result for the button.onclick method. Change it to var result = dice.roll(); and the code given by Andrew will work.

Could you post the code that you have?

vijay bhaskar
vijay bhaskar
3,893 Points

This worked for me Dice.js: function Dice(sides){ this.sides = sides; this.roll = function () { var randomNumber = Math.floor(Math.random() * this.sides) + 1;
return randomNumber; } }

ui.js: Call this method on click of button printNumber(dice.roll());