Skip to content

Instantly share code, notes, and snippets.

@peterjacobson
Last active December 8, 2022 20:29
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peterjacobson/305184d71fa347ea5b95 to your computer and use it in GitHub Desktop.
Save peterjacobson/305184d71fa347ea5b95 to your computer and use it in GitHub Desktop.
A simple number guessing game in node.js
//var process = require('process');
var readline = require('readline');
var randomNumber = Math.round(Math.random() * 10);
var lives = 5;
var terminal = readline.createInterface(
{
input : process.stdin,
output : process.stdout
});
terminal.setPrompt('Guess the number! (0-10): ');
terminal.prompt();
terminal.on('line', function(answer)
{
var answerNum = parseInt(answer);
if (answerNum > randomNumber)
{
console.log('Too high!');
console.log('You have '+lives+' lives left');
}
else if (answerNum < randomNumber)
{
console.log('Too low!');
console.log('You have '+lives+' lives left');
}
else if (answerNum === randomNumber)
{
console.log('W I N N E R ! ! !');
console.log('You lost only '+ (6-lives) + ' lives');
process.exit(0);
}
else
{
console.log("That wasn't a number I recognise");
console.log('You have '+lives+' lives');
}
lives--;
if (lives == 0)
{
console.log('G A M E O V E R ! ! !');
process.exit(0);
}
terminal.prompt();
});
terminal.on('close', function()
{
console.log('C H I C K E N :P')
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment