As a javascript beginner, I have created a simple "guess the number game" in javascript.
How does it work?
In this game, It will choose a random number between 1 and 100, and we have to guess the number. We also have chances to guess the number. After each turn, we would be told if the number is right or wrong — and if they are wrong, whether the guess was too low or too high. The game will end once the player guesses correctly.
If the number is correct then it shows "Congratulations!!! Your guessed number is right" and if the number is higher than the actual number, then it shows "Upss!!! Your guessed number is higher" or it would show "Upss!!! Your guessed number is lower "when the number is lower. If you missed all chances then it would show "Sorry!!! You did not guess the correct number".
Source Code
const a = Math.floor(Math.random() * (100 - 1)) + 1;
let chances = 24;
while(chances > 0){
let j = prompt("Enter an number ");
if(j == a){
console. log(`Congratulations!!! Your guessed number is right `);
}
else if(j>a){
console. log(`Upss!!! Your guessed number is higher);
}
else{
console. log(`Upss!!! Your guessed number is lower `);
}
chances--;
}
console.log("Sorry!!! You did not guess the correct number ");
Output
Conclusion
Hope you like this "guess the number" game.
In this post, we learned how to create a number guessing game using simple javascript.
