Skip to content

Instantly share code, notes, and snippets.

@debonx
Created January 19, 2020 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debonx/e57fb35cc4ff332e3bdcbc254b33c640 to your computer and use it in GitHub Desktop.
Save debonx/e57fb35cc4ff332e3bdcbc254b33c640 to your computer and use it in GitHub Desktop.
Node: User Input / Output game.
let secretValue = Math.floor(1+Math.random()*10).toString();
let numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'];
module.exports = {
testNumber: (input) => {
if (input === 'quit') {
process.stdout.write('Ok. Bye!\n')
process.exit();
}
if (!numbers.includes(input)) {
process.stdout.write('Choose a number from 1 through 10!\nIs the number ... ')
} else if (input === secretValue) {
process.stdout.write('Woah you got it! Are you psychic? See you later!\n')
process.exit();
} else {
process.stdout.write("Nope. Guess again!\nIs the number ... ");
}
}
}
let {testNumber} = require('./node-input-output-game-1.js');
process.stdout.write("I'm thinking of a number from 1 through 10. What do you think it is? \n(Write \"quit\" to give up.)\n\nIs the number ... ");
let playGame = (userInput) => {
let input = userInput.toString().trim();
testNumber(input);
};
process.stdin.on('data', playGame);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment