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

what am i doing Wrong ???

here is my code : var user = prompt("what is your number?");

generator =user+Math.random+1;

document.write (generator);

so to explain my code and the way i did it , i created a variable named user used command prompt to ask the user to input a number , then i created a variable called generator and added user + Math.random + 1 ; i havent added parseInt () command yet , i wanted to see what results i acquired first and i used document.write to print it on the web page , but i get a number with letters , what a am i doing wrong ?? before i watch the video with the results in it i would like to understand what my errors are and what is the proper method to do the challenge , THANKS in advanced !

2 Answers

Hi Eduardo,

When you use prompt to ask a user for input, the input itself turns into a string. In this case it seems like you are expecting a number.

So before handling user maybe turn it into a number? You can google this by asking for example: "How to turn string into a number in javascript" and the internet (probably MDN which is a good source) will have your answer :)

If you ever run into something where the output is not the same type you expected, you can always check what type you are dealing with by using typeof. It is a neat little trick that helps me out a lot whilst learning. So you could run console.log(typeof user) for example, to see that is it indeed a string.

Good luck!

Doron Geyer
seal-mask
.a{fill-rule:evenodd;}techdegree
Doron Geyer
Full Stack JavaScript Techdegree Student 13,897 Points

Hi Eduardo, So when you use prompt the value it returns is a string. You need to use the parseInt function on the value returned by prompt. for example:

var number1 = prompt("what number would you like to use");
//lets say user inputs 6 as in the previous videos 
//var number1 now has the value of "6" as a string.
// you now use parseInt to convert it
    number1 = parseInt(number1);
// now you have a value of 6 rather than "6"
// to generate the random number range you would then use Math.random.
var randnum;
randnum = Math.random()*number1 +1;
// if you wanted it to be a integer(whole number) and not a floating point(fraction) you would use something like this
radnum = Math.floor(Math.random()*number1 + 1);

good luck

Kevin Gates
Kevin Gates
15,053 Points

Hi Doron Geyer ,

FYI for future reference to improve your code visually.

You added the backticks for your code, which is great. But if you add the language (javascript, csharp, etc.) after the first set of backticks, then it's highly the syntax appropriately and make it easier to read. So, backtick backtick backtick JavaScript // your code backtick backtick backtick.

Your code above will then look like this:

var number1 = prompt("what number would you like to use");
//lets say user inputs 6 as in the previous videos 
//var number1 now has the value of "6" as a string.
// you now use parseInt to convert it
    number1 = parseInt(number1);
// now you have a value of 6 rather than "6"
// to generate the random number range you would then use Math.random.
var randnum;
randnum = Math.random()*number1 +1;
// if you wanted it to be a integer(whole number) and not a floating point(fraction) you would use something like this
radnum = Math.floor(Math.random()*number1 + 1);