<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Khyati Baria</title>
    <description>The latest articles on DEV Community by Khyati Baria (@khyatibaria).</description>
    <link>https://dev.to/khyatibaria</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F566797%2F93fb0acc-fdf6-461b-b321-7104ab390132.jpeg</url>
      <title>DEV Community: Khyati Baria</title>
      <link>https://dev.to/khyatibaria</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/khyatibaria"/>
    <language>en</language>
    <item>
      <title>Build CLI Quiz App with nodeJs</title>
      <dc:creator>Khyati Baria</dc:creator>
      <pubDate>Wed, 15 Dec 2021 11:28:25 +0000</pubDate>
      <link>https://dev.to/khyatibaria/build-cli-quiz-app-with-nodejs-1e85</link>
      <guid>https://dev.to/khyatibaria/build-cli-quiz-app-with-nodejs-1e85</guid>
      <description>&lt;p&gt;In this blog, we will build a Food Fact CLI Quiz App in JavaScript. We will use repl.it to write a program. We will use two npm modules&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;readlineSync -It will have a conversation with the user via a console.&lt;/li&gt;
&lt;li&gt;chalk: It is used for Terminal string styling and adding colors.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Planning our CLI App:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;We ask the user to enter name&lt;/li&gt;
&lt;li&gt;Then we welcome the user&lt;/li&gt;
&lt;li&gt;We will display the Rules of the game to user&lt;/li&gt;
&lt;li&gt;Then we let users to play game&lt;/li&gt;
&lt;li&gt;Check that user has entered the correct answer&lt;/li&gt;
&lt;li&gt;We will print the current score of a user on every given answer&lt;/li&gt;
&lt;li&gt;We will display the total score of users at the end of the game&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now Let’s Start the Build We need to take the user input through the console so we will require npm package: readline-sync. Firstly we will install npm packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var readlineSync = require('readline-sync');
const chalk = require('chalk');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the above code in repl.it will automatically install these packages for us.&lt;/p&gt;

