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 Loops, Arrays and Objects Simplify Repetitive Tasks with Loops What are Loops?

Eduardo Vargas
Eduardo Vargas
5,871 Points

Is the variable randNum really necessary?

For the purposes of this exercise and how it is demonstrated by Dave, is it necessary to use the variable randNum or is it generally good practice to put a function into a variable when using loops? I didn't use it and the program seems to run fine with the same results.

This is how I initially did it.

function randomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}

var counter = 0;
while ( counter < 10 ) {
  document.write(randomNumber(6) + " ");
  counter +=1;
} 

TIA.

3 Answers

Eric M
Eric M
11,545 Points

Hi Eduardo,

Good work on refactoring Dave's code and getting a smaller working solution.

In regards to whether or not it is best prace to write, for example:

myFunction(anotherFunction(argument));

or

myVariable = anotherFunction(argument);
myFunction(myVariable);

The answer is: it depends.

It is best practice to write clean, readable code, and use appropriate levels of functional abstraction (keep things DRY).

When coding for yourself, you should be concerned with how well you will able to read something a few weeks or months down the line. When coding as part of a team you should try to be as consistent with the style set out by the team leads.

In this case, you can go either way, and chaning function calls after or inside eachother is quite common when things are simple enough that you only have a couple of arguments. If you have a function call with a dozen arguments inside a triple nested loop you might want to seperate things out more to make them clearer (or even rewrite the way the program works so it doesn't get into such a confusing place to begin with - that's called refactoring).

Here Dave has broken things down with an extra step to make it easier to follow along while learning. You'll find this is often the case in Treehouse material - there's a more elegant solution but it might be harder for some students to understand, so line by line clarity if favoured.

As code is often read more than it's written, favouring clarity is a friendly act toward you future readers - and that's often future you!

Cheers,

Eric

huckleberry
huckleberry
14,636 Points

What an elegant answer :)

Cheers,

Huck - :sunglasses:

My thoughts exactly; it's brilliant.

Eduardo Vargas
Eduardo Vargas
5,871 Points

Got it. Very well explained. Thanks!