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 JavaScript Basics (Retired) Creating Reusable Code with Functions Getting Information From a Function

Dan MacDonagh
Dan MacDonagh
4,615 Points

Returning variable instead of returning calculation

In the beginning part of the video, when he changed the function to return a value, he assigned the Math.floor() calculation into a variable, and then returned that variable. Why couldn't you just return the Math.floor calculation inside the function and call it that way?

I tested it out, and this:

function getRandomNumber() {
  return Math.floor( Math.random() * 6 ) + 1; 
}
alert(getRandomNumber());

acted in the same way as this:

function getRandomNumber() {
  var randomNumber Math.floor( Math.random() * 6 ) + 1; 
  return randomNumber;
}
alert(getRandomNumber());

Is there a difference in the methods I'm not seeing?

1 Answer

Tony Nguyen
Tony Nguyen
24,934 Points

You're thinking more like a programmer now! Yes, those both work and provide the same results and there isn't a difference in terms if functionality as they both do the same thing. I think the instructor was just doing that to provide more readability and understanding.

Dan MacDonagh
Dan MacDonagh
4,615 Points

Thanks for the quick response! Glad I wasn't going crazy :)