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.510=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.510=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