<?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>Forem: fawazsullia</title>
    <description>The latest articles on Forem by fawazsullia (@fawazsullia).</description>
    <link>https://forem.com/fawazsullia</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%2F593165%2F820630de-e653-424c-95e5-7476889088c8.jpg</url>
      <title>Forem: fawazsullia</title>
      <link>https://forem.com/fawazsullia</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/fawazsullia"/>
    <language>en</language>
    <item>
      <title>Build a Password Generator API on Nodejs</title>
      <dc:creator>fawazsullia</dc:creator>
      <pubDate>Mon, 14 Jun 2021 11:27:46 +0000</pubDate>
      <link>https://forem.com/fawazsullia/build-a-password-generator-api-on-nodejs-40cm</link>
      <guid>https://forem.com/fawazsullia/build-a-password-generator-api-on-nodejs-40cm</guid>
      <description>&lt;p&gt;Some sites auto generates complex passwords during user signup.&lt;/p&gt;

&lt;p&gt;I found this cool and wanted to try building a similar feature. &lt;/p&gt;

&lt;p&gt;Instead of turning it into an app, I decided to create a public API, so that anyone could use this in their applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  Features:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The password by default should have only lower case letters. &lt;/li&gt;
&lt;li&gt;Users can select if they want to add uppercase letters, numbers and characters to it.&lt;/li&gt;
&lt;li&gt;Users can select the password length&lt;/li&gt;
&lt;li&gt;Password should be random with no predictable pattern&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So grab your popcorns, this is going to be a detailed one.&lt;/p&gt;

&lt;p&gt;For you all super curious folks out there, here's the &lt;a href="https://github.com/fawazsullia/password-generator"&gt;link&lt;/a&gt; :)&lt;/p&gt;

&lt;p&gt;Now, on with the tutorial.&lt;/p&gt;

&lt;h3&gt;
  
  
  Tech requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Nodejs installed&lt;/li&gt;
&lt;li&gt;Express js&lt;/li&gt;
&lt;li&gt;Cors&lt;/li&gt;
&lt;li&gt;dotenv&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The usual server stuff
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Create an empty package.json file inside the directory with npm init&lt;/li&gt;
&lt;li&gt;Next, install dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We need express to handle the routes, cors to allow cross-origin access and dotenv to access our environment variables.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;npm i express dotenv cors&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Require all the dependencies and your index.js should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const express = require('express');

const app = express();

const cors = require('cors');

dotenv.configure();

//Defining port

const PORT = process.env.PORT | 5000;

//listen to the server event on PORT

app.listen(PORT)

//Add routes

