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 Random Number Challenge, Part II Solution

Enemuo Felix
Enemuo Felix
1,895 Points

Having problem with Variable to use

I'm having an issue here. Although I stored the getRandom value on the calcRandom variable, My code won't still run when I call calcRandom variable unless I call the getRandom function just like I did below. Why is it so ?

Charles Wanjohi
Charles Wanjohi
9,235 Points

Please add your code for better understanding

2 Answers

**Now that you've posted your code, I'm going to update this to more closely tailor to your code:

It looks like you're asking why a number isn't showing up when you create the calcRandom variable on this line:

var calcRandom = getRandomNumber(firstN,secondN);

The code does run - however, this line only creates the variable and sets its value. It will only show up somewhere if you do something with it afterwards, like

var calcRandom = getRandomNumber(firstN,secondN); //sets the variable's value
console.log(calcRandom); //prints the variable to the console

Your getRandom() calls below that are showing numbers in the console because they begin with console.log(), which is telling the browser to log it to the console. You just need to do the same thing with calcRandom to get it to work the same way.

**In answer to the second code update: When you use calcRandom, you are just using the value of the variable - the variable does not take parameters. If you want to log the value of the calcRandom variable, it would just be

console.log(calcRandom);

If you want a new number for calcRandom, you would have to call getRandomNumber again - calcRandom is a variable, not a function, so it only changes value when you tell it to. For example, you could do something like:

var calcRandom = getRandomNumber(firstN,secondN); //sets calcRandom to a random number defined by user input
console.log(calcRandom);  //logs calcRandom's value
calcRandom = getRandomNumber(10,15);  //updates calcRandom to be a random number between 10 and 15
console.log(calcRandom); //logs new calcRandom value

If you want to log a different number every time, though, it's fewer lines of code to just call getRandomNumber, as you did originally. Does that make sense?

Enemuo Felix
Enemuo Felix
1,895 Points

I updated the code like this below and ran it in the console but I'm getting "syntax error" etc

function getRandomNumber( lower, upper ) { 
if (isNaN(lower) || isNaN(upper)) {
throw new Error ("Arguement is not a Number!");
} else {
return Math.floor(Math.random()* (upper-lower+1)+lower);
}
}
var firstN = parseInt(prompt("Enter your first lower Number"));
var secondN = parseInt(prompt("Enter your Second Upper Number"));
var calcRandom = getRandomNumber(firstN,secondN);
console.log(calcRandom( 10, 15 ));
console.log(calcRandom( 50, 100 ));
console.log(calcRandom( 46, 89 ));

Updated the answer again - let me know if that makes sense.

Enemuo Felix
Enemuo Felix
1,895 Points

Thank you Katie. That was more than helpful

Great! Glad to help

Enemuo Felix
Enemuo Felix
1,895 Points

I'm sorry I thought I added the code. Here it is

function getRandomNumber( lower, upper ) { 
if (isNaN(lower) || isNaN(upper)) {
throw new Error ("Arguement is not a Number!");
} else {
return Math.floor(Math.random()* (upper-lower+1)+lower);
}
}
var firstN = parseInt(prompt("Enter your first lower Number"));
var secondN = parseInt(prompt("Enter your Second Upper Number"));
var calcRandom = getRandomNumber(firstN,secondN);
console.log(getRandomNumber( 10, 15 ));
console.log(getRandomNumber( 50, 100 ));
console.log(getRandomNumber( 46, 89 ));

Hello there - I've updated my answer now that you've posted the code. Let me know if that answers your question, or if it's still confusing.