Guest User

Untitled

a guest
Sep 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. let rl = require("readline-sync");
  2.  
  3. console.log("Basic Number-Guessing Game");
  4. let playAgain = true;
  5.  
  6. function sortAnswer(option, rightAnswer) {
  7. if (option == rightAnswer) {
  8. console.log("You win");
  9. return true;
  10. } else if (option < rightAnswer) {
  11. console.log("Go higher");
  12. return false;
  13. } else {
  14. console.log("Go Lower");
  15. return false;
  16. }
  17. }
  18.  
  19. function game() {
  20. let rightAnswer = Math.floor(Math.random() * 100);
  21. for (var i = 0; i < 10; i++) {
  22. let option = rl.question("Options: Pick a number between 1 & 100");
  23. let gameOver = sortAnswer(option, rightAnswer);
  24. if (gameOver === true) {
  25. break;
  26. }
  27. }
  28. if (i === 10) console.log("You lose");
  29. }
  30.  
  31. while (playAgain) {
  32. game();
  33. let choice = rl.question("Do you want to play again? (y/n)");
  34. if (choice === "n") playAgain = false;
  35. }
  36.  
  37. console.log("Thanks for playing!");
Add Comment
Please, Sign In to add comment