&lt;h2&gt;
  
  
  Asking user their Name
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var userName= readlineSync.question( ("Please Enter Your Name?"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need a variable to store the name of the user so we will create a variable called userName.&lt;/p&gt;

&lt;h2&gt;
  
  
  Welcome Message for User
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log('WELCOME '+userName+ 'FOOD FACT QUIZ \n');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to display customize welcome message for every new user. For welcome message we need name of user with some greeting message. So we use string concatenation to show welcome message.&lt;/p&gt;

&lt;h2&gt;
  
  
  Showing Rules of the game to the user
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  console.log ("RULES OF THE GAME ARE SIMPLE"); 
  console.log ("1). All the QUESTIONS are COMPULSORY");
  console.log ("2). If you answer RIGHT you score 2 Points");
  console.log ("3). If you answer WRONG you Lose 1 Point");
  console.log ("----------LET’S START THE GAME------------");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use inbuilt JavaScript function console.log() to print Rules on screen.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding Question of the quiz
&lt;/h2&gt;

&lt;p&gt;To store Questions of the quiz we create an array of an objects. And to ask question to user we use readlineSync.question(question).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var quesBank=[
  { question: `
  What country is renowned for chocolate?
  a) Finland
  b) Belgium
  c) Argentina\n`,
    answer: "b"
  },
  { question: `
  Which of these was called "food of the gods" in ancient India?
  a) Yogurt
  b) Potato
  c) Bread\n`,
    answer: "a"
  },
  { question: `
  Which is the most stolen food in the world?
  a) Candy
  b) Cheese
  c) Chips\n`,
    answer: "b"
  },
  { question: `
  One food that all races eat is what?
  a) Chocolate
  b) Bread
  c) Cheese\n`,
    answer: "b"
  }];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need a function which traverse throughout array of objects and displays the each question to user. So we need a for loop to access all elements inside an array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function play(){
   for(var i=0; i&amp;lt;quesBank.length;i++){
     var currentItem= quesBank[i];
     check(currentItem.question, currentItem.answer)
   } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Check if user answer is correct
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function check(question, answer)
{
   var userAns=readlineSync.question(question);

   if( userAns.toUpperCase() === answer.toUpperCase())
   {
    console.log();
    console.log(chalk.green.italic.bold("Your are Right!!!!!"));
    console.log();
    score=score+2;
   } else{
    console.log();
    console.log(chalk.red.italic.bold("Your are Wrong!!!!!"));
    console.log();
    score=score-1;
  } 
  console.log(chalk.bgWhite.blue.bold("Your Total Score is:",score));
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We create a function that compares user answer with correct answers in array of objects. We need a variable to store the user answer. So we create variable userAns to store user answer. Here we use branching if user answer is correct we increment score of user by 2 and if answer is wrong user score is decrease by 1. Once all questions are answered by user we will display Total score at the end of quiz Game.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;So final program will look like this&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const chalk = require('chalk');
var readlineSync = require('readline-sync');
var score=0;

function Welcome()
{
var userName= readlineSync.question("Please Enter Your Name?");
console.log(chalk.yellowBright.bold('WELCOME'+userName+'FOOD FACT QUIZ \n'));
console.log(chalk.cyanBright.bold("RULES OF THE GAME ARE SIMPLE")); 
console.log(chalk.cyanBright("1). All the QUESTIONS are COMPULSORY"));
console.log(chalk.cyanBright("2). If you answer RIGHT you score 2 Points"));
console.log(chalk.cyanBright("3). If you answer WRONG you Lose 1 Point"));
console.log(chalk.yellowBright.bold("-------LETS START THE GAME------"));
}

var quesBank=[
  {
    question: `
    What country is renowned for chocolate?
    a) Finland
    b) Belgium
    c) Argentina\n`,
        answer: "b"
  },
  {
    question: `
    Which of these was called "food of the gods" in ancient India?
    a) Yogurt
    b) Potato
    c) Bread\n`,
        answer: "a"
  },
  {
    question: `
    Which is the most stolen food in the world?
    a) Candy
    b) Cheese
    c) Chips\n`,
        answer: "b"
  },
  {
    question: `
    One food that all races eat is what?
    a) Chocolate
    b) Bread
    c) Cheese\n`,
        answer: "b"
  }];

function check(question, answer)
{
   var userAns=readlineSync.question(question);
   if( userAns.toUpperCase() === answer.toUpperCase())
   {
    console.log(chalk.green.italic.bold("Your are Right!!!!!"));
    score=score+2;
   } else{
    console.log(chalk.red.italic.bold("Your are Wrong!!!!!"));
    score=score-1;
  } 
  console.log(chalk.bgWhite.blue.bold("Your Score is:",score));
  console.log();
  console.log(chalk.yellowBright.bold("------------------------------")); 
  console.log();
}

function play(){
   for(var i=0; i&amp;lt;quesBank.length;i++){
     var currentItem= quesBank[i];
     check(currentItem.question, currentItem.answer)
   }
}

Welcome();
play();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Thanks for reading. If you like this articles, consider following me.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>cli</category>
      <category>node</category>
      <category>quizapp</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Data Types in JavaScript</title>
      <dc:creator>Khyati Baria</dc:creator>
      <pubDate>Thu, 09 Dec 2021 09:50:19 +0000</pubDate>
      <link>https://dev.to/khyatibaria/data-types-in-javascript-1mal</link>
      <guid>https://dev.to/khyatibaria/data-types-in-javascript-1mal</guid>
      <description>&lt;p&gt;Data types describe the characteristics of data stored in a variable. Data types can be divided into two:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Primitive Data types&lt;/li&gt;
&lt;li&gt;Non-primitive Data types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;1. Primitive Data Types&lt;/strong&gt;&lt;br&gt;
Primitive data types are immutable or non-modifiable data types. Once a primitive data type is created we cannot modify it. Primitive data types in JavaScript include:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Number&lt;/strong&gt;&lt;br&gt;
Number datatype can stores both integers and decimal values. Using Number data type we can do all the arithmetic operations.&lt;br&gt;
&lt;code&gt;let age = 21;&lt;br&gt;
var quantity= 12;&lt;br&gt;
const gravity = 9.81  // we use const for non-changing values&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Strings&lt;/strong&gt;&lt;br&gt;
Strings data type are used to store textual data. To declare a string, we need a variable name, assignment operator, and a value under a single quote, double quote, or backtick quote.&lt;br&gt;
&lt;code&gt;let userName = "Khyati";&lt;br&gt;
let city = 'Mumbai';&lt;br&gt;
let language = 'JavaScript';&lt;br&gt;
let space = ' ';           // an empty space string&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Booleans&lt;/strong&gt;&lt;br&gt;
The Boolean data type can store only two values i.e. true and false.This type is commonly used to store yes or no types values.&lt;br&gt;
&lt;code&gt;var yes = true;&lt;br&gt;
var no= false;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Null&lt;/strong&gt;&lt;br&gt;
null data type is special type of placeholder in JavaScript. Null value represents the intentional absence of any object value.&lt;br&gt;
&lt;code&gt;var vaule = null;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Undefined&lt;/strong&gt;&lt;br&gt;
The undefined data type is special type of placeholder in JavaScript, undefined means “value is not assigned”. If a variable is declared, but not assigned, then its value is undefined.&lt;br&gt;
&lt;code&gt;var  userName;&lt;br&gt;
console.log(userName);  // shows "undefined"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Non-primitive Data Types&lt;/strong&gt;&lt;br&gt;
Non-primitive data types are modifiable or mutable. We can modify the value of non-primitive data types even after it gets created. Non-primitive data types in JavaScript includes:-&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Arrays&lt;/strong&gt;&lt;br&gt;
Arrays data type are used to store multiple values at a time in single variable. An array is a list of values store within a variable, and can access these values by referring to their index number.&lt;br&gt;
&lt;code&gt;let numbers = [1, 2, 3];&lt;br&gt;
let fruits=['apple', 'banana', 'mango'];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Objects&lt;/strong&gt;&lt;br&gt;
Objects are the complex data type in JavaScript. Object tries to map real like things in programing. Object stores the data in form of key value pair. Where key is string and value can be anything.&lt;br&gt;
&lt;code&gt;let car={&lt;br&gt;
name: 'Honda City',&lt;br&gt;
model: 'VMT' ,&lt;br&gt;
color: 'Black',&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So this is it for this article. If you find it informative please leave a like and consider following me. Thanks for reading.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How JavaScript Code is Executed?</title>
      <dc:creator>Khyati Baria</dc:creator>
      <pubDate>Wed, 08 Dec 2021 10:43:53 +0000</pubDate>
      <link>https://dev.to/khyatibaria/how-javascript-code-is-executed-15ap</link>
      <guid>https://dev.to/khyatibaria/how-javascript-code-is-executed-15ap</guid>
      <description>&lt;p&gt;JavaScript is a &lt;strong&gt;synchronous single-threaded language&lt;/strong&gt; meaning JavaScript can only execute one command at a time in a specific order. It can only go to the next line of code once the previous line of code is executed. &lt;strong&gt;Everything in JavaScript happens inside an Execution Context&lt;/strong&gt;. Imagine Execution Context as a big box inside which the whole JavaScript codes gets executed whenever a JavaScript program is run an execution context is created. Execution Context has two components they are:-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Variable Environment (Memory Component)&lt;/li&gt;
&lt;li&gt;Thread of Execution (Code Component)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example&lt;br&gt;
&lt;code&gt;1)  var number=10;  &lt;br&gt;
2)  function add (n) &lt;br&gt;
3)  {&lt;br&gt;
4)  var result=n+n; &lt;br&gt;
5)  return result;  &lt;br&gt;
6)  } &lt;br&gt;
7)  var result1= add(n); &lt;br&gt;
8)  var result2= add(4);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the above code is executed a global execution context is created. Global execution context gets created in two-phase&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Memory Creation Phase&lt;/strong&gt; in this phase, the JavaScript engine runs through the whole program and allocates the memory space for all the variables and functions present in the program. The variables in the program are store with placeholder undefined and the function is stored as it is.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Execution Phase&lt;/strong&gt; is the 2nd phase, in which JavaScript code is executed line by line when the engine executes the 1st line of code the variable value is updated from undefined to 10. It comes to the 2nd line where the function is declared and it skips lines 2 to 6 since the function is not invoked yet. Now line 7 in the program is executed where a function is invoked so it goes back to where a function is declared on line 2 and starts executing the function.&lt;/p&gt;

