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) Working With Numbers The Random Challenge

Aaron Coursolle
Aaron Coursolle
18,014 Points

My notated solution to the random number challenge

I added a couple of lines of extra code, so I could see parsing in action. Like other solutions posted here, I haven't watched the answer video, yet.

//Ask for user input and assign into a variable
var givenNumber = prompt("Please type in a whole number between 1 and 100.");
//Optional line: test for parse, numeric string expected
console.log(givenNumber + 1);
//parsing variable and assigning it to the same variable
givenNumber = parseInt(givenNumber);
//Optional line: test for parse, number expected
console.log(givenNumber + 1);
//Raw random number multiplied by "givenNumber" will provide a determined range.
//Math.floor will ensure a whole number
//+1 in formula will ensure that zero is not an answer
var randomNumber = (Math.floor( Math.random () * givenNumber) + 1);
//Next two lines catenates and assigns to the variable "sentence" 
var sentence = "Your number was " + givenNumber; 
sentence += " and the random number was " + randomNumber + ".";
//displays result to user
alert(sentence);