Advertisement
AyanUpadhaya

Guessing Game in Javascript Node Js

Jul 25th, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // prompt - sync is Node js module
  2.  
  3. // Make sure you have Node and NPM installed
  4. // Run npm install prompt-sync in the termina
  5.  
  6. //The code below creates a small number guessing game
  7.  
  8. const input = require('prompt-sync')({sigint:true});
  9.  
  10. //random number from 1-10
  11.  
  12. const numberToGuess = Math.floor(Math.random()*10)+1;
  13.  
  14. //this variable is used to determine if the app
  15. //should continue prompting the user for input
  16.  
  17. let foundCorrectNumber = false;
  18.  
  19. while(!foundCorrectNumber){
  20.     //get user input
  21.     let guess = input('Guess a number from 1 to 10:');
  22.  
  23.     //conver the string input to a number
  24.  
  25.     guess = Number(guess)
  26.  
  27.     //compare the guess to the secret answer and let the user know
  28.  
  29.     if (guess==numberToGuess){
  30.         console.log('Congrats you got it!');
  31.         foundCorrectNumber=true;
  32.     }
  33.     if(guess>numberToGuess){
  34.         console.log('Too high');
  35.     }
  36.     if (guess<numberToGuess){
  37.         console.log('Too low');
  38.     }
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement