A Console Game

Let's try to make a simple game.

We'll cover the following

Coding challenge

We want to create a random number guessing game. The way it works is that the game generates a random number from 1 to 10, and the player must try to guess it in 3 tries or less.

Here’s what you will have to do:

  • Generate a random number (don’t worry, we will do this for you).
  • Take the player’s input from the console (don’t worry, we will do this for you as well).
  • Compare the player’s guess with the generated number and check if:
    • The number of remaining tries is more than 0. If so, the player can continue guessing if they are wrong.
    • The player has won.
    • The player has lost.
  • If the player has tries left, output TOO HIGH or TOO LOW to the console depending on their guess.
  • If the player has won, output WINNER to the console.
  • If the player has lost, output YOU LOSE! THE NUMBER WAS: X to the console, where X is the randomNumber.
Press + to interact
// tries will be passed to your function
// you need to check if they are more than 1
// the randomNumber is what the player needs to guess
// we will generate this for you
// the guess is what the player has guessed
// we will pass it to the game function
function game(tries, randomNumber, guess) {
// write your code here
}

The complete game

We hope that challenge was fun. For a job well done, here is the complete game. Feel free to play around with the game and the code!

const readline = require("readline");

let rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

function getRandomIntInclusive(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  //The maximum and minimum is inclusive
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// We want the player to have 3 tries
let tries = 3;

// We want to player to guess numbers from 1 to 10
let randomNumber = getRandomIntInclusive(1, 10);

rl.setPrompt("Guess the number! (1-10): ");
rl.prompt();
rl.on("line", function (answer) {
  tries--;
  game(tries, randomNumber, answer);
  rl.prompt();
});

function game(tries, randomNumber, guess) {
  if (tries > 0) {
    if (guess == randomNumber) {
      console.log("WINNER");
      process.exit();
    } else if (guess < randomNumber) {
      console.log("TOO LOW");
    } else if (guess > randomNumber) {
      console.log("TOO HIGH");
    } else {
      console.log("NOT A NUMBER");
    }
  } else {
    if (guess == randomNumber) {
      console.log("WINNER");
    } else {
      console.log("YOU LOSE! THE NUMBER WAS:", randomNumber);
    }
    process.exit();
  }
}
Hit the RUN button to view the output

You can run the app again with the command node app.js

  • The getRandomIntInclusive is written in easy-to-understand JavaScript.
  • The setPrompt method on line 20 allows us to set our own prompt. The default prompt is >; however, we can change it to suit our needs.
  • The prompt method on line 21 does two things. Firstly, it writes our prompt to the output stream. Secondly, it unpauses the input stream, if it has been paused, making sure that we are able to read new data from it.
  • Using the on method, we assign a listener to the line event. The line event is fired when the input stream receives an end-of-line character. This is usually when the Enter or Return key is pressed. This is done on line 22.
  • process.exit() on line 33 and line 44 exit the program once the player has won or lost. We will discuss this in detail in a later lesson.

Get hands-on with 1300+ tech skills courses.