&lt;p&gt;Function in JavaScript are like mini-programs whenever JavaScript executes a function a new execution context is created. so when line 7 of program gets executed a function is invoked and execution context for function gets created in two phases Memory Creation phase where variables in a function are store with placeholder undefined. In the 2nd phase i.e. Code execution phase variable n is assigned to value 10 and line 4 is executed result value is calculated and stored in the result variable. Now it comes to line 5 where the return result indicates the function to return the calculated result value to the global execution context and control goes to the global execution context. Now execution context created for function gets deleted. And the value of variable result1 is updated to 20.&lt;/p&gt;

&lt;p&gt;Js engine goes to next line 8 again here the function is invoked and new execution context gets created same as mention above process happens once the function returns the value and control goes back to the global execution context. And execution context created for function gets deleted. Value of variable on line 8 is updated to 8. Now the whole JavaScript program is executed so global context also gets delete.&lt;/p&gt;

&lt;p&gt;A Call stack is used by JavaScript to maintain the &lt;strong&gt;"Order of execution in execution contexts"&lt;/strong&gt;. Global execution context is created at the start of the program execution and all other new execution context is pushed on top of it. It works similarly as a stack whenever a new function is invoked its execution context is pushed into the call stack. When function execution gets finished its execution context is pop out from the call stack.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;So this is it for this article. If you find it informative please leave a like and consider following me. Thanks for reading.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
