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

Unable to solve the challenge.

I think got the all pieces together but i am not able to pass the test

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

var dice = new Dice(6);

5 Answers

Jasmine Martin
Jasmine Martin
14,307 Points

Its because of the var result = 1; in the ui.js file that comes up when you launch the workspace. it should be var result = dice.roll(); instead.

Briain Corroon
Briain Corroon
13,689 Points

Your code in the dice.js file is correct. There must be a problem with the html or the ui.js code.

I copied your code into my workspace and it works fine.

The code for the HTML is

<html>
<head>
    <title>Dice Simulator 2015</title>
    <link rel="stylesheet" href="style.css">
</head>  
<body>
  <p id="placeholder">

  </p>
  <button id="button">Roll Dice</button>
  <script src="dice.js"></script>
  <script src="ui.js"></script>
</body>
</html>

The code for ui.js is

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

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

button.onclick = function () {
  var result = dice.roll();
  printNumber(result);
};
Evgeniia Mas
Evgeniia Mas
4,452 Points

Hello! Sorry, but I don't see a link to challenge, just a video, to check my idea. But may be it is just an error as you missed semicolon on line number 6?

Tom Dakan
Tom Dakan
3,967 Points

Jasmine's answer is the correct answer. The workspace is currently wrong and ui.js has a hard-coded value for the result.

Aymen Hachicha
Aymen Hachicha
12,653 Points

Hello,

I think you should remove "new". A function has no instance to my knowledge.

Ana Luiza Barreto Marinho
Ana Luiza Barreto Marinho
2,210 Points

It's a constructor function... watch the video...