All You Need For The First CLI App
What is a CLI?
CLI stands for COMMAND LINE INTERFACE . As the name suggests, it is used for interacting using some commands. Simply, it is a way of interaction with the user and browser without any graphics i.e majorly in plain text
Anything you do consists of mainly three things :
- Input
- Processing
- Output
The same you need to remember while building your FIRST CLI APP.
Before we jump to code, go check out the Basic Terminology that would be used.
LET'S BEGIN , BY MAKING A DO YOU KNOW ME QUIZ !!!!
Let's make the app on the online IDE . I am using REPL.it . Make your account and create a new REPL(language Node.js / javascript).
As we learned three things are a must , INPUT , PROCESSING AND OUTPUT .
For INPUT , we will be using readline-sync and for making our CLI app a little colorful we will be using chalk .
In case , if you are not using REPL.IT or any other online IDE , begin with Step 1 . Rest can start with Step 2 as online IDE will automatically install the packages as you use them
Step 1 : To Install the packages :-
- npm install readline-sync
- npm install chalk
Step 2 : To tell your app that we need the packages and we are going to use them .
const readLineSync = require('readline-sync');
const chalk = require('chalk');
Step 3 : Now we need to know the person's name who is playing the game , because w address people with their names.
const userName = readLineSync.question(
chalk.cyanBright("What's your good name?")
);
// chalk.cyanBright represents the color in which it will be visible.
Step 4 : We need an array to store all the questions and their correct answers. I am using one of the types in readline-sync i.e the user can I/P answer only in a yes(y) or no(n). You can choose whatever you would like to , as per your choice from the docs.
Also initialise the score to 0;
var score = 0;
var questionListOne =[{
question:"She lives in Delhi.",
answer:false
},
{
question:"She has a younger brother.",
answer:true
},
{
question:"She has watched 'FRIENDS'.",
answer:false
},
{question:"She loves Pizza over Burger.",
answer:true},
{
question:"She does'nt know cooking,",
answer:true
}
]
Step 5 : Now I need to map the questions , so I will be needing a for-loop. But in the loop I need to check whether the answer matches to what user has input , if it does , increase the score by 1 , and if it does'nt decrease the score by 1 or make no changes in the score.
function checkAns(ques,correctAns)
{
var userAns = readLineSync.keyInYNStrict(chalk.bold.yellowBright(ques));
if(userAns == correctAns)
{
console.log(chalk.greenBright(userName +" , Correct Answer"));
score = score + 1;
console.log(chalk.greenBright("Score = "+ score));
}
else
{
console.log(chalk.redBright(userName +" , Wrong Answer score remains same"));
}
}
for(var i =0;i<questionListOne.length;i++)
{
currQues =questionListOne[i]
console.log(); // to give a empty line
checkAns(currQues.question,currQues.answer)
}
Step 6 : Now comes the final step , that is to show the user his/her final score.
console.log(chalk.bgYellow("\nTHE QUIZ ENDED !! FINAL SCORE IS :" + score + " "));
## HERE YOU GO , YOUR FIRST CLI APP IS COMPLETE
If you still find any difficulty or want to checkout my version of "DO YOU KNOW ME". Checkout the link ( Click Here ) and make your version of it .
CHALLENGE : ADD LEVELS TO YOUR CLI APP . IF THE PERSON SCORES SUPPOSE 50% OR MORE THEN ONLY ALLOW HIM TO PLAY IN THE NEXT LEVEL .




