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

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

I have a slightly different approach but...

My approach is slightly different and after checking it several times I see that parseInt doesn't convert String ... If user's input '11' it yields NaN

In the console however I don't see it happening ... Why parseInt doesn't convert string to integer with Math.random() method ?

I thank you for your answer beforehand.

The purpose of the parseInt function is to parse an integer from a string. You can verify that the parseInt function itself will parse the value of 11 from a string representation of "11" by running this in your console:

var eleven = "11";
eleven = parseInt(eleven);
console.log(eleven);
console.log(typeof eleven);

It sounds like you have something else going on in your code that is causing an error. Please post a link to a snapshot of your Workspace. You can visit this post to see how to get a Workspace snapshot: https://teamtreehouse.com/forum/workspace-snapshots

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Oops sorry didn't post my approach ... And yes I saw that in the console "11" is being converted to integer that's why I started looking for an answer :) Thanks you for your prompt response Here my approach I am not focusing on Math.floor() method just pasreInt challenging me :) I see that it converts floating numbers but can't do it with string

        var promtNumber=prompt("Choose any number please ..."); 
        var resultNumber = parseInt(Math.random() * promtNumber + 1);

        document.write(resultNumber);

Thank you again

P.S. can you please mention how I can make my code colorful when I am posting ...

4 Answers

Ok Dash Dash,

The problem lies within how you are using the parseInt() function. There are a couple ways to go about this in your code. You can rearrange the code so that the parseInt() function only parses the variable "promtNumber" [sic]. You have to parse the integer from a string value before any math is applied so you could do this:

var promptNumber = prompt("Choose any number please ...");
var resultNumber = (Math.random() * parseInt(promptNumber)) + 1;
document.write(resultNumber);

You can also wrap the parseInt() function around the prompt so that it will go ahead and parse the value when the variable is created and math can continue as normal like so:

var promptNumber = parseInt(prompt("Choose any number please ..."));
var resultNumber = (Math.random() * promptNumber) + 1;
document.write(resultNumber);

Keep in mind that any time you need to use a number stored in a string, you must parse it before applying any mathematics! :)

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Thank you again :)

I still couldn't make my code colorful ... would you mind to give me a detailed answer , please ?

Ah, when you wrap your code, put the language you wish to use right beside the first 3 backticks in the code block like so:

```javascript
//YOUR CODE HERE
` ` `  //Put these backticks together

Also, ensure that you are putting 2 full carriage returns before and after your block of code i.e.

This is some text
//THIS LINE SHOULD BE BLANK
```javascript
//YOUR CODE HERE
` ` ` //Put these backticks together
//THIS LINE SHOULD BE BLANK
Some more text here.
Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

Cool :) fixed now ... thank you again see you around

Thank you, Dash Dash! :) If you want to go ahead and mark my answer as best answer, we'll go ahead and resolve this question. Although I am going to clean up my 2nd comment a bit so that it looks nicer and you can see what's going on.

Durdona Abdusamikovna
Durdona Abdusamikovna
1,553 Points

My name is Durdona I was curious to put Dash when I was registering and see if 'form input' checks non letter symbols as I see it doesn't :)

Ah, interesting! :) Well, nice to meet you, Durdona, and if you have any more questions, come on back! :)