JavaScript JavaScript Loops, Arrays and Objects Simplify Repetitive Tasks with Loops A Closer Look at Loop Conditions

Nicholas Wallen
Nicholas Wallen
12,278 Points

Why is upper put in the parenthesis at the top of the code?

I understand what the whole code does, just not that specific part:

var upper = 10000; var randomNumber = getRandomNumber(upper); var guess; var attempts = 0;

Explain like I am 5 what using upper as the argument does.

1 Answer

Steven Parker
Steven Parker
174,115 Points

Any time you call a function, you put parentheses after the name. if the function requires one or more arguments, they are put inside those parentheses.

In this case, the "upper" argument given to "getRandomNumber" determines the upper limit on the random number that is returned by the function.

Nicholas Wallen
Nicholas Wallen
12,278 Points

LOL, so I get what you said, but then can you explain this:

var randomNumber = getRandomNumber(10); var guess; var guessCount = 0;

function getRandomNumber( upper ) { var num = Math.floor(Math.random() * upper) + 1; return num; }

Steven Parker
Steven Parker
174,115 Points

I just answered that question separately. Arguments can be passed as variables or as literals.