<?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: PrakharV10</title>
    <description>The latest articles on DEV Community by PrakharV10 (@prakharv10).</description>
    <link>https://dev.to/prakharv10</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%2F517909%2Fe698a2d9-5309-44e8-8f3c-85ebbd0b81e3.png</url>
      <title>DEV Community: PrakharV10</title>
      <link>https://dev.to/prakharv10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prakharv10"/>
    <language>en</language>
    <item>
      <title>Getting Started with Speech Recognition in JS</title>
      <dc:creator>PrakharV10</dc:creator>
      <pubDate>Tue, 08 Dec 2020 18:44:28 +0000</pubDate>
      <link>https://dev.to/prakharv10/getting-started-with-speech-recognition-in-js-gc1</link>
      <guid>https://dev.to/prakharv10/getting-started-with-speech-recognition-in-js-gc1</guid>
      <description>&lt;h2&gt;
  
  
  Skip if you don't like Intros -
&lt;/h2&gt;

&lt;p&gt;From Google Assistant to Apple's Siri, from Amazon's Alexa to Microsoft's Cortana, everywhere voice recognition is being used. Be it for searching something, or asking some questions, everyone enjoys a Hands-Free experience!!&lt;/p&gt;

&lt;p&gt;Did you know, even our websites can be integrated with Speech Recognition system? Just by using some commands, we can operate our websites, Change themes and even get Text to Voice Output.&lt;/p&gt;

&lt;p&gt;In this tutorial, we will walkthrough the process for setting up Speech Recognition using Web Speech API in JavaScript.&lt;/p&gt;

&lt;p&gt;Now without further much wait, let's get started -&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerequisites -
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;IDE installed for JavaScript/ You can also use &lt;a href="https://repl.it/"&gt;Repl.it&lt;/a&gt; as an online IDE.&lt;/li&gt;
&lt;li&gt;Basic knowledge of JavaScript. (Refer my previous &lt;a href="https://dev.to/prakharv10/building-a-f-r-i-e-n-d-s-quiz-using-javascript-4nmg"&gt;blog&lt;/a&gt; for an overview of JavaScript.&lt;/li&gt;
&lt;li&gt;And as always, The enthusiasm to learn!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding Web Speech API -
&lt;/h2&gt;

&lt;p&gt;As the name suggests Web Speech is basically an API for handling voice/texts commands.&lt;br&gt;&lt;br&gt;
It has 2 parts: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Speech Recognition (Voice to text)&lt;/li&gt;
&lt;li&gt;Speech Synthesis   (Text to Speech)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Note - The API is not compatible with all the browsers. The compatibility chart is given below.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Compatibility for &lt;code&gt;Speech Recognition&lt;/code&gt; -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nmCeaxqU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwr56o0n4bzjs7raa425.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nmCeaxqU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uwr56o0n4bzjs7raa425.JPG" alt="Compatibility for Speech recognition"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Compatibility for &lt;code&gt;Speech Synthesis&lt;/code&gt; -&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--h2d9Bi2M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oc53o8p8n87m4vacgick.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--h2d9Bi2M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/oc53o8p8n87m4vacgick.JPG" alt="Compatibility for Speech Synthesis"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Setting up -
&lt;/h2&gt;

&lt;p&gt;First of all, we start by setting the recognition interface to &lt;code&gt;Speech Recognition&lt;/code&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 SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition;
const recognition = new SpeechRecognition();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Function for firing the Speech Recognition -
&lt;/h2&gt;

&lt;p&gt;Now, we define the function for starting the Speech Recognition-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;recognition.onstart = function(){
    console.log("The Recognition has fired, start speaking!");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Note - The concole.log command will print the specified statement in the console(Can be seen by Inspect page or &lt;code&gt;Ctrl + Shift + I&lt;/code&gt;)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Function to handle the result of Recognition -
&lt;/h2&gt;

&lt;p&gt;After we are done speaking, The &lt;code&gt;onresult&lt;/code&gt; event handler returns a &lt;code&gt;SpeechRecognitionEvent&lt;/code&gt; with a property results which is a two-dimensional array. To use these results we define a &lt;code&gt;recognition.onresult&lt;/code&gt; function, as shown below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;recognition.onresult = function(event){
    var transcript = event.results[0][0].transcript; 
    document.querySelector("#OutputBox").textContent = transcript;

recognition.start();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;event.results&lt;/code&gt; matrix returned, contains the converted Speech as its 1st object named &lt;code&gt;transcript&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;By the help of &lt;code&gt;querySelector&lt;/code&gt;, We are able to get the ID of an element present in our HTML code and we can print the results in that element, so as to show it on the user's screen.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;recognition.start()&lt;/code&gt; is used to start the recognition.&lt;/p&gt;

&lt;h2&gt;
  
  
  Finishing up-
&lt;/h2&gt;

&lt;p&gt;The Speech recognition with just Speech-to-Text may seem a bit less useful, but integrating it with other ideas can make User Experience, a lot better. &lt;br&gt;
For example, It can used to&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fill forms, without actually typing each and every details.&lt;/li&gt;
&lt;li&gt;Change Themes by just using the specified keyword.&lt;/li&gt;
&lt;li&gt;Copy and Paste texts from One part of website to another.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;If you have followed this article till here, I'm sure you were able to integrate the Speech Recognition successfully.&lt;/p&gt;

&lt;p&gt;If you liked the article and found it to be of a little use to you, award me with a Unicorn 🦄.&lt;/p&gt;

&lt;p&gt;Also, Stay tuned for more tutorials, as in the next one I'll be integrating Speech Recognition for making an Awesome Web Application!&lt;/p&gt;

&lt;h4&gt;
  
  
  Sources -
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Speech_API"&gt;MDN web docs&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>webdev</category>
      <category>speechrecognition</category>
    </item>
    <item>
      <title>Building A F.R.I.E.N.D.S Quiz Using JavaScript </title>
      <dc:creator>PrakharV10</dc:creator>
      <pubDate>Fri, 27 Nov 2020 17:25:24 +0000</pubDate>
      <link>https://dev.to/prakharv10/building-a-f-r-i-e-n-d-s-quiz-using-javascript-4nmg</link>
      <guid>https://dev.to/prakharv10/building-a-f-r-i-e-n-d-s-quiz-using-javascript-4nmg</guid>
      <description>&lt;p&gt;In this tutorial, we will learn to create our first ever project : Creating a Quiz using only JavaScript with &lt;a href="https://repl.it/"&gt;Repl.it&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Without further wait, Let us begin :&lt;/p&gt;

&lt;h4&gt;
  
  
  Prerequisites :-
&lt;/h4&gt;

&lt;ol&gt;
&lt;li&gt;Create an account on &lt;a href="https://repl.it/"&gt;Repl.it&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Ready the Questions of your quiz, the options and the correct answer of the questions.&lt;/li&gt;
&lt;li&gt;Make a Blueprint, involving the marking schemes and the structures of levels to be included. (&lt;em&gt;optional&lt;/em&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Understanding Repl -
&lt;/h4&gt;

&lt;p&gt;A "Repl" at Repl.it is an interactive programming environment. We can create a workspace in most popular programming languages, where we are given a container on a virtual machine where our code can run. It allows us to write code and build apps and websites using a browser. Additionally, We can also share projects through various ways. &lt;/p&gt;

&lt;h4&gt;
  
  
  Importing Packages
&lt;/h4&gt;

&lt;p&gt;This is our one the most essential commands, for creation of our projects. We import packages - &lt;code&gt;readline-sync&lt;/code&gt; and &lt;code&gt;chalk&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;readline-sync&lt;/code&gt; - Allows our console to have a 'conversation' with the users. Basically, We use it to take input from users. The Input can be of any data-type eg- String, integer etc.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;chalk&lt;/code&gt; package is used for the beautification of our project. By the help of chalk we can introduce various text-colors, background-colors and styles to our text.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We import these packages by using command -&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h4&gt;
  
  
  Taking Input from the user -
&lt;/h4&gt;

&lt;p&gt;Next, we take Username as Input from our user, in order to display their name and make the Quiz &lt;em&gt;more interactive&lt;/em&gt;.&lt;br&gt;
We input username by using the readline-sync package and Display it(with enhancing their colors a bit by the chalk package) we installed in the previous step, as shown below -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var Username = myVar.question(chalk.red("Hi! What is your name?\n"));
console.log(chalk.cyanBright("Welcome " + Username));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note - &lt;code&gt;console.log()&lt;/code&gt; is used to print output to the console.&lt;/p&gt;

&lt;h4&gt;
  
  
  Introducing the Question Bank -
&lt;/h4&gt;

&lt;p&gt;Now, we define a variable named &lt;code&gt;questionBank&lt;/code&gt; which includes various JavaScript objects, in which our questions and answers would be included.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;An Object is a collection of properties, and a property is an association between a name (or key) and a value.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Below is an example, which shows the initialization of various Questions and Answers, to be asked in the quiz -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var questionBank = [{
  question : "Which of the actors on Friends unfortunately decides 
to whiten his teeth\n before going out on a first date with a girl\n
he's had a crush on for some time?\n",
  answer : "Ross"
},
{
  question : "Who was Ross's second wife?\n",
  answer : "Emily"
},
{
  question : "Who is Gunther's roommate?\n",
  answer : "Jasmine"
}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note - Here '\n' is used to change line, that will be shown on the user's screen.&lt;/p&gt;

&lt;h4&gt;
  
  
  Defining a Function, to check user-provided Answer to the Actual Answer -
&lt;/h4&gt;

&lt;p&gt;To define a function, we use the keyword &lt;code&gt;function&lt;/code&gt;, followed by the name of the functions, followed by the parameters to be passed in it. &lt;br&gt;
For eg- Here we define a function named 'checkAnswer', and we pass the question and answer from questionBank as its parameters.&lt;br&gt;
We output the respective question to the user and check their answer to our pre-defined answer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function checkAnswer(question , answer){
  var userAnswer = myVar.question(chalk.cyanBright(question));
    if(userAnswer == answer){
    console.log(chalk.green("Correct."));
    score ++;
    }

    else{
    console.log(chalk.red("Wrong."));
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  A loop to access all the questions of &lt;code&gt;questionBank&lt;/code&gt; -
&lt;/h4&gt;

&lt;p&gt;Now, we run  a for loop, in order to access every object of the &lt;code&gt;questionBank&lt;/code&gt; and then use &lt;code&gt;function checkAnswer()&lt;/code&gt; to check the user's answers. &lt;br&gt;
Additionally, we introduce a variable score, which can keep a check on the total score of the user.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var score = 0;
for(var i = 0; i&amp;lt;questionBank.length; i++){
    current = questionBank[i];
    checkAnswer(current.question,current.answer);
  }
  console.log(chalk.yellowBright("Total = " + score + " out of " 
+ questionBank.length));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note -&lt;code&gt;questionBank.length&lt;/code&gt; is used to get the number of objects present in the &lt;code&gt;questionBank&lt;/code&gt; list, we defined earlier.&lt;/p&gt;

&lt;p&gt;Our quiz is now complete! Press the Run command to play the quiz by yourself.&lt;/p&gt;

&lt;h4&gt;
  
  
  Sharing the Quiz with your Friends -
&lt;/h4&gt;

&lt;p&gt;Add ?embed=1&amp;amp;output=1 in the URL of your project to share the Quiz. For eg-&lt;br&gt;
Initial URL - &lt;code&gt;https://repl.it/@your-username/Project-name&lt;/code&gt;&lt;br&gt;
URL to share - &lt;code&gt;https://repl.it/@your-username/Project-name?embed=1&amp;amp;output=1&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next?
&lt;/h3&gt;

&lt;p&gt;If you have followed this article till here, I'm sure you were able to create the Quiz successfully. If you faced any difficulties, you can always refer to my &lt;a href="https://github.com/PrakharV10/Project-01---p2"&gt;code&lt;/a&gt;.&lt;br&gt;
Also if you want to play my version of the Quiz, you can do it &lt;a href="https://repl.it/@Prakhar10V/Project-01-p2?embed=1&amp;amp;output=1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you liked this tutorial, share it with your friends and help them learn!&lt;/p&gt;

&lt;p&gt;Also! Stay tuned for more of these informative tutorials. :)&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
      <category>replit</category>
    </item>
  </channel>
</rss>