app.get('/generate', (req, res) =&amp;gt; {
//we will come back here later
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The password generating algorithm
&lt;/h2&gt;

&lt;p&gt;To avoid cluttering, we'll write the main algorithm in a separate file.&lt;/p&gt;

&lt;p&gt;Go ahead. Create a file and name it whatever you want.&lt;/p&gt;

&lt;p&gt;I’ll create a wrapper function ( generate-password ) and then export it. This function takes in 4 parameters and will return us the password.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parameters:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Length of password (default 8)&lt;/li&gt;
&lt;li&gt;Caps required (default false)&lt;/li&gt;
&lt;li&gt;Number required (default false)&lt;/li&gt;
&lt;li&gt;Special character required ( default false )&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Inside this function, I’ll create 4 different functions that generates lower case, upper case, number and a special character respectively. We will use these functions later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generate number

let generateNumber = () =&amp;gt; {
let number = Math.floor(9*Math.random());
return number.toString();
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generate a special character

let generateChar = () =&amp;gt; {
const charArray = [64, 38, 35, 37, 36, 42, 43];
let index = Math.floor(7*Math.random());
return String.fromCharCode(charArray[index]);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generate capital letters

let generateCaps = ()=&amp;gt;{
return String.fromCharCode(Math.floor(25 * Math.random() + 65));
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generate small case letters

let generateSMall = ()=&amp;gt;{
return String.fromCharCode(Math.floor(25 * Math.random() + 97))
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to generate the whole password, we create a new function that takes in the length of the password required as the parameter.&lt;/p&gt;

&lt;p&gt;Next, create a global array and set it to empty. We will push the password into this.&lt;/p&gt;

&lt;p&gt;We will set the max-number of upper-case letters or special characters or numbers to (password-length)/3. The rest would be small case alphabets.&lt;/p&gt;

&lt;p&gt;The password will contain numbers, uppercase letters or special chars only if the users specify it, if not we go with the default false.&lt;/p&gt;

&lt;p&gt;Let's create 3 if statements. These will push number, sp.char or uppercase to the password array, depending on the wrapper function parameters.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//generate the whole pass

let generate = (len) =&amp;gt; {

let turn = Math.floor(len/3);
var rem = len;

if(charRequired){
  let temp = Math.floor(turn * Math.random() + 1);
  for(i=0; i&amp;lt;temp; i++){
    passwordArray.push(generateChar());
    }
  rem = rem - temp;
}

if(numRequired){
  let temp = Math.floor(turn * Math.random() + 1);
  for(i=0; i&amp;lt;temp; i++){
    passwordArray.push(generateNumber());
    }
  rem = rem - temp;
}

if(capsRequired){
  let temp = Math.floor(turn * Math.random() + 1);
  for(i=0; i&amp;lt;temp; i++){
    passwordArray.push(generateCaps());
    }
  rem = rem - temp;
}

for(i=0; i&amp;lt;rem; i++){
  passwordArray.push(generateSMall());
  }

return passwordArray;

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

&lt;/div&gt;



&lt;p&gt;Note that, we just only set the upper limit of the type of character pushed to the password array. The actual value is decided by the Math.random() method.&lt;/p&gt;

&lt;p&gt;This pretty much gives us the password, but it's not random enough. Look closely, there's a pattern. The password will always have sp.char, number, uppercase and lowercase in the same order.&lt;/p&gt;

&lt;p&gt;Let’s add one more layer of randomness to it.&lt;/p&gt;

&lt;p&gt;I made use of a standard shuffling algo to jumble the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//shuffle Array
function shuffleArray(arr){
    for (let i = arr.length - 1; i &amp;gt; 0; i--) {
        let j = Math.floor(Math.random() * (i + 1));
        var temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
    return arr;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We then convert this into string with &lt;code&gt;array.join()&lt;/code&gt; and return it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Back to index.js
&lt;/h3&gt;

&lt;p&gt;Let’s add a basic routing.&lt;/p&gt;

&lt;p&gt;I’m using a get method on the “/generate” route. &lt;/p&gt;

&lt;p&gt;Remember that we had 4 parameters for the password generating function? We get values to that using query parameters.&lt;/p&gt;

&lt;p&gt;Destructure caps, num, char, len from req.parameters object.&lt;/p&gt;

&lt;p&gt;Invoke the function with the above password and send the password generated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get('/generate', cors(), (req, res)=&amp;gt;{

let {caps, num, char, len} = req.query;

let passtoSend = password(num, char, len, caps);
res.status(200).json({ data : passtoSend}).end();
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That’s all.&lt;/p&gt;

&lt;p&gt;The API we just built is accessible at basehost/generate&lt;/p&gt;

&lt;p&gt;You can add query parameters for additional complexity. &lt;/p&gt;

&lt;h2&gt;
  
  
  What next?
&lt;/h2&gt;

&lt;p&gt;You can either build a similar backend API or a front end that fetches this API to generate passwords for users.&lt;/p&gt;

&lt;p&gt;You can read about accessing the API &lt;a href="https://github.com/fawazsullia/password-generator"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you found this useful, consider connecting with me on &lt;a href="https://www.twitter.com/realfawazsullia"&gt;twitter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;See you in the next one!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to send Email with node js for free </title>
      <dc:creator>fawazsullia</dc:creator>
      <pubDate>Mon, 31 May 2021 17:01:04 +0000</pubDate>
      <link>https://forem.com/fawazsullia/how-to-send-email-with-node-js-for-free-4okc</link>
      <guid>https://forem.com/fawazsullia/how-to-send-email-with-node-js-for-free-4okc</guid>
      <description>&lt;p&gt;If you are building a project that involves customer interactions, then probably at some point you would have to send them an email as well.&lt;/p&gt;

&lt;p&gt;For example, on successful form submits, you need to send a confirmation email. Or on every purchase, a receipt or order details.&lt;/p&gt;

&lt;p&gt;You could hook up some of the existing apis like send in blue, mail chimp etc, but you can do it for free in nodejs itself.&lt;/p&gt;

&lt;p&gt;Node Mailer is a nodejs module, that makes it easy to send emails.&lt;/p&gt;

&lt;p&gt;Here's how you do it;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&amp;gt; First, install Node Mailer&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install nodemailer&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&amp;gt; Then &lt;code&gt;require('nodemailer')&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&amp;gt; Create a transporter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Transporter is the object that is able to send the email. It contains data about the connection. &lt;br&gt;
I'm using gmail to send emails and this is how the transporter looks for me:&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const transporter = nodemailer.createTransport({&lt;br&gt;
    service: 'gmail',&lt;br&gt;
    auth: {&lt;br&gt;
      user: myemail@gmail.com,&lt;br&gt;
      pass: password&lt;br&gt;
    }&lt;br&gt;
  });&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&amp;gt; We also need an object containing the message to be sent&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const mailOptions = {&lt;br&gt;
    from: 'The Idea project',&lt;br&gt;
    to: toAddress,&lt;br&gt;
    subject: 'My first Email!!!',&lt;br&gt;
    text: "This is my first email. I am so excited!"&lt;br&gt;
  };&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;You can send html emails with html key instead of text.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;-&amp;gt; Next, to actually send the email, use &lt;br&gt;
  &lt;code&gt;transporter.sendMail(mailOptions, callback)&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The call back takes error and info arguments and is executed once the sending process is complete. You can use this to log errors if any.&lt;/p&gt;

&lt;p&gt;You can customise the emails you send, where you send from and how you send it anyway you want. Read the docs &lt;a href="https://nodemailer.com/about/"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The complete code should look like this,&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;![Full code nodemailer](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i116of2cgdf0d7eljgtb.png)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>css</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Less annoying CSS in React with CSS modules</title>
      <dc:creator>fawazsullia</dc:creator>
      <pubDate>Tue, 18 May 2021 18:15:10 +0000</pubDate>
      <link>https://forem.com/fawazsullia/less-annoying-css-in-react-with-css-modules-38kk</link>
      <guid>https://forem.com/fawazsullia/less-annoying-css-in-react-with-css-modules-38kk</guid>
      <description>&lt;p&gt;A fundamental problem with CSS is that it's globally scoped. &lt;/p&gt;

&lt;p&gt;So, if you have a number of components in your React app, you would have to think of unique classes to name your components.&lt;/p&gt;

&lt;p&gt;And the bigger your app gets, the harder.&lt;/p&gt;

&lt;p&gt;This is where CSS modules come in. &lt;/p&gt;

&lt;p&gt;CSS modules let you create CSS files that are locally scoped. &lt;/p&gt;

&lt;p&gt;Here's how you do it;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a CSS file with &lt;code&gt;.module.css&lt;/code&gt; extension&lt;/li&gt;
&lt;li&gt;In your component, use &lt;code&gt;import * as anyName from 'relative path'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;And in yout jsx elements, use &lt;code&gt;className={anyName.nameofclass}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;In your module.css file, use &lt;code&gt;.nameofclass&lt;/code&gt; to select the elements&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note that, you can select child elements with .nameofclass element name as well (.container button)&lt;/p&gt;

&lt;p&gt;This way, you don't have to worry about thinking of unique names to name your class.&lt;/p&gt;

&lt;p&gt;Note: Css modules are not intrinsic css features. On compilation, they get compiled to normal css. You would need the right compiler configuration to be able to use this. If you use web pack, it's already included.&lt;/p&gt;

&lt;p&gt;If you found this useful, let me know in the comments. if there's a better way to CSS in react, drop a comment&lt;/p&gt;

</description>
      <category>react</category>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Reduce React app loading time with lazy loading</title>
      <dc:creator>fawazsullia</dc:creator>
      <pubDate>Sun, 16 May 2021 17:42:35 +0000</pubDate>
      <link>https://forem.com/fawazsullia/reduce-react-app-loading-time-with-lazy-loading-4o56</link>
      <guid>https://forem.com/fawazsullia/reduce-react-app-loading-time-with-lazy-loading-4o56</guid>
      <description>&lt;p&gt;When I pushed a front end portfolio project to production, I noticed that there was a delay when the web app loaded for the first time.&lt;/p&gt;

&lt;p&gt;Being a beginner, I started researching why. Here's what I found out:&lt;/p&gt;

&lt;p&gt;React is based on components ( obviously ) and it makes use of bundlers like webpack to bundle up all the imports ( components, views etc) into a single file, which is then loaded when the user first opens the app.&lt;/p&gt;

&lt;p&gt;Which means, even if the user never visits a particular component,it is still loaded.&lt;/p&gt;

&lt;p&gt;How do we solve this? By code splitting.&lt;/p&gt;

&lt;p&gt;The solution is to dynamically import components depending on the route or components the user is trying to access. This results in only those components loading, thereby reducing the load time.&lt;/p&gt;

&lt;p&gt;Here's how the syntax is;&lt;/p&gt;

&lt;p&gt;Without lazy, &lt;code&gt;import About from './about'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With lazy, &lt;code&gt;const About = React.lazy(()=&amp;gt; {import './about'}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Additionally, we use Suspense, which allows us to use a fall back when the components are loading.&lt;/p&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Suspense fallback={&amp;lt;p&amp;gt;Loading&amp;gt;&amp;lt;/p&amp;gt;}&amp;gt;&lt;br&gt;
&amp;lt;App /&amp;gt;&lt;br&gt;
&amp;lt;/Suspense&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For detailed explanation on this, visit &lt;a href="https://reactjs.org/docs/code-splitting.html"&gt;https://reactjs.org/docs/code-splitting.html&lt;/a&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How I built a simple 'word guessing game' using Javascript ( for Beginners )</title>
      <dc:creator>fawazsullia</dc:creator>
      <pubDate>Fri, 14 May 2021 13:10:30 +0000</pubDate>
      <link>https://forem.com/fawazsullia/how-i-built-a-simple-word-guessing-game-using-javascript-for-beginners-448j</link>
      <guid>https://forem.com/fawazsullia/how-i-built-a-simple-word-guessing-game-using-javascript-for-beginners-448j</guid>
      <description>&lt;p&gt;I wanted to build something fun for my portfolio. After spending sometime thinking, I decided to code a game that was popular in my childhood.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hangman&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Players will have to fill the dashes with letters, until they guess the word. The game ends if the player guesses the correct word or if he chooses the wrong letter 5 times.&lt;/p&gt;

&lt;p&gt;Here’s how the game looks:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf405aexymb4io1tubdw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftf405aexymb4io1tubdw.png" alt="Word guessing game"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we start building, let's see what the game needs to do.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Generate a random word&lt;/li&gt;
&lt;li&gt;Generate dashes for the players to fill letters&lt;/li&gt;
&lt;li&gt;Provide letters for the players to choose&lt;/li&gt;
&lt;li&gt;End the game when the player loses or wins&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, with that sorted, lets start building.&lt;/p&gt;

&lt;p&gt;Also, you might want to keep &lt;a href="https://fawazsullia.github.io/guesstheword.github.io/game.html" rel="noopener noreferrer"&gt;the game&lt;/a&gt; and the &lt;a href="https://github.com/fawazsullia/guesstheword.github.io" rel="noopener noreferrer"&gt;source code&lt;/a&gt; open for reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  HTML:
&lt;/h2&gt;

&lt;p&gt;The basic structure of the game is quite simple.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A section for the title : This section contains the title and the number of chances a player has left&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A section for the word: The blank space for the word is displayed here. We will dynamically display this, depending on the word length&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A section for the letters: English alphabet to select will be displayed here. You can create a span tag for each letter, but it is much easier to do this with Javascript.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Styling
&lt;/h2&gt;

&lt;p&gt;Styling entirely depends on your preferences, but to get started, here’s the link to my &lt;a href="https://github.com/fawazsullia/guesstheword.github.io/blob/master/game.css" rel="noopener noreferrer"&gt;stylesheet&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The dynamics of the game ( Javascript )
&lt;/h2&gt;

&lt;p&gt;As I said earlier, we need to create a &lt;strong&gt;section of alphabets&lt;/strong&gt; that the players can select. &lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create an array of alphabets, from a - z&lt;/li&gt;
&lt;li&gt;Map through this array with array.map&lt;/li&gt;
&lt;li&gt;For each alphabet on the array, create a span node, set the id to the alphabet and set a common classname ("alphabet")&lt;/li&gt;
&lt;li&gt;Create a text node with the letter as the inner text&lt;/li&gt;
&lt;li&gt;Append the textnode to the span and the span to our div ( alphabet section)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It should look something like this ( after styling )&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdcqxklapl706yn6gkeu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhdcqxklapl706yn6gkeu.png" alt="alphabet section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Fetching a random word.
&lt;/h3&gt;

&lt;p&gt;I’m sure you have heard about the acronym &lt;strong&gt;API&lt;/strong&gt; before. We will be using an API called ‘&lt;strong&gt;Random word Api&lt;/strong&gt;’ by M C Naveen, to generate a random word. &lt;/p&gt;

&lt;p&gt;The endpoint we are going to use is &lt;a href="https://random-words-api.vercel.app/word" rel="noopener noreferrer"&gt;https://random-words-api.vercel.app/word&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Fetch request at endpoint&lt;/li&gt;
&lt;li&gt;Resolve the promise with .then. &lt;/li&gt;
&lt;li&gt;Resolve the json promise returned&lt;/li&gt;
&lt;li&gt;Inside the callback, invoke a function that adds functionality to the game. By doing this, we make sure that the game becomes functional only after the fetch is successful &lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The game function:
&lt;/h3&gt;

&lt;p&gt;Let’s split the fetched word into an array and store it.&lt;/p&gt;

&lt;p&gt;To create empty spaces for the players to fill in, we use a similar method we used to create alphabets, but instead of an alphabet, the textnode will be an empty string.&lt;/p&gt;

&lt;p&gt;Let’s then append it the div called word section.&lt;/p&gt;

&lt;p&gt;It should ook like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1gpe5cy8i60b3jjlzd2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fx1gpe5cy8i60b3jjlzd2.png" alt="letter section"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, using foreach, we add an event listener to each alphabet.&lt;/p&gt;

&lt;p&gt;We now define a call back for the event listener (click) we added in each letter, so that whenever a player clicks a letter, the call back is executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Inside the callback
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Select the letter with event.taget.innertext&lt;/li&gt;
&lt;li&gt;Check if the letter is in the word we fetched with array.includes&lt;/li&gt;
&lt;li&gt;Mark the letter as selected in the DOM ( for styling )&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now, the conditional. We have 2 conditions. \&lt;br&gt;
a. The selected letter is in the word fetched. &lt;br&gt;
b. The selected letter isn’t.&lt;/p&gt;

&lt;h3&gt;
  
  
  Selected letter is in the word
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Check where the selected letter is in the word and store it in an array(indexes). This lets us store multiple values.&lt;/li&gt;
&lt;li&gt;With DOM manipulation, add the letter in position stored in the array indexes&lt;/li&gt;
&lt;li&gt;Store the number of elements in the indexes array in a variable.&lt;/li&gt;
&lt;li&gt;If the variable becomes equal to word length, the player wins the game.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Selected letter is not in the word
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Increase the false count by 1&lt;/li&gt;
&lt;li&gt;When false count is 5, game over&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That’s it! You successfully created a Word Game. &lt;/p&gt;

&lt;p&gt;Feel free to build this on your own and modify it as you see fit.&lt;/p&gt;

&lt;p&gt;If you have questions or improvements, feel free to ping me here.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>html</category>
      <category>css</category>
    </item>
  </channel>
</rss>
