Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Build a Node.js Cricket MCQ Quiz Game in Command Line Using readline-sync Module in Javascript

Posted on October 25, 2022

Welcome folks today in this tutorial we will be building a cricket MCQ quiz app inside command line using readline-sync library inside node JS so all the source code of this application will be given below

 

 

Get Started

 

 

So now to get started guys first of all you need to initialise a simple node JS application on the command line so you need to follow the step-by-step instruction which is given below

 

 

npm init -y

 

 

 So as you can see that guys we have created a empty package.json file with the above command now we need to install the dependency which will be needed for this project

 

 

npm i readline-sync

 

 

 so as you can see that there is there is only a single dependency needed here which is called as readline-sync with the help of this dependency you will be able to to collect the user input and you can build your own game in the form of multiple choice questions

 

 

Now you need to make the starting point of a node JS application file which is starting point so simply create that file index.js in the root directory and copy paste the below code

 

 

index.js

 

 

JavaScript
1
2
3
4
5
6
var rs = require("readline-sync");
var score = 0;
var username = rs.question("Please enter your Name:");
 
console.log("Welcome", username + " to the quiz created by Deepank ");
console.log("--------------")

 

 

As you can see that guys in the above code we are loading the library and then we are asking the user their name and the user will enter the name in the command line and after that there we are welcoming them to the cricket quiz app as you can see e in the screenshot as shown below

 

 

 

 

Defining the MCQ Questions Alongside With Choices

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var questionOne =
{
  question: "\n 1.Who won the cricket world cup 2019? \n A. India \n B. England \n C. New Zealand \n D. Australia ",
  answer: "B"
}
 
var questionTwo =
{
  question: "2. Who is known as Jumbo? \n A. Ishant Sharma \n B. Rahul Dravid \n C. Harbhajan Singh \n D. Anil Kumble ",
  answer:"D"
}
 
var questionThree =
{
  question: "3. Who was the Player of the series in 2019 World Cup? \n A. Kane Williamson \n B. Ben Stokes \n C. Rohit Sharma \n D. Shakib Al Hasan ",
  answer: "A"
}
 
var questionFour =
{
  question: "4. Who was the Player of the series in IPL 2020? \n A. Marcus Stoinis \n B. Trent Boult \n C. Jofra Archer \n D. Kagiso Rabada?  ",
  answer: "C"
}
 
var questionFive =
{
  question:"5. Who won the Purple Cap in IPL 2020? \n A. Mohammad Shami \n B. Trent Boult \n C. Jofra Archer \n D. Kagiso Rabada?  ",
  answer: "D"
}
 
var questions = [questionOne, questionTwo, questionThree, questionFour, questionFive];

 

 

 as you can see that guys we are now collecting for defining all the questions Alongside with their multiple choices for the answer and also so we are adding the answer in the next property and we are storing all these questions inside an array a list of five questions are there inside the array

 

 

JavaScript
1
2
3
4
5
//loop
for(var i=0; i<questions.length; i++)
{
  play(questions[i].question, questions[i].answer)
}

 

Now as you can see in the above code we are making use of a for loop and inside each iteration we are making use of a custom function which is called as play and inside that we are passing the question alongside with the answer as well

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
function play(question, answer)
{
  var userAnswer = rs.question(question);
 
  if(userAnswer.toUpperCase() === answer.toUpperCase())
  {
    console.log("\n Right Answer");
    score = score + 10;
  }
  else
  {
    console.log("\n Wrong Answer");
    score = score - 5;
  }
   console.log("Your Current Score:", score);
    console.log("----------");
}

 

 

 Now inside the play method was basically we are receiving two arguments first is the question and second is the answer and here we are simply first reading the question using the custom module that we have imported and after that we are comparing the answer which is given by the user with the original answer if the answer matches then we are incrementing the score by 5 and if the answer is not correct then in that case we are decrementing the score by five and lastly we are showing the current score of the user in the terminal

 

 

 

 

Full Source Code

 

 

Wrapping it this blog post. Just copy paste the full source code of the cricket quiz app in node.js as shown below

 

 

index.js

 

 

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
var rs = require("readline-sync");
var score = 0;
var username = rs.question("Please enter your Name:");
 
console.log("Welcome", username + " to the quiz created by Deepank ");
console.log("--------------")
 
 
function play(question, answer)
{
  var userAnswer = rs.question(question);
 
  if(userAnswer.toUpperCase() === answer.toUpperCase())
  {
    console.log("\n Right Answer");
    score = score + 10;
  }
  else
  {
    console.log("\n Wrong Answer");
    score = score - 5;
  }
   console.log("Your Current Score:", score);
    console.log("----------");
}
 
var questionOne =
{
  question: "\n 1.Who won the cricket world cup 2019? \n A. India \n B. England \n C. New Zealand \n D. Australia ",
  answer: "B"
}
 
var questionTwo =
{
  question: "2. Who is known as Jumbo? \n A. Ishant Sharma \n B. Rahul Dravid \n C. Harbhajan Singh \n D. Anil Kumble ",
  answer:"D"
}
 
var questionThree =
{
  question: "3. Who was the Player of the series in 2019 World Cup? \n A. Kane Williamson \n B. Ben Stokes \n C. Rohit Sharma \n D. Shakib Al Hasan ",
  answer: "A"
}
 
var questionFour =
{
  question: "4. Who was the Player of the series in IPL 2020? \n A. Marcus Stoinis \n B. Trent Boult \n C. Jofra Archer \n D. Kagiso Rabada?  ",
  answer: "C"
}
 
var questionFive =
{
  question:"5. Who won the Purple Cap in IPL 2020? \n A. Mohammad Shami \n B. Trent Boult \n C. Jofra Archer \n D. Kagiso Rabada?  ",
  answer: "D"
}
 
var questions = [questionOne, questionTwo, questionThree, questionFour, questionFive];
 
//loop
for(var i=0; i<questions.length; i++)
{
  play(questions[i].question, questions[i].answer)
}
 
 
console.log("\n \n  Excellent! Your Final score: ", score);

 

 

 

 

Recent Posts

  • Node.js Express Project to Upload Image Buffer & BLOB Data to MongoDB Database & Display it in Browser Using Javascript
  • Node.js Tutorial to Export Images to PDF Document With Effects Using FilePix Library in Javascript
  • Node.js Tutorial to Export All Pages of PDF Document to Images and Save it in Javascript
  • Node.js OfficeGen Example to Add Text & Images in Powerpoint Presentation in Javascript
  • Node.js OfficeGen Example to Generate Excel Files By Adding Data inside Cells & Sheets in Javascript
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme