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) Introduction to Methods Returning Values

Luke Dawes
Luke Dawes
9,739 Points

Identical code, different results?

As far as I can tell, I've reproduced Andrew's code exactly, but when I click on the HTML/CSS button upon previewing the page, the number doesn't change from 1.

The dice.js file:

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

And the ui.js file:

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);
};

A fresh pair of eyes to look here and tell me what I'm doing wrong would be great! I'd like the output of my code to mimic how the number changes at the end of the video.

3 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

Have you saved your progress and tried relaunching it? The behavior that only display 1 is coming from default workstation setup for the project. Your code works as expected there should be no place in your code that will always generate 1.

I tried myself and then I tried it using your code and both produce same result.

If problem keeps happening upload your workstation environment.

Luke Dawes
Luke Dawes
9,739 Points

I had tried that before but I just relaunched the Workspace and it finally works as it should!

Thanks for your help. :)

Anthony c
Anthony c
20,907 Points

This was a little buggy for me too. But did end up working.