Guess random number HELP!

Hello everyone, I am stuck and have been sitting in front of this computer for hours trying to understand the following program:

var upper = 10000;
var randomNumber = getRandomNumber(upper);
var guess;
var attempts = 0;


function getRandomNumber(upper) {
  return Math.floor( Math.random() * upper ) + 1;
}

while(guess !== randomNumber) {
  guess = getRandomNumber(upper);
  attempts+=1;
}

document.write("<p>The random number was: " + randomNumber + "</p>");
document.write("<p>It took the computer " + attempts + " attempts to get it right.</p>");

Would someone mind breaking it down for me, I just don’t get it and I’m about to GIVE UP!!!

The approach used in this program is recursion. if you have doubt on that I cant explain that here, it is a huge topic. so i can,t explain here. it is a easy way to find solution for the problem. the first time when you learn it, you may feel difficult, later you will get used to it. there is a mit video on recursion on you tube. Try to watch that. video. but i can give you one simple time.
function recursion(){

recursion()
}
recursion()
write this code and observe the output. you can see the out put is that maximum call stack is exceeded.
search recursion in JavaScript on YouTube

or
if you have doubt about Math.floor or math.random
I will try to explain you that

To understand this program , you should know what Math.random() and Math.floor() method is? it is not a big deal to understand this. To learn Math.randome() , do one thing. first, open you google chrome console by pressing ctrl+shift+i . then console.log(Math.random()) many times. so that you can see different value between 0 and 1 being printed out there . Most of the time you get a fractional value like 0.11 or 0.21 . Now Math,floor() is used to round of the number.
var a =Math.random()
console.log(a) and console.log(Math.random()) is equal

if the first time you do console.log(Math.randome()) ,just assume that the value you got is 0.15
but instead of if you do console.log(Math.floor(Math.random)) for the value 0.15 ,
you can only get value 1 . because Math.floor method round off the value.
instead of if you do console.log(Math.floor(Math.random()10)) the answer you are going to get is 15 .
because 1.5
10=15

if the second time you do console.log(Math.randome()) ,just assume that the value you got is 0.25
but instead of if you do console.log(Math.floor(Math.random)) for the value 0.15 ,
you can only get value 1 . because Math.floor method round off the value.
instead of if you do console.log(Math.floor(Math.random()10)) the answer you are going to get is 25 .
because 2.5
10=25

you can multiply with any number instead of 10. in this program they multiply with 1000

now i will rewrite this program

var upper = 10000;

var guess;
var attempts = 0;
var randomValue;

function getRandomNumber(upper){

if(guess !== randomValue) {
guess = randomValue;
attempts+=1;
}

randomValue= Math.floor( Math.random() * upper ) + 1;
return randomValue
}

var randomNumber = getRandomNumber(upper);
console.log(getRandomNumber(upper))

Remember I am also not an expert
all the best
Happy coding

how to understand whether it is recursion or recursive function. i have issue with recursion. hoping a video by explaining everything about recursion. if i try to do recursion , i get error message maximum call stack is exceeded. i am aware why i am getting that error. in free code camp there is a challenge for factorial number i guess, that only i can solve by using recursion. can you please explain that well. also one more thing. did you notice that i have rewrite that code without recursive function. was that code right? Hope you replay. today way my first day on free code camp forum. and 5th month on freecodecamp.com

Hi @Eldhose ,
In your code you are recursively calling a non recursive function getRandomNumber(upper) in the while statement. So the while statement is going to be running util you are going to get the correct value
[ guess == randomNumber ].
And as you are using Math.Random this will be generating different number every time. So its rare that you will get the same number generated by the Math.Random function. So some time this might guess but some time it will take time, so you might be needing a huge ram and processing speed to guess and that is why you are getting that error “stack is exceeded”.
Also while this function is working your web page might get hang because of the running script.

So this is an example of recursion using while loop, and there is no recursive function.

you can think like while is a function that calls itself until the condition becomes false.
So when you have a function like this which stops at certain condition, otherwise keeps on running then its a recursive function.

Let me know if you need more explanation

thanks for replaying. I completely understood . I know how it work. but writing algorithm for recursive solution is difficult. I am week at that point. That is the part making me sad.

.

I was able to solve the problem.

I needed to update guessNum with a random number by putting it inside the while loop and also update the randomNumber variable too. Since my while loop tests for: while guessNum !== randomNumber it needs to update each time through the loop.

// generate random number between 1 and 10
var randomNumber = Math.floor(Math.random() * 10 + 1);

// computer guess variable
var guessNum;

// counter for number of guesses
var count = 0;



// while computer hasn't guessed the random number, increment the counter by one.
while (guessNum !== randomNumber) {
guessNum = Math.floor(Math.random() * 10 + 1);
randomNumber = Math.floor(Math.random() * 10 + 1);
count += 1;



/*

If the computer guesses random number, log random number and number of attempts to the console. Otherwise, log the random number and the number the computer guessed.

*/

if(guessNum === randomNumber) {

console.log("random number is " + randomNumber + " and it took " + count + " number of guesses");

}

else { console.log("random number is " + randomNumber + " and the computer guessed " + guessNum);

}

}