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 Solution

Valerie Smith
PLUS
Valerie Smith
Courses Plus Student 5,244 Points

Please take a look at the way I completed this challenge. Notes would be appreciated about why my method may not work.

"alert("Hey there! I am looking for a random number. But I need you to give me the parameters. You will be asked to fill two numbers, and I will choose a number within that parameter. Thank you! "); var questions var promptOne = prompt("What is the largest number you feel like typing? "); parseInt(promptOne); var promptTwo = prompt ("What is the smallest whole number you feel like typing?"); parseInt(promptTwo); var randomNum = Math.floor(Math.random() * (promptOne - promptTwo) + promptTwo); alert('Your random number of the day is ' + randomNum);"

If that makes no sense follow this link V. https://teamtreehouse.com/workspaces/22654712#

2 Answers

Nando Delgado
Nando Delgado
7,157 Points

Hi Valerie!

I think the problem you're having is that you're parsing the input for each number but not assigning it to a variable, so when you try to run your randomNum function your numbers are still strings. Try something like

alert("Hey there! I am looking for a random number. But I need you to give me the parameters. You will be asked to fill two numbers, and I will choose a number within that parameter. Thank you! "); 
var promptOne = parseInt(prompt("What is the largest number you feel like typing? ")); 
var promptTwo = parseInt(prompt ("What is the smallest whole number you feel like typing?")); 
var randomNum = Math.floor(Math.random() * (promptOne - promptTwo) + promptTwo); 
alert('Your random number of the day is ' + randomNum);

Hope that helps.

Valerie Smith
Valerie Smith
Courses Plus Student 5,244 Points

Nado! Thank you! I ended up fixing it with a simple spelling mistake. I wrote parce like the example used, however the actual function is parse... pretty simple and annoying at the same time! But your example disregards the variable in the parse function, is that a standard method of coding?

Again thank you!

Nando Delgado
Nando Delgado
7,157 Points

I guess you could add another two variables to hold the value of the answers as a string in the prompt and as a number once they're parsed. But considering your function only really needs number you could just parse the answers right at the prompt like I did and keep your code a bit simpler.

nico dev
nico dev
20,364 Points

I did almost exactly as you Nando, and completely agreed (at least basing on the knowledge I have so far, although I may learn anything later that change it, and that would be OK), in that depending on what you need, you may choose to create variables with the strings provided, but that's not needed in this case.

Just glad to see I'm not alone on this :) :)

Nando Delgado
Nando Delgado
7,157 Points

Glad that I was of some help :)