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

Adebayo Ojo
Adebayo Ojo
23,661 Points

Methods with Prototypes

Please I need a little explanation on this exercise, what is the relationship between the UI.JS and DICE.JS codes below? I need to know how the two codes reference each other.

img1

img2

img3

Adebayo Ojo
Adebayo Ojo
23,661 Points

I tried to display the code screenshot here for easy check, but don't know how to do that, but those are the links to the screenshots.

2 Answers

Hi Adebayo,

I've commented the code below. Let me know if that answers your question.

// constructor for the Dice object
function Dice(sides) {
    this.sides = sides
}

Dice.prototype.roll = function() {
    // returns a random number between 1 and the value of this.sides
    return Math.floor(Math.random() + this.sides) + 1
}

// Creates a new Dice object based on the Dice constructor
// This dice object has 6 sides ... because 6 was passed in as 'sides'
var dice = new Dice(6)

// Just a simple function to set the innerHTML property of a 'p' element, with the id 'placholder', to a given number
function printNumber(number) {
    document.getElementById('placholder').innerHTML = number
}

// Attaches a click event handler onto an element with the id of 'button'
// When button is clicked this function is called
document.getElementById('button').onClick = function() {
    // This will return a number between 1 and 6
    var result = dice.roll()
    // This will change the innerHTML property of the 'p' element on the page to be a number between 1 and 6
    printNumber(result)
}
Adebayo Ojo
Adebayo Ojo
23,661 Points

Thanks for all the responses. Now the question is - why can't we put the two codes in a single file just the way Mikis Woodwinter did it above? And how is the 'var result' in ui.js able to use the 'var dice' in the dice.js when both are in separate files? Is it because the index.html file has ui.js before dice.js?

They're two files for pedagogical reasons; they don't have to be — and often won't be — for production code. The code in ui.js can reference code in dice.js because the browser runs them in the same JavaScript runtime; any subsequent script would be able to reference code from both ui.js and dice.js.

Vance Rivera
Vance Rivera
18,322 Points

The Dice.js is creating an object called Dice which is instantiated with the number of sides (6). Since the object is instantiated and created before the your UI.js is loaded you can reference dice and call the roll function. You may also create a new dice object as well with any number of sides. Just create another variable like this

var dice4 = new Dice(4);

var rollNumber = dice4.roll();

Hope this helps cheers!