Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. const readline = require('readline')
  2.  
  3. const rl = readline.createInterface({
  4. input: process.stdin,
  5. output: process.stdout
  6. })
  7.  
  8. let randomNumber = Math.round(Math.random() * 10)
  9. let lives = 5
  10. let playerName = null
  11.  
  12. console.log(`
  13.  
  14. Welcome to
  15. G U E S S
  16. T H A T
  17. N U M B E R
  18.  
  19. `)
  20.  
  21. function readyToPlay () {
  22. rl.question("\n Are you ready to play (y/n)?\n", (answer) => {
  23. if (answer == 'y') {
  24. askName()
  25. } else if (answer == 'n') {
  26. console.log("No worries, take your time!")
  27. return readyToPlay()
  28. } else {
  29. console.log("\n Sorry, I didn't catch that, let's try again...")
  30. return readyToPlay()
  31. }
  32. })
  33. }
  34.  
  35. function askName () {
  36. rl.question("\n What is your name?\n", (answer) => {
  37. playerName = answer
  38. console.log(`\n Greetings ${playerName}!`)
  39. livesRemaining()
  40. })
  41. }
  42.  
  43. function makeGuess () {
  44. rl.question("\n Pick a number between 1-10\n", (answer) => {
  45. if (answer == randomNumber) {
  46. winGame()
  47. } else if (lives > 1) {
  48. console.log(`\n Wup wuh. That's not it.`)
  49. lives = lives - 1
  50. livesRemaining()
  51. } else {
  52. endGame()
  53. }
  54. })
  55. }
  56.  
  57. function livesRemaining () {
  58. console.log(`\n You have ${lives} lives remaining.`)
  59. makeGuess()
  60. }
  61.  
  62. function winGame () {
  63. console.log(`\n Yusssss! \n ${playerName} is the champion! \n ${randomNumber} is the correct answer!\n\n\n`)
  64. playAgain()
  65. }
  66.  
  67. function endGame () {
  68. console.log(`\n Oh no ${playerName}, it's
  69. G A M E
  70. O V E R
  71. \n\n\n`)
  72. playAgain()
  73. }
  74.  
  75. function playAgain () {
  76. rl.question('\nDo you want to play again (y/n)?\n', (answer) => {
  77. if (answer == 'y') {
  78. randomNumber = Math.round(Math.random() * 10)
  79. lives = 5
  80. makeGuess()
  81. } else if (answer == 'n') {
  82. rl.close()
  83. } else {
  84. console.log("I didn't catch that, let's try again...")
  85. return playAgain()
  86. }
  87. })
  88. }
  89.  
  90. readyToPlay()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement