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

Nouh Ahmed
Nouh Ahmed
7,085 Points

UI.JS

Instead of declaring a variable to store the property of the dice object, i passed the the object as an argument and it work. So could someone tell the difference between these two approaches.

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

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

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

Nouh Ahmed
Nouh Ahmed
7,085 Points

i commented out the variable declaration part and passed the dice.roll() as an argument for the printNumber function.

I think that you've just found another approach to accomplish the same task. There's always multiple ways to get to the same place in development. I'm no expert but I can't see any drawbacks to your approach vice Andrew's. Great job.

1 Answer

The result variable is there to understand the code more clearly, especially for new programmers:

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

Your approach is completely valid as well. You just don't create a variable and get straight to the point :)

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