Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let rl = require("readline-sync");
- console.log("Basic Number-Guessing Game");
- let playAgain = true;
- function sortAnswer(option, rightAnswer) {
- if (option == rightAnswer) {
- console.log("You win");
- return true;
- } else if (option < rightAnswer) {
- console.log("Go higher");
- return false;
- } else {
- console.log("Go Lower");
- return false;
- }
- }
- function game() {
- let rightAnswer = Math.floor(Math.random() * 100);
- for (var i = 0; i < 10; i++) {
- let option = rl.question("Options: Pick a number between 1 & 100");
- let gameOver = sortAnswer(option, rightAnswer);
- if (gameOver === true) {
- break;
- }
- }
- if (i === 10) console.log("You lose");
- }
- while (playAgain) {
- game();
- let choice = rl.question("Do you want to play again? (y/n)");
- if (choice === "n") playAgain = false;
- }
- console.log("Thanks for playing!");
Add Comment
Please, Sign In to add comment