DEV Community

Piyush Kumar Das
Piyush Kumar Das

Posted on

Create a Number Guessing Game using Node JS

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.

Resources required

  1. VS code or any IDE
  2. Node JS
  3. The basic knowledge of callback functions and event emitter

Tutorial

  • 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

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️