Create variable for random number (from 0 to 100) and set it using Math.random() tip: Math.random() returns number from 0 to 1, so you have to multiply it by 100 and then convert result to integer.
Ask user (with prompt) to guess the number if user clicks "Cancel" - do not ask to enter the number more If entered number doesn’t equal to our variable, alert “Our number is higher, try again” or “Our number is lower, try again”. Then ask user to enter number again.
If user guessed the number, show message “You are right, the number is [number]”.
function guessTheNumber(){
// Math.random() генерує число від 0 до 1
// множення на 100 збільшує діапазон від 0 до 100
// Math.round відкидає знаки після коми
var randomNum = Math.round(Math.random() * 100);
console.log (randomNum); // cheat
while(randomNum !== userNum){
var userNum = prompt('Try to guess the number 0-100');
if (userNum === null){
console.log('GoodBye');
return;
}
if(userNum > randomNum ) {
console.log('Our number is lower, try again ');
}
else if(userNum < randomNum){
console.log('Our number is higher, try again');
}
else{
console.warn('You are right, the number is ' + randomNum);
return;
}
}
}
Launch the script