Help me find the bug

Tell us what’s happening:
I don’t think I understand this problem quite well, I am not able to complete 3/4 objectives of the challenge.

Your code so far

function randomRange(myMin, myMax) {
 // Only change code below this line
function randomRange(myMin, myMax) {
 return Math.floor(Math.random() * (myMax - myMin + 1) + myMin);
 // Only change code above this line

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Generate Random Whole Numbers within a Range

Link to the challenge:

Hey @Swarajsm,

It seems like you have a function inside a function.

1 Like

You didn’t tell us anything.

Why did you declare your function inside of your function? This is what is confusing the test suite.

yes I realized, can I use while loop to solve this problem, like
while( result<= myMax&& result>=myMin){result=Math.floor(Math.random)}
I think this should print out the random value between myMax and myMin?
am I wrong with this approach?

thanks I realized, But can you check my previous reply and help me out with this?

You should not be using a while loop to solve this challenge.

xD I have updated my question

Here at FCC? but if I am solving such problems elsewhere? is it going to give me my desired output?

You should never use a while loop for random number generation. And I suspect that loop does not do what you think it does. Your condition is not correct for what you are trying to use it for.

Math.floor(Math.random) will always give you NaN.
Math.floor(Math.random()) will always give you 0.

yes I think I get it. We use loops to generate a loop, So right!
(sometimes I ask dumb questions and make fool of myself)

1 Like

You use loops to do the same thing lots of times.

In this case you want to do one thing.

Math.random() will give you a random decimal number from 0 to 1.

You want a random integer from myMin to myMax.

The goal of this challenge is to use the formula provided to make than single random integer.

myRandomInteger = Math.floor(Math.random() * (max - min + 1)) + min

You shouldn’t need a loop. You should only use myMin and myMax with the floor and random functions.


(sigh) I do too. :slightly_smiling_face:

Dumb questions can be the best way to learn. I was a beginner at one point, and I certainly don’t think less of you guys for being beginners now. Beginning is, definitionally, the first step : )


You’re absolutely right! Thank you very much. Now I feel so much better about my dumb questions. :grin:

1 Like

Very True! I hope to someday help beginners around here.


What is the goal of your function. The function that JeremyLT posted fwill give you a random int between myMin and myMax.

I wanted to do the same, I got a bit confused and used while loop (I know I it’s stupid) thinking that it will give me random value only when it satisfies my while condition, but It didn’t work for me.