Number Guessing Game using NodeJS

Introduction

A number guessing game is a game such that in which a random number is generated, but you don't know the number. Then you have to make successive guesses to find out the number. Each time you guess the number, the generator tells you how close you are to the number by telling to guess higher or lesser.

Tools required

  1. VS code or any IDE

  2. Node JS

  3. The basic knowledge of callback functions and event emitter

Steps

  • Start by creating a JS file in VS code.

  • Then import the readline module of Node JS.

Image code

  • Create an instance of Interface class of readline module

Image code

  • Now generate a random number using Math.floor and Math.random

Image code

  • Now apply this logic to take input, check and if required then prompt for another input

Image code

-Finally, use the event emitter module to listen the close event and execute a callback function.

Image code