<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ Kingsley Ubah - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ Kingsley Ubah - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Thu, 28 May 2026 20:56:25 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/author/kingchuks/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ How to Create a Chatbot With the ChatGPT API ]]>
                </title>
                <description>
                    <![CDATA[ OpenAI's ChatGPT is a great tool for getting information as quickly as possible for your coding projects. Even better, you can now integrate the artificial intelligence-powered chat capability of OpenAI's models directly into your application. Recent... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-chatbot-with-the-chatgpt-api/</link>
                <guid isPermaLink="false">66d4618137bd2215d1e24604</guid>
                
                    <category>
                        <![CDATA[ #chatbots ]]>
                    </category>
                
                    <category>
                        <![CDATA[ chatgpt ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Node.js ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 26 Jul 2023 15:48:02 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/07/levart_photographer-drwpcjkvxuU-unsplash--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>OpenAI's ChatGPT is a great tool for getting information as quickly as possible for your coding projects. Even better, you can now integrate the artificial intelligence-powered chat capability of OpenAI's models directly into your application.</p>
<p>Recently, the OpenAI team expanded their API by giving developers access to their pretrained AI models (DALL-E, Codex, and GPT-3). This means that you can send a question to the API, get the response, and use the data in your application, all within seconds.</p>
<p>In this article, you'll learn how to create an OpenAI account, retrieve your API keys and query OpenAI's GPT-3 model from your Node.js application. Let's dive right in!</p>
<h2 id="heading-how-to-sign-up-for-a-chatgpt-account"><strong>How to Sign Up for a ChatGPT Account</strong></h2>
<p>The first thing you need to do is sign up for an <a target="_blank" href="https://platform.openai.com/overview">OpenAI account</a> if you don't already have one. Once you're in, you'll be redirected back to the homepage.</p>
<p>At the top right corner of the page, click on your profile image, then click on Manage Account. On the sidebar, click API Keys and then click the <strong>create new secret key</strong> button to generate a secret key:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/Screenshot_of_OpenAI_API_key.jpg" alt="Screenshot_of_OpenAI_API_key" width="600" height="400" loading="lazy"></p>
<p><em>Screenshot of secret key</em></p>
<p>Copy the secret key and paste it somewhere safe and accessible because you'll need it later to connect your application with the OpenAI API.</p>
<p>With the key safely stored, the next step is to create a Node.js project and spin up an Express server on top of it. Let's start with the installation and basic setup.</p>
<h2 id="heading-how-to-set-up-the-project"><strong>How to Set up the Project</strong></h2>
<p>To follow along with this project, you need to have Node.js and npm installed on your local machine. The latest version of Node.js comes with npm, and it's available on the <a target="_blank" href="https://nodejs.org/en/download">official Node.js website</a>.</p>
<p>Start by creating an empty directory in your computer. Next, launch the command prompt and <code>cd</code> into the folder you just created:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">cd</span> path/to/project
</code></pre>
<p>Once pointed to the directory, run the following command to create a Node project:</p>
<pre><code class="lang-bash">npm init -y
</code></pre>
<p>Next, run the following command to install <code>express</code> and <code>openai</code> libraries from npm:</p>
<pre><code class="lang-bash">npm i express openai
</code></pre>
<p>The next step is to create the server.</p>
<h2 id="heading-how-to-create-an-express-server"><strong>How to Create an Express Server</strong></h2>
<p>For now, this server will only serve the static files. We'll implement the chat API towards the end of this article.</p>
<p>Start by creating a file named <strong>server.js</strong> inside the root folder of your project. Next, open the file with a code editor and add the following code:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> express =  <span class="hljs-built_in">require</span>(<span class="hljs-string">'express'</span>)
<span class="hljs-keyword">const</span> app = express()

app.use(express.static(<span class="hljs-string">'public'</span>))

app.listen(<span class="hljs-number">5000</span>, <span class="hljs-function">()=&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Server is active"</span>)
})
</code></pre>
<p>With this code, you've created a web server that serves static files (i.e. HTML, CSS) from the <strong>/public</strong> folder.</p>
<p>Next, we'll create the HTML file that renders the chat interface on the web page, as well as the stylesheet file and the JavaScript file.</p>
<h2 id="heading-how-to-create-the-chatbot"><strong>How to Create the Chatbot</strong></h2>
<p>Start by creating a folder named public inside the root of your project. Then inside the <strong>/public</strong> directory, create a file named <strong>index.html</strong>.</p>
<p>Open the file with a text editor and add the following markup in the file:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"chat-area"</span>&gt;</span>

    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"submit-form"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">cols</span>=<span class="hljs-string">"40"</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">"3"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"btn"</span>&gt;</span>Submit<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>    
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>    

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>As you can see above, the page comprises of the chat area (where the messages are displayed) and the submit area (comprising the text area and the submit button).</p>
<p>To style the page, add the following stylesheet between the opening and closing <code>&lt;head&gt;</code> tags in your <strong>/public/index.html</strong> file:</p>
<pre><code class="lang-html">    <span class="hljs-tag">&lt;<span class="hljs-name">style</span>&gt;</span><span class="css">
        <span class="hljs-selector-id">#chat-area</span> {
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
            <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>;
            <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
            <span class="hljs-attribute">overflow</span>:scroll;
            <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid gray;
            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">4px</span>;
        }

        <span class="hljs-selector-class">.input</span> {            
            <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
        }

        <span class="hljs-selector-class">.submit-area</span>{
            <span class="hljs-attribute">justify-content</span>: center;
            <span class="hljs-attribute">display</span>: flex;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> auto;
            <span class="hljs-attribute">width</span>: <span class="hljs-number">80%</span>;            
        }       

        <span class="hljs-selector-tag">textarea</span> {
            <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
        }

        <span class="hljs-selector-class">.box</span> {
            <span class="hljs-attribute">width</span>: <span class="hljs-number">96%</span>;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
            <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">10px</span>;
            <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#C4DBFE</span>;
            <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> auto;            
        }

        <span class="hljs-selector-class">.answer</span> {
            <span class="hljs-attribute">background-color</span>: aquamarine;
        }

        <span class="hljs-selector-tag">button</span> {
            <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#004089</span>;
            <span class="hljs-attribute">color</span>: white;
            <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">10px</span>;
            <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
            <span class="hljs-attribute">border</span>: none;
        }
    </span><span class="hljs-tag">&lt;/<span class="hljs-name">style</span>&gt;</span>
</code></pre>
<p>If you save the file and check the browser, you should find your page like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/Document--1-.png" alt="Document--1-" width="600" height="400" loading="lazy"></p>
<p><em>Screenshot of the page</em></p>
<p>The chat area is empty for now because we haven't submitted any messages yet. To do that, we need to bring in JavaScript.</p>
<h2 id="heading-how-to-add-front-end-javascript"><strong>How to Add Front-end JavaScript</strong></h2>
<p>When the user inputs a message in the text area and clicks on the submit button, we'll send the message to the backend, get the response from the API and display it on the page.</p>
<p>Start by adding an empty script element within the <code>&lt;body&gt;</code> tags in <strong>index.html</strong>:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">script</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
</code></pre>
<p>Inside the script tags, we're to call the <code>getResponse()</code> function whenever the user clicks on the submit button:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> btn = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"btn"</span>)

btn.addEventListener(<span class="hljs-string">'click'</span>, getResponse)
</code></pre>
<p>The <code>getResponse</code> function essentially gets the user's question, sends it to our Node.js backend to fetch the answer, and displays the response on the page.</p>
<p>Inside the function, we start by accessing the prompt submitted by the user:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getResponse</span>(<span class="hljs-params"></span>) </span>{                  
  <span class="hljs-keyword">var</span> inputText = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"input"</span>).value           
  <span class="hljs-keyword">const</span> parentDiv = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"chat-area"</span>) 

  <span class="hljs-comment">// The remaining code goes inside this function</span>
}
</code></pre>
<p>If the value of the text area is empty, we simply return nothing:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(inputText === <span class="hljs-string">''</span>) { <span class="hljs-keyword">return</span> }
</code></pre>
<p>Otherwise, we first add the question to the chat area in the UI:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> question = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">'div'</span>)
question.innerHTML = inputText
question.classList.add(<span class="hljs-string">"box"</span>)
parentDiv.appendChild(question)
</code></pre>
<p>Next, we reset the text area so it's blank:</p>
<pre><code class="lang-javascript"><span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"input"</span>).value = <span class="hljs-string">''</span>
</code></pre>
<p>Then we send the question to our server so that the server can send it to the OpenAI API and send us back a response:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> res = <span class="hljs-keyword">await</span> fetch(<span class="hljs-string">'http://localhost:5000/chat'</span>, 
  {
    <span class="hljs-attr">method</span>: <span class="hljs-string">'POST'</span>,
    <span class="hljs-attr">headers</span>: {
      <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">'application/json'</span>                
    },
    <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
      <span class="hljs-attr">question</span>: inputText          
    })
  }
)

<span class="hljs-keyword">const</span> data = <span class="hljs-keyword">await</span> res.json()
</code></pre>
<p>If the response has a <code>message</code> property, we add the message content to the chat area in the UI:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">if</span>(data.message) {
  <span class="hljs-keyword">const</span> answer = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">'div'</span>)
  answer.innerHTML = data.message
  answer.classList.add(<span class="hljs-string">"box"</span>, <span class="hljs-string">"answer"</span>)
  parentDiv.appendChild(answer)
}
</code></pre>
<p>Now the frontend is all set. Let's move our focus back to the backend.</p>
<h2 id="heading-how-to-send-the-api-response-to-the-client"><strong>How to Send the API Response to the Client</strong></h2>
<p>Our backend will serve as the middleman between the frontend and OpenAI's API. Basically, we'll get the prompt from the client, send it to the API, and send the response back to the client.</p>
<p>In <strong>server.js</strong>, import these at the top of the file:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> { OpenAI } = <span class="hljs-built_in">require</span>(<span class="hljs-string">"openai"</span>)
</code></pre>
<p>Next, create an instance of the <code>openai</code> connection using the API key you generated earlier:</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> openai = <span class="hljs-keyword">new</span> OpenAI({
    <span class="hljs-comment">// replace your-api-key with your API key from ChatGPT</span>
    <span class="hljs-attr">apiKey</span>: <span class="hljs-string">'your-api-key'</span>
})
</code></pre>
<p>Finally, create the route:</p>
<pre><code class="lang-javascript">app.post(<span class="hljs-string">'/chat'</span>, <span class="hljs-keyword">async</span> (req, res)=&gt; {   
  <span class="hljs-keyword">try</span> {
    <span class="hljs-keyword">const</span> resp = <span class="hljs-keyword">await</span> openai.chat.completions.create({
      <span class="hljs-attr">model</span>: <span class="hljs-string">"gpt-3.5-turbo"</span>,
        <span class="hljs-attr">messages</span>: [
          { <span class="hljs-attr">role</span>: <span class="hljs-string">"user"</span>, <span class="hljs-attr">content</span>: req.body.question}
        ]  
    })           

    res.status(<span class="hljs-number">200</span>).json({<span class="hljs-attr">message</span>: resp.choices[<span class="hljs-number">0</span>].message.content})
  } <span class="hljs-keyword">catch</span>(e) {
      res.status(<span class="hljs-number">400</span>).json({<span class="hljs-attr">message</span>: e.message})
  }
})
</code></pre>
<p>Save the file changes, then go to your browser and submit a question. You should get back a response after a few seconds.</p>
<p>You can ask as many questions as you want, but you'll have to wait for a response to each question from the backend.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/07/Document.png" alt="Document" width="600" height="400" loading="lazy"></p>
<p><em>Screenshot of the questions and corresponding answers from ChatGPT</em></p>
<p>If any error is encountered, check the console in your browser to inspect the error message.</p>
<h2 id="heading-conclusion"><strong>Conclusion</strong></h2>
<p>OpenAI's API offers you a way to include AI-powered chatbots in your application using JavaScript or even <a target="_blank" href="https://letsusetech.com/the-awesome-things-you-can-do-with-htmx">HTMX</a> (if you're knowledgeable of HTML but not JavaScript).</p>
<p>Connect with me on <a target="_blank" href="https://twitter.com/kingchuuks">Twitter</a> and <a target="_blank" href="https://www.linkedin.com/in/kingchuks/">LinkedIn</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Create a 3D Tilting Card Effect in React Native ]]>
                </title>
                <description>
                    <![CDATA[ Recently, I came across a tutorial about how to create a 3D tilting card effect (with mouse tracking functionality) on Kevin Powell's YouTube channel. https://youtu.be/Z-3tPXf9a7M   Kevin used HTML, CSS, and JavaScript to create the markup, style the... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-create-a-3d-tilting-card-in-react-native/</link>
                <guid isPermaLink="false">66d4617e33b83c4378a51851</guid>
                
                    <category>
                        <![CDATA[ mobile app development ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 22 Feb 2023 16:35:15 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--14--1.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Recently, I came across a tutorial about how to create a 3D tilting card effect (with mouse tracking functionality) on Kevin Powell's <a target="_blank" href="https://www.youtube.com/@KevinPowell">YouTube channel</a>.</p>
<div class="embed-wrapper">
        <iframe width="560" height="315" src="https://www.youtube.com/embed/Z-3tPXf9a7M" style="aspect-ratio: 16 / 9; width: 100%; height: auto;" title="YouTube video player" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen="" loading="lazy"></iframe></div>
<p> </p>
<p>Kevin used HTML, CSS, and JavaScript to create the markup, style the card, and implement the mouse tracking functionality, respectively.</p>
<p>In this tutorial, we'll take a look at how to replicate the same project in React Native. We'll write pure HTML markup and use the <a target="_blank" href="https://www.npmjs.com/package/react-native-render-html">react-native-render-html</a> library to render it all into 100% native views.</p>
<p>This how our tilted cards will look by the end of this tutorial:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--20-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Feeling excited? Let's begin!</p>
<h2 id="heading-prerequisites"><strong>Prerequisites</strong></h2>
<p>To follow this tutorial, you'll need to have a good understanding of HTML. Knowledge of JavaScript and React Native is helpful but not necessary.</p>
<p>To follow this tutorial locally, on your computer, you need to have the following installed:</p>
<ul>
<li><p><a target="_blank" href="https://nodejs.org/en/download/">Node.js v10 or above</a></p>
</li>
<li><p><a target="_blank" href="https://developer.apple.com/xcode/">Xcode</a> or <a target="_blank" href="https://developer.android.com/studio">Android Studio</a> (set up to run the emulator)</p>
</li>
<li><p>React Native CLI</p>
</li>
</ul>
<p>For step-by-step instructions on how to set up your React Native development environment, you can read the <a target="_blank" href="https://reactnative.dev/docs/environment-setup">official documentation</a>.</p>
<p>Alternatively, if you don't have the time or resources to set it up from scratch, use <a target="_blank" href="https://snack.expo.dev/">Expo Snack</a>. This is a browser-based environment that lets you create React Native projects without needing to install anything.</p>
<p>Skip the <strong>Project Setup</strong> section if you're following this tutorial from Expo Snack. Otherwise proceed.</p>
<h2 id="heading-project-setup">Project Setup</h2>
<p>Run this command to install the React Native CLI tool using npm:</p>
<pre><code class="lang-c">npm install -g react-native-cli
</code></pre>
<p>Initialize a new project:</p>
<pre><code class="lang-c">react-native init my-app
</code></pre>
<p>Start your emulator:</p>
<pre><code class="lang-c">cd my-app &amp;&amp; npx react-native run-ios <span class="hljs-comment">// Start the iOS simulator</span>

<span class="hljs-comment">// OR</span>

cd my-app &amp;&amp; npx react-native run-android <span class="hljs-comment">// Android the android emulator</span>
</code></pre>
<p>These commands should have the simulator show up on the screen like this:</p>
<p><img src="https://paper-attachments.dropboxusercontent.com/s_8DBD080458BC82C38C5265C10E1A2396B6E318C95FC96C295C6A241E0096B1AD_1675941462527_emulator.png" alt="Image" width="160" height="314" loading="lazy"></p>
<p>Afterward, cd into your application folder on the terminal and run the following command to install <a target="_blank" href="https://www.npmjs.com/package/react-native-render-html">React Native HTML Renderer</a>:</p>
<pre><code class="lang-c">npm i react-native-render-html
</code></pre>
<p>That's all for installations. Let's get started with the code.</p>
<h2 id="heading-how-to-set-up-the-app">How to Set Up the App</h2>
<p>In this section, we'll import the React, <code>useWindowDimensions</code>, and <code>RenderHtml</code>, which we'll use create the layout of our application.</p>
<p>Open the App.js file in your project and clear the file content. Then start by importing the following:</p>
<pre><code class="lang-c"><span class="hljs-keyword">import</span> * as React from <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { useWindowDimensions } from <span class="hljs-string">'react-native'</span>;
<span class="hljs-keyword">import</span> RenderHtml from <span class="hljs-string">'react-native-render-html'</span>;
</code></pre>
<p>The <code>useWindowDimentions</code> hook allow us to access the dimensions of the viewport, which is how we're going to make the content responsive across all screen sizes. We'll use <code>RenderHtml</code> to render pure HTML markup into native views in our React Native application.</p>
<p>If you're following this tutorial on Expo Snack, you'll be asked to add the <code>react-native-render-html</code> package by clicking on <strong>Add dependency</strong> link at the page bottom. Click it.</p>
<p>Next, include the following code beneath the imports:</p>
<pre><code class="lang-c"><span class="hljs-keyword">const</span> source = {
  html: `
&lt;pre class=<span class="hljs-string">"language-css"</span> tabindex=<span class="hljs-string">"0"</span>&gt;&lt;code class=<span class="hljs-string">"language-css"</span>&gt;&lt;span class=<span class="hljs-string">"token selector"</span>&gt;.awesome-layouts&lt;/span&gt; &lt;span class=<span class="hljs-string">"token punctuation"</span>&gt;{&lt;/span&gt;
  &lt;span class=<span class="hljs-string">"token property"</span>&gt;display:&lt;/span&gt;&lt;/span&gt; &lt;span&gt;grid;&lt;/span&gt;
&lt;span <span class="hljs-class"><span class="hljs-keyword">class</span>="<span class="hljs-title">token</span> <span class="hljs-title">punctuation</span>"&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;`
};</span>
</code></pre>
<p>As a value of the <code>html</code> property, we pass the HTML markup for the code block. It's pre-formatted content, so don't format the code (or it'll break). Just copy the code as it is and paste in your file.</p>
<p>Next, create <code>App()</code> as a functional component and a base stylesheet for the application. Return <code>&lt;RenderHTML /&gt;</code> and pass in both the pre-formatted code and stylesheet to the <code>source</code> and <code>baseStyles</code>, respectively:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> function <span class="hljs-title">App</span><span class="hljs-params">()</span> </span>{  
  <span class="hljs-keyword">const</span> { height, width } = useWindowDimensions();

  <span class="hljs-keyword">const</span> base = {
    height: height,
    width: width,
    flex: <span class="hljs-number">1</span>,
    alignItems: <span class="hljs-string">'center'</span>,
    justifyContent: <span class="hljs-string">'center'</span>,
    backgroundColor: <span class="hljs-string">'hsl(224, 32%, 12%)'</span>, 
    color: <span class="hljs-string">'white'</span>
  }

  <span class="hljs-keyword">return</span> (  
      &lt;RenderHtml      
      contentWidth={width}
      source={source}      
      baseStyle={base}      
      /&gt;
  );
}
</code></pre>
<p>On line 2, we access the height and width of the viewing device from the <code>useWindowsDimensions()</code> call. We then set the height and width of the root element to that of the screen, align the content to the center, set the background color to dark-blue, and the text color to white.</p>
<p>Save the file. You should find a UI that looks like the image below:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--15-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next up is styling the code block.</p>
<h2 id="heading-how-to-style-the-code">How to Style the <code>&lt;pre&gt;</code> Code</h2>
<p>In this section, we'll place the pre-formatted code inside a card, tilt the card, and apply different colors to the code to make it appear like real CSS code.</p>
<h3 id="heading-1-how-to-put-the-code-in-a-card">1. How to put the code in a card</h3>
<p>All of the stylesheets for our HTML markup will go inside the <code>htmlStyles</code> object. The first property of <code>htmlStyles</code> is the stylesheet for the <code>&lt;pre&gt;</code> element:</p>
<pre><code class="lang-c"><span class="hljs-keyword">const</span> htmlStyles = { 
  pre: {
    fontSize: <span class="hljs-string">'18px'</span>,
    fontWeight: <span class="hljs-string">'bold'</span>,
    backgroundColor: <span class="hljs-string">'hsl(222, 45%, 7%)'</span>,
    padding: <span class="hljs-string">'10px'</span>,
    borderRadius: <span class="hljs-string">'5px'</span>,    
    transform: [
      { perspective: <span class="hljs-number">5000</span> },
      { rotateX: <span class="hljs-string">'8deg'</span>},
      { rotateY: <span class="hljs-string">'20deg'</span>},
      { skewX: <span class="hljs-string">'8deg'</span>},
    ],
  },  
}
</code></pre>
<p>This adds a dark-blue background around the code, rounds the box edges, increases the font size and weight of the code, and adds some padding between the box and the code.</p>
<p>With <code>transform</code>, we rotate the card 8 degree around the x-axis and 20 degrees along the y-axis, tilting the card a little bit.</p>
<p>We also set perspective to 5000 to set a significant distance between the user and the card (which makes it more realistic).</p>
<p>To see this in effect, you need to add the <code>tagsStyles</code> property to <code>&lt;RenderHTML /&gt;</code> and pass <code>htmlStyles</code> as its value, like so:</p>
<pre><code class="lang-c">&lt;RenderHtml      
  contentWidth={width}
  source={source}      
  baseStyle={base}
  tagsStyles={htmlStyles}      
/&gt;
</code></pre>
<p>Save the file and go over to your device/emulator to see the effect:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--16-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we style the text by adding different colors to it.</p>
<h3 id="heading-2-how-to-style-the-code">2. How to style the code</h3>
<p>We don't want all of the code to be white. Instead, we want the <code>.selector</code> to be pink, the <code>.property</code> greenish-blue and the rest white. We'll add some extra CSS to get these colors.</p>
<p>Recall that each <code>&lt;span&gt;</code> element within <code>&lt;pre&gt;</code> had a class name of either <code>.selector</code>, <code>.property</code> or <code>.punctuation</code>. With react-native-render-html, we can't use the class selector to target elements, so this will not work:</p>
<pre><code class="lang-c">.selector {
    color: <span class="hljs-string">'hsl(338, 70%, 65%)'</span>
}
</code></pre>
<p>We'll have to define the CSS code directly on the respective HTML tags with the inline <code>style</code> property. So for the <code>&lt;span&gt;</code> element with the class name <code>selector</code>, apply the pink color like so:</p>
<pre><code class="lang-c">style=<span class="hljs-string">"color: hsl(338, 70%, 65%);</span>
</code></pre>
<p>As for the <code>&lt;span&gt;</code> element with class name of <code>property</code>, apply a greenish-blue color to it:</p>
<pre><code class="lang-c">style=<span class="hljs-string">"color: hsl(183, 70%, 62%);</span>
</code></pre>
<p>Next, we'll add some glow to the text. To do this, we'll use the React Native versions of the <code>text-shadow</code> property and we'll apply it to the <code>&lt;span&gt;</code> elements (add inside the <code>htmlStyles</code> object):</p>
<pre><code class="lang-c"> span: {
    textShadowColor: <span class="hljs-string">'currentColor'</span>,
    textShadowOffset: {width: <span class="hljs-number">-0.5</span>, height: <span class="hljs-number">0.5</span>},
    textShadowRadius: <span class="hljs-number">4</span>
  },
</code></pre>
<p>By setting <code>textShadowColor</code> to <code>currentColor</code>, we're telling React Native to use the same color of each text as shadow color, so the selector will have a pink glow and the property will have a greenish-blue glow.</p>
<p>Here's the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--17-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now we're done with styling the code. In the next section, we'll turn the card into a 3D element.</p>
<h2 id="heading-how-to-make-the-card-3d-and-add-different-layers">How to Make the Card 3D and Add Different Layers</h2>
<p>In this section, we'll add a second layer to the card and transform the card into a 3D object. The second box will be an image gradient containing the colors blue and red.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/angryimg.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the <code>source</code> object, underneath the <code>&lt;pre /&gt;</code> tag, create an <code>&lt;img&gt;</code> element and point its <code>src</code> to the image above (upload the image to a place like Dropbox and get the URL):</p>
<pre><code class="lang-c">&lt;img src=<span class="hljs-string">"put-the-url-to-your-image-here"</span>/&gt;
</code></pre>
<p>Next, add the following stylesheet to the <code>htmlStyles</code> object:</p>
<pre><code class="lang-c">  img: {    
    top: <span class="hljs-number">-95</span>,
    right: <span class="hljs-number">7</span>, 
    zIndex: <span class="hljs-number">-10</span>,   
    height: <span class="hljs-number">95</span>,
    width: <span class="hljs-number">216</span>,    
    borderRadius: <span class="hljs-string">'5px'</span>, 
    overflow: <span class="hljs-string">'hidden'</span>,   
   transform: [
      { perspective: <span class="hljs-number">5000</span> },
      { rotateX: <span class="hljs-string">'8deg'</span>},
      { rotateY: <span class="hljs-string">'20deg'</span>},
      { skewX: <span class="hljs-string">'8deg'</span>},
    ],
  }
</code></pre>
<p>The above stylesheet basically overlaps the image with the code block (top and right), places the image behind the code block (z-index), makes the image the same size as the code block (height and width) and the same shape as well (transform).</p>
<p>Here's the final look of our application:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--18-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>To straighten the button, you simply reset the value of <code>rotateX</code> and <code>rotateY</code> to 0 for both the <code>&lt;img&gt;</code> and the <code>&lt;pre&gt;</code> element:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/02/Untitled-design--19-.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Grab the full source code from <a target="_blank" href="https://snack.expo.dev/@ubahthebuilder/69e153">here</a>.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>In this tutorial, we took a tilted 3D card made with HTML and CSS and replicated it in React Native.</p>
<p>To achieve this, we used react-native-html-renderer to render HTML to React Native elements, and React Native specific properties to style the elements.</p>
<p>I hope you enjoyed following this tutorial as I did making it. You can give feedback on this article or discuss opportunities with me on <a target="_blank" href="https://twitter.com/kingchuuks">Twitter</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Native Tutorial – How to Create a Simple Responsive Layout for Beginners ]]>
                </title>
                <description>
                    <![CDATA[ Having a responsive layout is an important component of user interface (UI) design. It enables a website or application to automatically adjust its size and layout based on the size of the user's device and screen. This provides an optimal viewing ex... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/responsive-layout-react-native/</link>
                <guid isPermaLink="false">66d4619837bd2215d1e24608</guid>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ responsive design ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Thu, 02 Feb 2023 23:50:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/pexels-luna-lovegood-4087468.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Having a responsive layout is an important component of user interface (UI) design. It enables a website or application to automatically adjust its size and layout based on the size of the user's device and screen. This provides an optimal viewing experience.</p>
<p>Responsive layouts also give the user a more consistent and unified look across all devices and platforms, while still making sure the user's experience is tailored to their device.</p>
<p>This helps create a more user-friendly experience and improves the overall usability of the website or application.</p>
<p>In this tutorial, I'll show you how to create a simple responsive layout in React Native. You can preview the demo <a target="_blank" href="https://snack.expo.dev/@ubahthebuilder/67d71a">here</a>.</p>
<h2 id="heading-requirements">Requirements</h2>
<p>To follow this tutorial, you'll need:</p>
<ul>
<li><p>A basic understanding of React Native</p>
</li>
<li><p><a target="_blank" href="https://snack.expo.dev/">Expo Snack</a> – a browser-based development environment for React Native</p>
</li>
</ul>
<h2 id="heading-how-to-create-a-responsive-layout">How to Create a Responsive Layout</h2>
<p>Go to Expo Snack and clear the content of App.js. Start by importing the React library and Text, View, and Stylesheet UI components:</p>
<pre><code class="lang-c"><span class="hljs-keyword">import</span> * as React from <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Text, View, StyleSheet } from <span class="hljs-string">'react-native'</span>;
</code></pre>
<p>React Native is based on React, so we need to import the React library explicitly. The Text component is used to create text, View is a container element used for grouping other elements, and StyleSheet is used to define the components stylesheet.</p>
<p>Next, define an App function and return two custom components, <code>&lt;Header /&gt;</code> and <code>&lt;Footer /&gt;</code>:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> function <span class="hljs-title">App</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">return</span> (
    &lt;View style={styles.container}&gt;
      &lt;Header /&gt;
      &lt;Boxes /&gt;
    &lt;/View&gt;
  );
}
</code></pre>
<p>Here we wrap <code>&lt;Header /&gt;</code> and <code>&lt;Footer /&gt;</code> inside a <code>&lt;View /&gt;</code> container. Both of them are custom components, and because they haven't been created yet, you'll get a ReferenceError. Ignore that for now.</p>
<p>Next, create the stylesheet for this component and define the styling for the container element:</p>
<pre><code class="lang-c"><span class="hljs-keyword">const</span> styles = StyleSheet.create({
  container: {
    flex: <span class="hljs-number">1</span>,   
  },  
});
</code></pre>
<p>This <code>flex:1</code> value tells the component to fill all available space and to share the space evenly amongst its children.</p>
<p>Let's now create both the <code>&lt;Header&gt;</code> and <code>&lt;Boxes/&gt;</code> components.</p>
<h2 id="heading-how-to-create-the-component">How to Create the <code>&lt;Header /&gt;</code> Component</h2>
<p>In the components folder, create a new file named Header.js. Then import both the following:</p>
<pre><code class="lang-c"><span class="hljs-keyword">import</span> * as React from <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Text, View, StyleSheet} from <span class="hljs-string">'react-native'</span>;
</code></pre>
<p>Create a <code>&lt;Header /&gt;</code> function and return a single <code>&lt;View /&gt;</code> with text saying "Header Component":</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> function <span class="hljs-title">Header</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">return</span> (
    &lt;View style={styles.header}&gt;
      &lt;Text&gt;Header Component&lt;/Text&gt;
    &lt;/View&gt;
  );
}
</code></pre>
<p>The <code>&lt;View /&gt;</code> is linked to a <code>style</code> property of <code>header</code>. Keep in mind that we want to make this header responsive on all screen sizes. Let's do that using CSS.</p>
<pre><code class="lang-c"><span class="hljs-keyword">const</span> styles = StyleSheet.create({
  header: {
    width: <span class="hljs-string">"100%"</span>,
    height: <span class="hljs-string">"15%"</span>,
    alignItems: <span class="hljs-string">'center'</span>,
    justifyContent: <span class="hljs-string">'center'</span>,
    backgroundColor: <span class="hljs-string">'grey'</span>    
  },
});
</code></pre>
<p>With this stylesheet, we're specifying that, no matter how big or small the screen, the header should span the whole width of the screen (100%) and take only 15% of the screen's height.</p>
<p>Now to the boxes.</p>
<h2 id="heading-how-to-create-the-component-1">How to Create the <code>&lt;Boxes /&gt;</code> Component</h2>
<p>Below the header we want to place two boxes, one to the left and one to the right. We also want the boxes to take up a certain width and height based on the screen dimension of the target device.</p>
<p>Inside the components folder, create a new file named boxes.js. Start by importing the following:</p>
<pre><code class="lang-c"><span class="hljs-keyword">import</span> * as React from <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Text, View, StyleSheet} from <span class="hljs-string">'react-native'</span>;
</code></pre>
<p>Next, create a Boxes function and return the markup for the boxes:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> function <span class="hljs-title">Boxes</span><span class="hljs-params">()</span> </span>{
  <span class="hljs-keyword">return</span> (
    &lt;View style={styles.container}&gt;
      &lt;View style={styles.box}&gt;
        &lt;View style={styles.inner}&gt;
          &lt;Text&gt;Box <span class="hljs-number">1</span>&lt;/Text&gt;
        &lt;/View&gt;                      
      &lt;/View&gt;
      &lt;View style={styles.box}&gt;
        &lt;View style={styles.inner}&gt;
          &lt;Text&gt;Box <span class="hljs-number">2</span>&lt;/Text&gt;
        &lt;/View&gt;                      
      &lt;/View&gt;
    &lt;/View&gt;
  );
}
</code></pre>
<p>Here we have two boxes placed inside the container. Inside both is a View element containing the text label for each box.</p>
<p>Next, define the styling for the component. We'll create three sets of styles, one for the box container, one for the inner wrapper and the last for the boxes themselves.</p>
<pre><code class="lang-c"><span class="hljs-keyword">const</span> styles = StyleSheet.create({
  container: {
    width: <span class="hljs-string">"100%"</span>,
    height: <span class="hljs-string">"85%"</span>,
    alignItems: <span class="hljs-string">"center"</span>,
    flexDirection: <span class="hljs-string">'row'</span>,
    flexWrap: <span class="hljs-string">'wrap'</span>    
  },
  box: {
    width: <span class="hljs-string">"50%"</span>,
    height: <span class="hljs-string">"50%"</span>,
    padding: <span class="hljs-number">5</span>
  },
  inner: {
    flex: <span class="hljs-number">1</span>,
    backgroundColor: <span class="hljs-string">"#D3D3D3"</span>,
    alignItems: <span class="hljs-string">'center'</span>,
    justifyContent: <span class="hljs-string">'center'</span>
  }
});
</code></pre>
<p>Recall that the header takes up 15% of the screen's height. Therefore, with the above CSS, we specify that the box container should take up the remaining 85%.</p>
<p>We align the boxes to the center and place them in a row arrangement. Each box takes up 50% of the total height and width, and we apply padding of 5 to space them a bit.</p>
<p>Now that we have both the responsive Header and Boxes ready, let's import them to App.js and see the results.</p>
<h2 id="heading-how-to-import-and">How to Import <code>&lt;Header /&gt;</code> and <code>&lt;Boxes /&gt;</code></h2>
<p>Inside App.js, at the top of the file, import both <code>&lt;Header /&gt;</code> and <code>&lt;Boxes /&gt;</code> like so:</p>
<pre><code class="lang-c"><span class="hljs-keyword">import</span> Header from <span class="hljs-string">"./components/Header"</span>
<span class="hljs-keyword">import</span> Boxes from <span class="hljs-string">"./components/Boxes"</span>
</code></pre>
<p>Once the app compiles, you should see your responsive layout on the right side of your window looking like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/UI.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look of responsive layout</em></p>
<p>This arrangement will be consistent across all screen sizes because we used percentages to set the height and width instead of fixed width values.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>I hope this tutorial helped you understand React Native positioning better. You should now be able to create responsive layouts in your React Native app using the techniques we covered in this post.</p>
<p>Grab my FREE freelance writing checklist <a target="_blank" href="https://kingchuks.gumroad.com/l/fwc">here</a>. Have a great week!</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React.js vs React Native – What's the Difference? ]]>
                </title>
                <description>
                    <![CDATA[ Are React.js and React Native the same? If you're new to the world of web and mobile development, you might be wondering the same thing. As a newbie, it's easy to assume that React.js are React Native are the same. After all, they both have "React" a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-js-vs-react-native-whats-the-difference/</link>
                <guid isPermaLink="false">66d46192a326133d12440a9e</guid>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 01 Feb 2023 00:41:21 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/02/pexels-pixabay-209339.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Are React.js and React Native the same?</p>
<p>If you're new to the world of web and mobile development, you might be wondering the same thing.</p>
<p>As a newbie, it's easy to assume that React.js are React Native are the same. After all, they both have "React" as part of their name.</p>
<p>Though React.js and React Native share a lot in common, they're different from each other. In this article, I'll explain both React.js and React Native, then I'll list out their similarities and differences. By the end of this article, you'll have a clear knowledge of both tools and what kind of app they're used to build.</p>
<p>In order to have a clear understanding of the difference between React.js and React Native, we have to first dive into how a website is rendered on a web browser.</p>
<h2 id="heading-how-websites-get-rendered-html-css-and-javascript">How Websites Get Rendered: HTML, CSS, and JavaScript</h2>
<p>When you type the URL of a website into your browser's address bar and click enter, the browser requests the website, and the web server sends an HTML file to the browser.</p>
<p>The HTML file contains the content of the webpage and the linked files such as images, videos, and stylesheets. The web browser parses the HTML file and builds a Document Object Model (DOM) which is a tree-like structure containing the elements of the page (for example, buttons, paragraphs, links, and so on).</p>
<p>The browser initiates requests for the linked files and downloads them to the computer. It then parses the linked files, such as CSS and JavaScript, and applies the styling to the content, making it more presentable to the user. After all the files are downloaded, the browser renders the content on the screen.</p>
<p>The browser also runs any JavaScript code to make the page interactive. So, for example, if the user fills the wrong information in a form, JavaScript can be used to insert a <code>&lt;div&gt;</code> element into the page showing an error message to the user.</p>
<p>However, one of the biggest problems of inserting elements into the DOM with JavaScript is that the code is not reusable. For example, if you want to insert the same button into the page, but with different background colors, you have to create the element twice in JavaScript:</p>
<pre><code class="lang-c">let blueBtn = document.createElement(<span class="hljs-string">"button"</span>).style.backgroundColor(<span class="hljs-string">"blue"</span>)
let redBtn = document.createElement(<span class="hljs-string">"button"</span>).style.backgroundColor(<span class="hljs-string">"red"</span>)

<span class="hljs-comment">// Insert blue and red button into the page</span>
</code></pre>
<p>This is just a simple example. With complex user interfaces, you can imagine how long and confusing things can become. React was developed to solve this problem by making the process of creating web apps much more organized and intuitive.</p>
<h2 id="heading-what-is-reactjs">What is React.js?</h2>
<p>Technically speaking, ReactJS is an open-source, front-end JavaScript library for building user interfaces or UI components. In simple terms, this means that you can use React to build all the parts of a website that the user can see and interact with on their browser window.</p>
<p>So what's the difference between using plain JavaScript and React? Well, React makes the process of designing the user interface much easier. It allows you to create elements that you can easily re-use in other parts of the website or app.</p>
<p>With JavaScript, I previously mentioned how you'll need to write the same code twice to create the same button with different colors, which could lead to complexity in large projects.</p>
<p>React's component architecture solves this problem brilliantly. With React, you define a single piece of the UI, say a button, as a component.</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">const</span> <span class="hljs-title">Button</span> <span class="hljs-params">(props)</span> </span>=&gt; {
    <span class="hljs-keyword">return</span> (
        &lt;div&gt;
            &lt;button style={props.color}&gt;Submit&lt;/button&gt;
        &lt;/div&gt;
    )
}
</code></pre>
<p>The component in this case is a function that returns HTML-like syntax called JSX, which defines the presentation and look of the component on the web browser.</p>
<p>Now, say you want to use the same button (but with different colors) in multiple places on your website. Instead of creating each button from scratch with different color properties (as you would with JavaScript), with React, you simple use the same <code>&lt;Button&gt;</code> element and pass a different color to each of them as <code>props</code>, creating variations of the same button.</p>
<pre><code class="lang-c">&lt;Button color=<span class="hljs-string">"red"</span> /&gt;
&lt;Button color=<span class="hljs-string">"blue"</span> /&gt;
&lt;Button color=<span class="hljs-string">"green"</span> /&gt;
</code></pre>
<p>This method keeps everything simple and organized, which is the whole essence of the React.js library.</p>
<p>Another benefit of using React for UI development is separation of concerns. This means that the data used in a component exists separately from the logic, which exists separately from the view layer.</p>
<p>Here's an example:</p>
<pre><code class="lang-c"><span class="hljs-function"><span class="hljs-keyword">const</span> <span class="hljs-title">Button</span> <span class="hljs-params">(props)</span> </span>=&gt; {
    <span class="hljs-comment">// component data</span>
    <span class="hljs-keyword">const</span> [btnText, setBtnText] = useState(<span class="hljs-string">"Submit"</span>)

    <span class="hljs-comment">// component logic</span>
    function onClick() {
        setBtnText(<span class="hljs-string">"Submitted!"</span>)
    }

    <span class="hljs-keyword">return</span> (
        <span class="hljs-comment">// component view</span>
        &lt;div&gt;
            &lt;button style={props.color}&gt;{btnText}&lt;/button&gt;
        &lt;/div&gt;
    )
}
</code></pre>
<p>As you can see here, the state, logic and presentation of a component are all separate from each other, making React UI components easier to understand and compose.</p>
<p>To conclude, React is a JavaScript library designed to simplify the process to building the frontend of web applications.</p>
<h2 id="heading-what-is-react-native">What is React Native?</h2>
<p>Here's the main difference between ReactJS and React Native:</p>
<ul>
<li><p>React JS is used to build the user interface of web applications (that is, apps that run on a web browser)</p>
</li>
<li><p>React Native is used to build applications that run on both iOS and Android devices (that is, cross-platform mobile applications)</p>
</li>
<li><p>React uses HTML, CSS and JavaScript to create interactive user interfaces. React Native, on the other hand, uses native UI components and APIs to create mobile apps.</p>
</li>
</ul>
<p>Both React JS and React Native share the same syntax. React Native was created as a way for developers to build cross-platform mobile applications using their existing knowledge of web development tools like HTML, CSS, JavaScript and the React core library.</p>
<p>In fact, some of the libraries commonly used together with React to develop web applications also have a mobile version for building apps in React Native – for example, Axios, Bootstrap CSS, and Tailwind CSS.</p>
<p>Here are the things React DOM and React Native have in common:</p>
<ol>
<li><p>They both use the same core React library.</p>
</li>
<li><p>They both use the same component-based architecture, which means that developers can break down their applications into smaller, more manageable pieces.</p>
</li>
<li><p>They both use JavaScript as their programming language, and JSX as their templating language.</p>
</li>
<li><p>Both React DOM and React Native use virtual DOMs to render their applications.</p>
</li>
<li><p>Both React DOM and React Native also use the same styling techniques and components, through React Native's is a bit different.</p>
</li>
<li><p>They both utilize Chrome DevTools for debugging applications.</p>
</li>
<li><p>They use the same JavaScript APIs.</p>
</li>
<li><p>Both were developed in Meta. React was developed by a software engineer named Jordan Walke while React Native was born from a hackathon.</p>
</li>
</ol>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This article explored the differences between React DOM and React Native, two popular JavaScript tools. React DOM is primarily used for web development, while React Native is used for mobile development.</p>
<p>React DOM uses HTML, CSS, and JavaScript for layout and styling, and allows developers to create interactive user interfaces. React Native, on the other hand, uses native UI components and APIs to create cross-platform mobile applications.</p>
<p>Thanks for reading. Grab my FREE freelance writing checklist <a target="_blank" href="https://kingchuks.gumroad.com/l/fwc">here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Native Networking – How To Perform API Requests In React Native using the FetchAPI ]]>
                </title>
                <description>
                    <![CDATA[ APIs, or application program interfaces, are essential mechanisms for businesses in all industries. They allow for a secure exchange of data between two different systems, such as a web application and a database. Think of when you are using a mobile... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-native-networking-api-requests-using-fetchapi/</link>
                <guid isPermaLink="false">66d46194d7a4e35e384349c9</guid>
                
                    <category>
                        <![CDATA[ api ]]>
                    </category>
                
                    <category>
                        <![CDATA[ computer networking ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React Native ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Fri, 20 Jan 2023 19:35:24 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2023/01/pexels-pixabay-276502--1-.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>APIs, or application program interfaces, are essential mechanisms for businesses in all industries. They allow for a secure exchange of data between two different systems, such as a web application and a database.</p>
<p>Think of when you are using a mobile app to order food from a restaurant. The restaurant's menu, pricing, and ordering information could all be stored in a database that is managed by a back-end application.</p>
<p>To enable the mobile app to access the data, the app must make an API request to the back-end application. The request will include information such as the restaurant's location, the menu items, and the desired order.</p>
<p>The back-end application will then respond with the requested information. The mobile app can then use the data to create a user-friendly interface for ordering food.</p>
<p>API requests can also be used to update the database with new data, providing a way for the application to save and store new information. You can also set up with in-app subscription events. For example, when a user's subscription is about to expire, an API request is sent to a notification system to alert the them.</p>
<p>In this tutorial, you’ll learn how to make GET, POST, PUT and DELETE requests to APIs in a React Native app using the FetchAPI. You can access the full code from this tutorial <a target="_blank" href="https://snack.expo.dev/@ubahthebuilder/b61a85">here</a>.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>To follow this tutorial, you’ll need just two things:</p>
<ul>
<li><p>A basic understanding of React Native</p>
</li>
<li><p><a target="_blank" href="https://snack.expo.dev/">Expo Snack</a></p>
</li>
</ul>
<p>Expo Snack is an online development environment for React Native which essentially lets you run React Native apps on your web browser. This removes the hassle of setting up your local React Native environment from scratch.</p>
<h2 id="heading-setting-up-the-app">Setting up the app</h2>
<p>Go to <a target="_blank" href="https://snack.expo.dev/">Expo Snack</a> to initialize a new React Native project, then go to the App.js file and clear the file’s content.</p>
<p>Start by importing React, useState, and useEffect hooks from React as well as the Text, View, and StyleSheet components from React Native.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React,{useState, useEffect} <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { Text, View, StyleSheet } <span class="hljs-keyword">from</span> <span class="hljs-string">'react-native'</span>;
</code></pre>
<p>Next, define an App component function. Inside the function body, we set the data state to an empty array, the loading state to true, then we return a simple “Hello World” text for now.</p>
<pre><code class="lang-js"><span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> [data, setData] = useState([]);
  <span class="hljs-keyword">const</span> [loading, setLoading] = useState(<span class="hljs-literal">true</span>);
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span>Hello World<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
  );
}
</code></pre>
<p>Later on, when we retrieve data from JSONPlaceholder API (see next section), we’ll populate out the data state with it and render the data from the App component as text.</p>
<h2 id="heading-the-api-well-be-working-with">The API we’ll be working with</h2>
<p>The API we’ll be fetching our data from is <a target="_blank" href="https://jsonplaceholder.typicode.com/">JSONPlaceholder.</a> This is a free fake API for testing and prototyping purposes.</p>
<p>The API comes with 6 common resources which you can read, edit, update or delete by making API requests.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/https___jsonplaceholder.typicode.com_posts-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Array of 100 post objects</em></p>
<p>So here each post is an object with four properties: userId, id, title and body. There are 100 objects in the array.</p>
<p>You can:</p>
<ul>
<li><p>create a new post by making a POST request to this API (that is, add a new object)</p>
</li>
<li><p>read a specific post by making a GET request to this API (that is, read an object)</p>
</li>
<li><p>update an existing post by making a PUT request to this API (that is, modify an object)</p>
</li>
<li><p>delete an existing post by making a DELETE request to this API (that is, remove an object)</p>
</li>
</ul>
<p>Let’s start with making GET requests in our React Native app.</p>
<h2 id="heading-how-to-make-a-get-request-in-react-native">How to Make a GET Request in React Native</h2>
<p>An API GET request is a type of API request used to retrieve data from a server. The request is sent via a HTTP GET method and data is returned in the form of a JSON or XML object.</p>
<p>Let’s make a GET request in our React Native app. First, store the URL you want to make the request to inside a variable (paste this code under state variable declaration):</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> url = <span class="hljs-string">"https://jsonplaceholder.typicode.com/posts"</span>
</code></pre>
<p>Next, use the fetch method to execute the API request to the URL. Wrap the fetch inside of useEffect (this hook allows us to perform side effects in our code, for example API calls):</p>
<pre><code class="lang-js">useEffect(<span class="hljs-function">() =&gt;</span> {
  fetch(url)
    .then(<span class="hljs-function">(<span class="hljs-params">resp</span>) =&gt;</span> resp.json())
    .then(<span class="hljs-function">(<span class="hljs-params">json</span>) =&gt;</span> setData(json))
    .catch(<span class="hljs-function">(<span class="hljs-params">error</span>) =&gt;</span> <span class="hljs-built_in">console</span>.error(error))
    .finally(<span class="hljs-function">() =&gt;</span> setLoading(<span class="hljs-literal">false</span>));
}, []);
</code></pre>
<p>So what’s happening here inside <code>useEffect</code>? First we make a GET call to the URL. Once the data is returned, we parse it to JSON with <code>resp.json()</code>, and in the next <code>then</code> block we call <code>setData</code> to update the state with the post.</p>
<p>In the event of any error, the <code>catch</code> method will run (which logs the error to the console). Finally, we set the loading state to <code>false</code>.</p>
<p>Finally, we render the post gotten from the API:</p>
<pre><code class="lang-js">&lt;View style={styles.container}&gt;
  {loading ? (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span>Loading...<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span></span>
  ) : (
    data.map(<span class="hljs-function">(<span class="hljs-params">post</span>) =&gt;</span> {
      <span class="hljs-keyword">return</span> (
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">View</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Text</span> <span class="hljs-attr">style</span>=<span class="hljs-string">{styles.title}</span>&gt;</span>{post.title}<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Text</span>&gt;</span>{post.body}<span class="hljs-tag">&lt;/<span class="hljs-name">Text</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">View</span>&gt;</span></span>
      );
    })
  )}
&lt;/View&gt;;
</code></pre>
<p>So if the API request is still in progress, we show the “Loading” text on our app. Once the data is retrieved, we loop through the array and render the title and text of each post.</p>
<p>To style it up a bit, paste the following stylesheet underneath App:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> styles = StyleSheet.create({
  <span class="hljs-attr">container</span>: {
    <span class="hljs-attr">flex</span>: <span class="hljs-number">1</span>,
    <span class="hljs-attr">justifyContent</span>: <span class="hljs-string">"center"</span>,
    <span class="hljs-attr">backgroundColor</span>: <span class="hljs-string">"#ecf0f1"</span>,
    <span class="hljs-attr">padding</span>: <span class="hljs-number">8</span>,
  },
  <span class="hljs-attr">title</span>: {
    <span class="hljs-attr">fontSize</span>: <span class="hljs-number">30</span>,
    <span class="hljs-attr">fontWeight</span>: <span class="hljs-string">"bold"</span>,
  },
});
</code></pre>
<p>Here’s the result when the posts are returned from the API.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2023/01/vigorous-carrot---Snack.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look</em></p>
<p>As you can see, we successfully fetched the list of posts from the API and rendered each of them in our React Native app.</p>
<p>Now if you want to retrieve a specific resource (like a specific post) as opposed to a collection of resources like a list of posts, all you need to do is add the resource ID to the URL like so:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> url = <span class="hljs-string">"https://jsonplaceholder.typicode.com/posts/1"</span>
</code></pre>
<p>If we make a GET request to the above API endpoint, the server will only gives us back the post with an ID of 1.</p>
<p>Let’s move on to other types of requests.</p>
<h2 id="heading-how-to-make-a-post-request-in-react-native">How to Make a POST Request in React Native</h2>
<p>An API POST request is a type of API request that is used to create or update a resource on a web server.</p>
<p>It sends data to the server in the form of a request body which usually contains information such as the title, description, and other relevant details. If the data is accepted, the server responds with a success code and the resource is created or updated.</p>
<p>When making a POST request with the FetchAPI, you must specify the method as ‘POST’. Here’s an example of a POST to the fake server from our React Native app:</p>
<pre><code class="lang-js">fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts"</span>, {
  <span class="hljs-attr">method</span>: <span class="hljs-string">"POST"</span>,
  <span class="hljs-attr">headers</span>: {
    <span class="hljs-attr">Accept</span>: <span class="hljs-string">"application/json"</span>,
    <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span>,
  },
  <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
    <span class="hljs-attr">userId</span>: <span class="hljs-number">55</span>,
    <span class="hljs-attr">id</span>: <span class="hljs-number">101</span>,
    <span class="hljs-attr">title</span>: <span class="hljs-string">"Post title"</span>,
    <span class="hljs-attr">body</span>: <span class="hljs-string">"Post body"</span>,
  }),
})
  .then(<span class="hljs-function">(<span class="hljs-params">response</span>) =&gt;</span> response.json())
  .then(<span class="hljs-function">(<span class="hljs-params">responseData</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(responseData));
  })
  .done();
</code></pre>
<p>The value of the body property must always be a JSON object with JSON string values (hence the <code>JSON.stringify</code> call).</p>
<p>Once the server is done processing the request, it sends back a response to tell you if the resource was created on the server or not (and why it failed).</p>
<h2 id="heading-how-to-make-a-put-request-in-react-native">How to Make a PUT Request in React Native</h2>
<p>If a POST request is used to create a new resource on a server, a PUT request is used to update a specific resource on that server.</p>
<p>In a PUT request, you need to specify the ID of the resource you want to update on the server as well as the new values. Here’s an example which updates the title and body of post one on the server:</p>
<pre><code class="lang-js">fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts/1"</span>, {
  <span class="hljs-attr">method</span>: <span class="hljs-string">"PUT"</span>,
  <span class="hljs-attr">body</span>: <span class="hljs-built_in">JSON</span>.stringify({
    <span class="hljs-attr">userId</span>: <span class="hljs-number">55</span>,
    <span class="hljs-attr">id</span>: <span class="hljs-number">101</span>,
    <span class="hljs-attr">title</span>: <span class="hljs-string">"New Post title"</span>,
    <span class="hljs-attr">body</span>: <span class="hljs-string">"New Post body"</span>,
  }),
})
  .then(<span class="hljs-function">(<span class="hljs-params">response</span>) =&gt;</span> response.json())
  .then(<span class="hljs-function">(<span class="hljs-params">responseData</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(responseData));
  })
  .done();
</code></pre>
<p>Similar to a POST request, the server sends back a response to tell you if the resource was updated on the server or not (and why it failed).</p>
<h2 id="heading-how-to-make-a-delete-request-in-react-native">How to Make a DELETE Request in React Native</h2>
<p>As you might have guessed, a DELETE request is used to delete a specific resource from a server.</p>
<p>In a DELETE request, you only specify the ID of resource you want to delete on the server:</p>
<pre><code class="lang-js">fetch(<span class="hljs-string">"https://jsonplaceholder.typicode.com/posts/1"</span>, {
  <span class="hljs-attr">method</span>: <span class="hljs-string">"DELETE"</span>,
  <span class="hljs-attr">headers</span>: {
    <span class="hljs-attr">Accept</span>: <span class="hljs-string">"application/json"</span>,
    <span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span>,
  },
})
  .then(<span class="hljs-function">(<span class="hljs-params">response</span>) =&gt;</span> response.json())
  .then(<span class="hljs-function">(<span class="hljs-params">responseData</span>) =&gt;</span> {
    <span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">JSON</span>.stringify(responseData));
  })
  .done();
</code></pre>
<p>Once the server is done with processing the request, it sends back a response to tell you if the resource was deleted on the server or not (and why it failed).</p>
<h2 id="heading-how-to-integrate-other-third-party-apis-to-react-native">How to Integrate Other Third-Party APIs to React Native</h2>
<p>Third-party APIs are created and maintained by organizations other than the main developer of the application. They are used to provide access to certain external data sources so that developers can incorporate them into their applications.</p>
<p>With the widespread use of React Native for developing mobile applications, third-party APIs can be easily integrated to create powerful and feature-rich applications.</p>
<p>One of the main benefits of using third-party APIs is that they are often updated frequently, which means that the developers can quickly access the latest features and data sources. This can be especially useful when developing mobile applications, as they often require access to the latest features and data sources.</p>
<p>Additionally, you can also use third-party APIs to add features that are hard or impossible to create in-house as it requires substantial human, financial, and time resources.</p>
<p>For example, there are complex APIs for <a target="_blank" href="https://developers.facebook.com/docs/facebook-login/web/">Facebook login</a>, <a target="_blank" href="https://stripe.com/docs/api">payment processing</a>, <a target="_blank" href="https://openweathermap.org/api">weather report</a>, <a target="_blank" href="https://adapty.io/blog/react-native-in-app-purchases-implementation-tutorial">integrating in-app purchases infrastructure</a>, and so on.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>This tutorial walked you through the steps of making API requests in React Native using the built-in Fetch library.</p>
<p>I hope you enjoyed this as much as I did writing it. Have a great week.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Responsive Form with Filter Functionality Using HTML, CSS, and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ Most sites that display a list of data in a table typically have some kind of filter functionality implemented. This helps the user filter relevant items based on the text they type into the form input. In this quick tutorial, we are going to build a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-a-responsive-filterable-form-with-css-and-javascript/</link>
                <guid isPermaLink="false">66d46173d14641365a050983</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ forms ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 13 Oct 2021 20:54:46 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/10/responsiveform.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>Most sites that display a list of data in a table typically have some kind of filter functionality implemented. This helps the user filter relevant items based on the text they type into the form input.</p>
<p>In this quick tutorial, we are going to build a responsive filterable table which looks like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/ezgif.com-gif-maker--2-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>A responsive table filtered by programming language</em></p>
<p>The table will contain a list of developers. Each row contains a name, an age, and programming language. When a user types into the input field, the table will filter and return the rows which match the value entered.</p>
<p>During the process of building this, you will learn about some CSS properties as well as DOM access/manipulation with JavaScript.</p>
<p>You can get the example code from <a target="_blank" href="https://codepen.io/ubahthebuilder/pen/RwgdLoj">CodePen</a>.</p>
<h2 id="heading-getting-started">Getting Started</h2>
<p>You will need to create three simple files in your project folder. They are <code>index.html</code> for the HTML markup, <code>styles.css</code> for the styling, and <code>index.js</code> for the script.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Filterable Table<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Make sure to link your stylesheet and script file as I have done in the HTML above.</p>
<h2 id="heading-html-markup-for-the-table">HTML Markup For The Table</h2>
<p>Add the following markup inside the <code>body</code> tags:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"app"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"searchInput"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"filter developer..."</span>&gt;</span>

    <span class="hljs-tag">&lt;<span class="hljs-name">table</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">thead</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Name<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Age<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Language<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"names"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Kingsley<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>32<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>JavaScript<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Samuel<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>22<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Python<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Joyce<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>28<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Ruby<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Jake<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>29<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Python<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Daniel<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>40<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>JavaScript<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Mary<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>21<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>C<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>David<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>26<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>JavaScript<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Kelly<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>31<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>React<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Cleo<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>43<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Java<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Peter<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>19<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Vue<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>George<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>59<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Cobol<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>James<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>29<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>JavaScript<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Ethan<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>22<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>PHP<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Sandra<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>29<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>R<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Pires<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>34<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>React Native<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>Martha<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>30<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>React<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>The first element is the form input. We'll use this to collect data from the user.</p>
<p>Then we have the table. The table consists of a head (<code>thead</code>) and a body (<code>tbody</code>). The head has a single row (<code>tr</code>) of data, which is the heading. The body has 16 rows of data, each of which consist of a name, age, and programming language.</p>
<p>We wrap both these elements in a parent <code>div</code> tag. They will help us with alignment as we will see later in the CSS.</p>
<p>Save the file and open the app on a web browser.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/htmltable.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Table HTML</em></p>
<h2 id="heading-how-to-style-the-table-with-css">How to Style the Table with CSS</h2>
<p>We will now apply some styling to the table. First, we set the base styles like this:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">"https://fonts.googleapis.com/css2?family=Lato:wght@300&amp;display=swap"</span>);

<span class="hljs-comment">/* Set no margin and padding around body. Set height to take up entire screen height. Align everything to the center, both horizontally and vertically */</span>

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"lato"</span>, sans-seriff;
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-comment">/* Remove border and outline around input. Set width to take up the entire width of parent and set margin on bottom */</span>

<span class="hljs-selector-id">#searchInput</span> {
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">outline</span>: none;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-comment">/* When input gains focus, add a blue border below it */</span>

<span class="hljs-selector-id">#searchInput</span><span class="hljs-selector-pseudo">:focus</span> {
  <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#4575b6</span>;
}
</code></pre>
<p>The table will get aligned to the center.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/centertable.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Center aligned table</em></p>
<p>To make the table look better, we can use the following styling rules:</p>
<pre><code class="lang-css">
<span class="hljs-comment">/* Sets width of table container (div) to 80% of browser window's width and the height to 100% of window's height. `vh` and `vw` makes the table responsive because it scales along with screen size. Also set margin of 20px to top and bottom */</span>

<span class="hljs-selector-class">.app</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">80vw</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">20px</span> <span class="hljs-number">0</span>;
}

<span class="hljs-comment">/* Collapse all borders separating each cell. Table takes up entire width of .app. Set gray shadow around table */</span>
<span class="hljs-selector-tag">table</span> {
  <span class="hljs-attribute">border-collapse</span>: collapse;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">5px</span> <span class="hljs-number">7px</span> gray;
}

<span class="hljs-comment">/* Set shadow on just the table head */</span>
<span class="hljs-selector-tag">thead</span> {
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0px</span> <span class="hljs-number">0px</span> <span class="hljs-number">10px</span> gray;
}

<span class="hljs-comment">/* Add some space around table heading. Align text to right and transform to uppercase */</span>

<span class="hljs-selector-tag">th</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">1rem</span> <span class="hljs-number">3rem</span>;
  <span class="hljs-attribute">text-transform</span>: uppercase;
  <span class="hljs-attribute">letter-spacing</span>: <span class="hljs-number">1px</span>;
  <span class="hljs-attribute">text-align</span>: left;
}

<span class="hljs-comment">/* Add padding on each cell */</span>
<span class="hljs-selector-tag">td</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0.5rem</span> <span class="hljs-number">3rem</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">1rem</span>;
}

<span class="hljs-comment">/* Create alternating color(blue) across the rows. Set blue on all even ones (2, 4, 6 ...) */</span>

<span class="hljs-selector-tag">tr</span><span class="hljs-selector-pseudo">:nth-child(even)</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#dee8f5</span>;
}
</code></pre>
<p>Now our table looks a bit better and it's also responsive.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/ezgif.com-gif-maker--3-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Table is now responsive</em></p>
<h2 id="heading-how-to-implement-the-filter-functionality-with-javascript">How to Implement the Filter Functionality with JavaScript</h2>
<p>The table is pretty much static at this point. Using JavaScript, we will implement the logic for filtering the names based on what a user types into the form field.</p>
<p>In your script file, define a function called <code>filter</code>. In the first three lines we access the user's input value, pass the table body <code>&lt;tbody&gt;</code> into the variable <code>names</code>, and finally access all table rows <code>&lt;tr&gt;</code> from inside of the <code>&lt;tbody&gt;</code> .</p>
<p>We also change the value to uppercase to make sure everything is consistent (the user might type 'j' instead of 'J').</p>
<pre><code class="lang-js">  <span class="hljs-comment">/* This function will check for the user's input and based on that will either hide or display a particular row */</span>

  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">filter</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-comment">// Access text value and elements from the DOM </span>
    <span class="hljs-keyword">let</span> value = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchInput"</span>).value.toUpperCase();
    <span class="hljs-keyword">let</span> names = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"names"</span>);
    <span class="hljs-keyword">let</span> rows = names.getElementsByTagName(<span class="hljs-string">"tr"</span>);

   <span class="hljs-comment">// Code continues</span>
</code></pre>
<p>Next, we loop through each array. For each row, we access the last column (language column) and get its text content (actual value).</p>
<pre><code class="lang-js"><span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; rows.length; i++) {
    <span class="hljs-keyword">let</span> column = rows[i].getElementsByTagName(<span class="hljs-string">"td"</span>)[<span class="hljs-number">2</span>];
    <span class="hljs-keyword">let</span> language = column.textContent;

    rows[i].style.display =
      language.toUpperCase().indexOf(value) &gt; <span class="hljs-number">-1</span> ? <span class="hljs-string">""</span> : <span class="hljs-string">"none"</span>;
  }
}

<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchInput"</span>).addEventListener(<span class="hljs-string">"keyup"</span>, filter);
</code></pre>
<p>If the actual string value from the table contains any of the user's values from their input, we display that row. Otherwise, we hide it. We also used the ternary operator to shorten the conditional statement.</p>
<p>Finally, we add an event listener on the input. Every time the key is released, the filter will be invoked.</p>
<p>Here is the full code for that:</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">filter</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">let</span> value = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchInput"</span>).value.toUpperCase();
  <span class="hljs-keyword">var</span> names = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"names"</span>);
  <span class="hljs-keyword">var</span> rows = names.getElementsByTagName(<span class="hljs-string">"tr"</span>);

  <span class="hljs-keyword">for</span> (i = <span class="hljs-number">0</span>; i &lt; rows.length; i++) {
    <span class="hljs-keyword">let</span> column = rows[i].getElementsByTagName(<span class="hljs-string">"td"</span>)[<span class="hljs-number">2</span>];
    <span class="hljs-keyword">let</span> language = column.textContent;

    rows[i].style.display =
      language.toUpperCase().indexOf(value) &gt; <span class="hljs-number">-1</span> ? <span class="hljs-string">""</span> : <span class="hljs-string">"none"</span>;
  }
}

<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"searchInput"</span>).addEventListener(<span class="hljs-string">"keyup"</span>, filter);
</code></pre>
<p>Your table should look similar to this in the end:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/10/ezgif.com-gif-maker--2--1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>With just HTML, CSS, and JavaScript you can create some really stylish elements with advanced features.</p>
<p>I hope you learned a thing or two from this. Once again, you can check out the code on CodePen and tweak it as you like.</p>
<p>Thanks for following along.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build an Accordion Menu Using HTML, CSS and JavaScript ]]>
                </title>
                <description>
                    <![CDATA[ You can use HTML, CSS and JavaScript to create stylish and dynamic web elements. And one useful element you can build is an accordion menu. Accordion menus expand and collapse when a user clicks a button. It's a great way to not have to show all the ... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/build-an-accordion-menu-using-html-css-and-javascript/</link>
                <guid isPermaLink="false">66d46176230dff0166905893</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 29 Sep 2021 19:45:33 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/accordion-canva.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>You can use HTML, CSS and JavaScript to create stylish and dynamic web elements. And one useful element you can build is an accordion menu.</p>
<p>Accordion menus expand and collapse when a user clicks a button. It's a great way to not have to show all the info about a topic up front, and instead give users the option to show only what they need.</p>
<p>In this tutorial, I'll show you how to build a simple accordion menu that looks like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632910257498/EuFFg0l3d.gif" alt="ezgif.com-gif-maker.gif" width="600" height="325" loading="lazy"></p>
<h2 id="heading-what-is-an-accordion-menu">What is an Accordion Menu?</h2>
<p>In UI design, an accordion menu is a vertically stacked list of various pieces of information. For each list, there is a labelled header pointing to corresponding content. Each list's content is hidden by default. Clicking on a particular label will expand its content.</p>
<p>One very common use case for accordions is to hold a list of frequently asked questions. Clicking on any question will toggle/show a corresponding answer.</p>
<h2 id="heading-how-to-build-an-accordion-menu-using-html-css-and-js">How to Build an Accordion Menu using HTML, CSS and JS</h2>
<p>We'll begin by defining the HTML markup. If you are using an IDE like VS Code and you have emmet installed, create a new <code>index.html</code> file and type <code>!</code> followed by enter. This should create HTML boilerplate code for your project.</p>
<p>Alternatively, you can copy the following code into your <code>index.html</code>, or get the code for this project from <a target="_blank" href="https://codepen.io/ubahthebuilder/pen/gORqxNe">Codepen</a>.</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"app.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>The folder structure is simple. We will create a folder called accordion. Inside the folder we will create three files: <code>index.html</code>, <code>styles.css</code>, and <code>app.js</code>. We will also link all files into our HTML markup as you can see above.</p>
<h3 id="heading-html-markup-for-the-accordion">HTML Markup for the Accordion</h3>
<p>For each list in the menu, we will have two <code>div</code>s – one for the label, the other for the content.</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"accordion-body"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"accordion"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Frequently Asked Questions<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is HTML<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>Hypertext Markup Language (HTML) is a computer language that makes up most web pages and online applications. A hypertext is a text that is used to reference other pieces of text, while a markup language is a series of markings that tells web servers the style and structure of a document. HTML is very simple to learn and use.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is CSS?<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>CSS stands for Cascading Style Sheets. It is the language for describing the presentation of Web pages, including colours, layout, and fonts, thus making our web pages presentable to the users. CSS is designed to make style sheets for the web. It is independent of HTML and can be used with any XML-based markup language. CSS is popularly called the design language of the web.
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is JavaScript?<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>JavaScript is a scripting or programming language that allows you to implement complex features on web pages — every time a web page does more than just sit there and display static information for you to look at — displaying timely content updates, interactive maps, animated 2D/3D graphics, scrolling video jukeboxes, etc. — you can bet that JavaScript is probably involved. It is the third of the web trio.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is React?<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>React is a JavaScript library created for building fast and interactive user interfaces for web and mobile applications. It is an open-source, component-based, front-end library responsible only for the application’s view layer. In Model View Controller (MVC) architecture, the view layer is responsible for how the app looks and feels. React was created by Jordan Walke, a software engineer at Facebook. <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is PHP?<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>PHP is a server-side and general-purpose scripting language that is especially suited for web development. PHP originally stood for Personal Home Page. However, now, it stands for Hypertext Preprocessor. It’s a recursive acronym because the first word itself is also an acronym.<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>What is Node JS?<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"content"</span>&gt;</span>Node.js is an open-source, cross-platform, back-end JavaScript runtime environment that runs on the V8 engine and executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">hr</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"index.js"</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
</code></pre>
<p>Without CSS, our page will look all bare, like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632478639319/6_WWaZagG.png" alt="htmlook.png" width="1600" height="732" loading="lazy"></p>
<p><em>How the accordion menu looks without CSS</em></p>
<h3 id="heading-how-to-style-the-accordion-with-css">How to Style the Accordion with CSS</h3>
<p>We want our accordion menu to look good, of course. Time to bring some CSS into play. I've added some comments in the code to explain what each piece is doing:</p>
<pre><code class="lang-c">@<span class="hljs-function"><span class="hljs-keyword">import</span> <span class="hljs-title">url</span><span class="hljs-params">(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Rubik:wght@300&amp;display=swap'</span>)</span></span>;

<span class="hljs-comment">/* Sets the background color of the body to blue. Sets font to Rubik */</span>

body {
  background-color: #<span class="hljs-number">0</span>A2344;
  font-family: <span class="hljs-string">'rubik'</span>, sans-serif;
}

<span class="hljs-comment">/* Aligns the heading text to the center. */</span>

h1 {
  text-align: center;
}

<span class="hljs-comment">/* Sets the width for the accordion. Sets the margin to 90px on the top and bottom and auto to the left and right */</span>

.accordion {
  width: <span class="hljs-number">800</span>px;
  margin: <span class="hljs-number">90</span>px <span class="hljs-keyword">auto</span>;
  color: black;
  background-color: white;
  padding: <span class="hljs-number">45</span>px <span class="hljs-number">45</span>px;
}
</code></pre>
<p>With all of these styles applied, here is how our accordion will look like now:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632479039630/X8DyVEIrx.png" alt="withcss1.png" width="1583" height="732" loading="lazy"></p>
<p><em>Styles added to the accordion menu</em></p>
<p>Now we need to start doing some work on the inside to set up its functionality. First, we set the position property for each of the containers (holding both the label and content) to relative.</p>
<p>That means we can now position its children relative to it.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">10px</span>;
}

<span class="hljs-comment">/* Positions the labels relative to the .container. Adds padding to the top and bottom and increases font size. Also makes its cursor a pointer */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">cursor</span>: pointer;
}
</code></pre>
<p>You can now notice the difference in the image below:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632479377592/ZaOptRvWO5.png" alt="withcss2.png" width="1583" height="732" loading="lazy"></p>
<p>The next action will be to append a little '+' sign at the end of each list. This will let users know that they can expand the section to learn/see more.</p>
<p>We will achieve this using the <code>::before</code> selector. The <code>::before</code> and <code>::after</code> selector is used to place content before of or after a specified element.</p>
<p>Here, we are inserting '+' before the label. But we will use the offset properties <code>top</code> and <code>right</code> to place it at the far right corner.</p>
<pre><code class="lang-css">
<span class="hljs-comment">/* Positions the plus sign 5px from the right. Centers it using the transform property. */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.label</span><span class="hljs-selector-pseudo">::before</span> {
  <span class="hljs-attribute">content</span>: <span class="hljs-string">'+'</span>;
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">50%</span>;
  <span class="hljs-attribute">right</span>: -<span class="hljs-number">5px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">50%</span>);
}

<span class="hljs-comment">/* Hides the content (height: 0), decreases font size, justifies text and adds transition */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.content</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">background</span>: white;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">text-align</span>: justify;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">780px</span>;
  <span class="hljs-attribute">overflow</span>: hidden;
  <span class="hljs-attribute">transition</span>: <span class="hljs-number">0.5s</span>;
}

<span class="hljs-comment">/* Adds a horizontal line between the contents */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-tag">hr</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100</span>;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid grey;
}
</code></pre>
<p>This will make everything better. Also notice that the content for each list is now hidden.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632480128515/4Q2E4YR5d.png" alt="nowbig.png" width="1583" height="732" loading="lazy"></p>
<p><em>Accordion menu's content is now hidden</em></p>
<h3 id="heading-adding-javascript-to-our-accordion">Adding JavaScript to our Accordion</h3>
<p>At this point, our accordion is pretty much static. To make a container display the content when a user clicks on it, we will need to bring in some JavaScript.</p>
<p>Navigate to your script <code>app.js</code> and type in the following code:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> accordion = <span class="hljs-built_in">document</span>.getElementsByClassName(<span class="hljs-string">'container'</span>);

<span class="hljs-keyword">for</span> (i=<span class="hljs-number">0</span>; i&lt;accordion.length; i++) {
  accordion[i].addEventListener(<span class="hljs-string">'click'</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">this</span>.classList.toggle(<span class="hljs-string">'active'</span>)
  })
}
</code></pre>
<p>This script will access all of our lists by <code>classname</code> of <code>container</code>.</p>
<p>Then we will loop through the list. For each container, we simply want to register an event listener to it. When it gets clicked, we want to toggle the class "active" on that element.</p>
<p>Now we are going to test this effect. Click the first container with the label <code>What is HTML</code>, open your DevTools (click on F12), and inspect it inside of the elements tab.</p>
<p>You should find the <code>active</code> class registered on it:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632480884158/5AuG4fo_q.png" alt="active.png" width="483" height="443" loading="lazy"></p>
<p><em>active class toggled on first menu item</em></p>
<p>Clicking on the element again will remove the <code>active</code> class from it.</p>
<h3 id="heading-how-to-finish-up-the-app">How to Finish Up the App</h3>
<p>There is one last thing we need to do. We need to create an active class within a stylesheet. We will define how we want our accordion to look once JavaScript toggles the class on a container.</p>
<pre><code class="lang-css">
<span class="hljs-comment">/* Unhides the content part when active. Sets the height */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.container</span><span class="hljs-selector-class">.active</span> <span class="hljs-selector-class">.content</span> {
  <span class="hljs-attribute">height</span>: <span class="hljs-number">150px</span>;
}

<span class="hljs-comment">/* Changes from plus sign to negative sign once active */</span>

<span class="hljs-selector-class">.accordion</span> <span class="hljs-selector-class">.container</span><span class="hljs-selector-class">.active</span> <span class="hljs-selector-class">.label</span><span class="hljs-selector-pseudo">::before</span> {
  <span class="hljs-attribute">content</span>: <span class="hljs-string">'-'</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
}
</code></pre>
<p>This is how our app looks and behaves in the end:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1632910257498/EuFFg0l3d.gif" alt="ezgif.com-gif-maker.gif" width="600" height="325" loading="lazy"></p>
<p><em>Final look</em></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>So in this tutorial, we built an accordion menu using HTML, CSS, and JavaScript.</p>
<p>Thanks for following along. I hope you learned something useful from this tutorial.</p>
<p>If you are interested in content like this, <a target="_blank" href="https://ubahthebuilder.tech">follow my blog</a>.</p>
<p>Have a great week.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ How to Build a Sign Up Form with Floating Labels and Transitions Using Plain HTML and CSS ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial we are going to build a modern sign up form with floating labels and smooth transitions using plain HTML and CSS. A view As you can see in the above image, when an input within the form gains focus, its label floats to the top and a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/how-to-build-sign-up-form-with-html-and-css/</link>
                <guid isPermaLink="false">66d4617c57503cc72873dee0</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ transitions ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Mon, 13 Sep 2021 21:15:35 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/floating-label.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial we are going to build a modern sign up form with floating labels and smooth transitions using plain HTML and CSS.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--9--2.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>A view</em></p>
<p>As you can see in the above image, when an input within the form gains focus, its label floats to the top and a semi-thick border appears around the input. If text gets typed into the input and the input loses focus, the label remains on top. Otherwise, the label drops back down into the input.</p>
<p>Many modern forms have some sort of transition applied to them. Not only do these transitions make the form more dynamic, but they also help guide the user on the state of the input (that is whether it has focus or not) and what kind of data each input is expected to handle.</p>
<p>In this tutorial, you will learn about some cool CSS features like transitions, selectors like <code>:placeholder_focus</code> ,and many other CSS properties you should know.</p>
<p>Let's dive in!</p>
<h2 id="heading-the-html-markup">The HTML Markup</h2>
<p>We are going to define the markup for our sign up form. But before that, we have to set up our HTML boilerplate and correctly link to our stylesheet from the <code>head</code> tag. You can easily do this with the <a target="_blank" href="https://emmet.io/">Emmet plugin</a> by typing <code>!</code> then tab in your IDE/Code editor.</p>
<p>You can also copy this boilerplate and paste it into your <code>index.html</code> file:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<p>Within the body tag, we define the markup for our sign up form:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"signupFrm"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"form"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Sign up<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Email<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Username<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Password<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"inputContainer"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"input"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"a"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"label"</span>&gt;</span>Confirm Password<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"submitBtn"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Sign up"</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
</code></pre>
<p>We create a container <code>div</code> to hold the form element. Each of the form's inputs, along with its text label, are wrapped inside a container div. The labels serve the purpose of informing the user what information each input should take in.</p>
<p>And our page will look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/noCSS.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>HTML Form of four inputs and four labels</em></p>
<p>You may notice that the placeholder value we have assigned to all inputs is "a". This will be helpful later in the tutorial when we start to apply some dynamic logic.</p>
<h2 id="heading-how-to-style-the-form">How to Style the Form</h2>
<p>Our form is pretty basic, so let's add some styling to make it look nicer.</p>
<p>First, we need to perform some resets and set the background color:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Lato&amp;display=swap'</span>);

<span class="hljs-comment">/* Get rid of all default margins/paddings. Set typeface */</span>
<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">box-sizing</span>: border-box;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"lato"</span>, sans-serif;
}
</code></pre>
<p>Here's what our page will look like:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/reset-lato.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Without any styling yet</em></p>
<p>After stying the <code>body</code>, we'll set the display mode of the content to <code>flex</code>. This makes sure that all the direct children inside a container element <code>div</code> are displayed side-by-side by default.</p>
<p>In our case, there's only one child inside the container <code>signupFrm</code>. The only reason we use <code>display: flex</code> here is to use the <code>align-items</code> and <code>justify-content</code> properties to help center everything vertically and horizontally:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Puts the form in the center both horizontally and vertically. Sets its height to 100% of the viewport's height */</span>

<span class="hljs-selector-class">.signupFrm</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: center;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100vh</span>;
}
</code></pre>
<p>The <code>vh</code> property, which stands for viewport height, ensures that the form takes up 100% of the height of the browser window, regardless of the screen size or orientation. That will make it more responsive.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/middle.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Our form is now aligned to the center</em></p>
<p>Now we'll style the form a bit:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.form</span> {
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">400px</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">8px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span> <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">10px</span> <span class="hljs-number">25px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">92</span>, <span class="hljs-number">99</span>, <span class="hljs-number">105</span>, .<span class="hljs-number">2</span>);
}

<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>In the first style targeted on the form, we set the background to white, we give it a width of 400px, we add some curve around the form, and finally we set a shadow around the box. We also set the font size of the title and some space below the element.</p>
<p>And the result should look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/with-card.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The form is now inside a card, with a box shadow</em></p>
<p>Next, we'll style the <code>div</code> which contains the form inputs and form labels.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.inputContainer</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">45px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">90%</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">17px</span>;
}
</code></pre>
<p>We set the position property of our input's container <code>div</code> to relative. This will enable us position the children <code>input</code> and <code>label</code> however we want. We also set the width to take up 90 percent of the entire container width.</p>
<p>This is how our form will be rendered in the web browser.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/margin-added.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Looks better</em></p>
<p>Now we need to style our inputs.</p>
<p>We first set the <code>position</code> to <code>absolute</code>. This will allow us to move each of them to the top-left part of the relatively positioned container parent.</p>
<p>We also need to hide our arbitrary placeholder text (the "a" characters mentioned earlier), so they don't overlap with the text within each label. The placeholder text will be needed when we implement the transition:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Style the inputs */</span>

<span class="hljs-selector-class">.input</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">0px</span>;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">border</span>: <span class="hljs-number">1px</span> solid <span class="hljs-number">#DADCE0</span>;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">7px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">outline</span>: none;
  <span class="hljs-attribute">background</span>: none;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">1</span>;
}

<span class="hljs-comment">/* Hide the placeholder texts (a) */</span>

<span class="hljs-selector-pseudo">::placeholder</span> {
  <span class="hljs-attribute">color</span>: transparent;
}
</code></pre>
<p>With the styles applied, our form should now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/absolute.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The placeholder "a" is no longer visible</em></p>
<p>Next, we style the text labels:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Styling text labels */</span>

<span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">position</span>: absolute;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">4px</span>;
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#DADCE0</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">transition</span>: <span class="hljs-number">0.5s</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">0</span>;
}
</code></pre>
<p>The label shows text that tells which information is expected inside the input. We start by setting its position to absolute. And by setting the <code>top</code> and <code>left</code> properties, we can move the text upwards relative to its container.</p>
<p>Next we set a transition of 0.5 seconds. This is how long it will take the text to move up when hovered on.</p>
<p>Finally, we also set a z-index of 0. The low z-index ensures that the label is positioned behind other "higher-placed" elements if they ever overlap.</p>
<p>Here is what gets rendered on the page:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/labels-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The result</em></p>
<p>Now we are going to focus on the buttons.</p>
<p>We'll add some smooth animations with the CSS <code>transform</code> property, which moves the button up a little and changes the color once it's hovered over:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.submitBtn</span> {
  <span class="hljs-attribute">display</span>: block;
  <span class="hljs-attribute">margin-left</span>: auto;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">15px</span> <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">30px</span>;
}

<span class="hljs-selector-class">.submitBtn</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#9867C5</span>;
  <span class="hljs-attribute">transform</span>: <span class="hljs-built_in">translateY</span>(-<span class="hljs-number">2px</span>);
}
</code></pre>
<p>Here is the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--6-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The button scales up and changes color when hovered over</em></p>
<p>Next, we need to perform some state changes.</p>
<p>When an input gains focus, we want to position its label beyond the top of the container (-7px), 3 pixels from the left, reduce the font-size to 14, and change the color to purple:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:focus</span> + <span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">top</span>: -<span class="hljs-number">7px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">3px</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">10</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">600</span>;
  <span class="hljs-attribute">color</span>: purple;
}
</code></pre>
<p>Here's the result:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--5-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Labels go up when input gains focus</em></p>
<p>We also need to add a purple border around the input when it gains focus.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:focus</span> {
  <span class="hljs-attribute">border</span>: <span class="hljs-number">2px</span> solid purple;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--7--3.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Purple border added</em></p>
<p>Finally, we have to do something very important.</p>
<p>Currently, when you type some text into the form and move focus (your mouse) away from it, the label text and the text within the input collide:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--8--3.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Collision between label and input value</em></p>
<p>With the following CSS, we'll specify that, when we type a value into the input and change focus, we want the label to remain floating. Also, well specify that we want the label text to lose its purple color:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.input</span><span class="hljs-selector-pseudo">:not(</span><span class="hljs-selector-pseudo">:placeholder-shown)+</span> <span class="hljs-selector-class">.label</span> {
  <span class="hljs-attribute">top</span>: -<span class="hljs-number">7px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">3px</span>;
  <span class="hljs-attribute">z-index</span>: <span class="hljs-number">10</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">600</span>;
}
</code></pre>
<p>And with that, here is the final look of our sign up page.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--9--1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look</em></p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>I hope you learned some new things about CSS from this tutorial. CSS transitions bring life to your website, and in this guide we have made our form much more lively with them.</p>
<p>You can get all the code from this tutorial from this <a target="_blank" href="https://github.com/KingsleyUbah/Sign-Up-CSS">GitHub repository</a>.</p>
<p>I recently created a newsletter where I provide practical tips and resources on learning web development. Subscribe to my <a target="_blank" href="https://www.getrevue.co/profile/ubahthebuilder">newsletter</a> and get tips right in your inbox.</p>
<p>Thanks for following along.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ CSS Flexbox and Grid Tutorial – How to Build a Responsive Landing Page with HTML and CSS ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, we are going to build a simple landing page for an online education platform called Skilllz. This tutorial will teach you how to use and implement CSS Flexbox and CSS Grid alignment. We'll also cover many other CSS concepts. And fin... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/css-flexbox-and-grid-tutorial/</link>
                <guid isPermaLink="false">66d461793dce891ac3a9683e</guid>
                
                    <category>
                        <![CDATA[ CSS ]]>
                    </category>
                
                    <category>
                        <![CDATA[ CSS Grid ]]>
                    </category>
                
                    <category>
                        <![CDATA[ flexbox ]]>
                    </category>
                
                    <category>
                        <![CDATA[ HTML ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 08 Sep 2021 17:16:00 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/SkilllzLanding.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, we are going to build a simple landing page for an online education platform called <strong>Skilllz.</strong></p>
<p>This tutorial will teach you how to use and implement CSS Flexbox and CSS Grid alignment. We'll also cover many other CSS concepts. And finally, we'll learn how to make the page responsive so that it works on all screen sizes.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>The tutorial is divided into five parts:</p>
<ul>
<li><p>How to Build the Navigation Bar</p>
</li>
<li><p>How to Build the Showcase Section</p>
</li>
<li><p>How to Build the Lower Section</p>
</li>
<li><p>How to Build the Footer Section</p>
</li>
<li><p>How to Make the Page Responsive</p>
</li>
</ul>
<p>Each of these sections will teach you some new CSS and web development skills and tools. So let's jump right in.</p>
<h2 id="heading-how-to-create-the-html-boilerplate">How to Create the HTML Boilerplate</h2>
<p>If you have emmet installed in your IDE, you can generate an HTML boilerplate for your project by typing <code>!</code> and clicking the <code>enter</code> or <code>tab</code> key on your keyboard.</p>
<p>If not, you can copy this boilerplate code and paste it into your code editor:</p>
<pre><code class="lang-html"><span class="hljs-meta">&lt;!DOCTYPE <span class="hljs-meta-keyword">html</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">html</span> <span class="hljs-attr">lang</span>=<span class="hljs-string">"en"</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">head</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">charset</span>=<span class="hljs-string">"UTF-8"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">name</span>=<span class="hljs-string">"viewport"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"width=device-width, initial-scale=1.0"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">meta</span> <span class="hljs-attr">http-equiv</span>=<span class="hljs-string">"X-UA-Compatible"</span> <span class="hljs-attr">content</span>=<span class="hljs-string">"ie=edge"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">title</span>&gt;</span>Document<span class="hljs-tag">&lt;/<span class="hljs-name">title</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"styles.css"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ=="</span>
  <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span> <span class="hljs-attr">referrerpolicy</span>=<span class="hljs-string">"no-referrer"</span> /&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">head</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">body</span>&gt;</span>

<span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">html</span>&gt;</span>
</code></pre>
<h3 id="heading-how-to-use-font-awesome-icons">How to Use Font Awesome Icons</h3>
<p>As you can see in one of the shots, we will be using some font icons to give better swap to our service section.</p>
<p>For this, we will be using font awesome from the CDN. If you created an HTML biolerplate by yourself, copy the following <code>link</code> tag and paste it into your <code>head</code> tag:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">link</span> <span class="hljs-attr">rel</span>=<span class="hljs-string">"stylesheet"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css"</span> <span class="hljs-attr">integrity</span>=<span class="hljs-string">"sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ=="</span>
  <span class="hljs-attr">crossorigin</span>=<span class="hljs-string">"anonymous"</span> <span class="hljs-attr">referrerpolicy</span>=<span class="hljs-string">"no-referrer"</span> /&gt;</span>
</code></pre>
<h2 id="heading-lets-get-started">Let's Get Started</h2>
<p>First, make sure that your stylesheet file (.css) is properly linked to your HTML page.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/test.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-how-to-build-the-navigation-bar">How to Build the Navigation Bar</h2>
<p>The Navigation Bar section is going to be comprised of our site's name as well as two navigation links: <code>Log in</code> and <code>check courses</code>.</p>
<p>Here is the markup for our navbar:</p>
<pre><code class="lang-js">&lt;div <span class="hljs-class"><span class="hljs-keyword">class</span></span>=<span class="hljs-string">"navbar"</span>&gt;
        <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container flex"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"logo"</span>&gt;</span>Skilllz<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">nav</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"outline"</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span>Log in<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"nav"</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"outline"</span>&gt;</span>Check courses<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>                 &lt;/<span class="hljs-attr">li</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
</code></pre>
<p>On the div wrapping the elements inside this section (the navbar), we register the container and flex class.</p>
<ul>
<li><p><code>.container</code>: we will use this utility class in every section to make sure that the inner elements do not exceed a certain width which we'll specify in CSS</p>
</li>
<li><p><code>.flex</code>: we will use this utility class to display children elements in a horizontally aligned manner (side-by-side) using CSS Flexbox.</p>
</li>
</ul>
<p>Inside the <code>div</code> we have an <code>h1</code> with class of <code>logo</code> and two navigation links <code>li&gt;a</code> with the <code>outline</code> classes, respectively.</p>
<p>At this point, our page will look all plain and bare like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarHTML1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>No CSS yet</em></p>
<h3 id="heading-how-to-apply-css-styling-to-our-page">How to Apply CSS Styling to our Page</h3>
<p>We now have to apply some CSS rules to style our nav section the way we want. What we want to do first is set the base styling for our web page with the following code:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Override default style and set padding and margin to nothing */</span>

* {
  <span class="hljs-attribute">box-sizing</span>: border-box;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>
}

<span class="hljs-comment">/* White text throughout */</span>

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"lato"</span>, sans-serif;
  <span class="hljs-attribute">color</span>: white;
}

<span class="hljs-comment">/* Make all link text black with no text decoration */</span>
<span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">text-decoration</span>: none;
}


<span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">600</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.2</span>;
}


<span class="hljs-selector-tag">h2</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">25px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">300</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.2</span>;
}

<span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
}

<span class="hljs-comment">/* All images must not be larger than parent container */</span>
<span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}

<span class="hljs-comment">/* No styling on list items */</span>
<span class="hljs-selector-tag">li</span> {
  <span class="hljs-attribute">list-style-type</span>: none;
}



<span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
}
</code></pre>
<p>With the default styles applied, our page will now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarHTML2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we need to define the styling for our container class:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Centers it, sets a maximum width and makes sure elements can flow past it*/</span>

<span class="hljs-selector-class">.container</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">1200px</span>;
  <span class="hljs-attribute">overflow</span>: visible;
}
</code></pre>
<p>Now, our content will not exceed the maximum width specified.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarHTML3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>After that, we need to set the background color of our navbar section to purple:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Sets background color, height and padding*/</span>

<span class="hljs-selector-class">.navbar</span> {
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">70px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0</span> <span class="hljs-number">30px</span>;
}
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarHTML4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Then we target only the <code>h1</code> element inside the <code>navbar</code> and specify the following styles:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Sets font size, reduces font-weight, adds margin and line height */</span>

<span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">300</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">line-height</span>: <span class="hljs-number">1.2</span>;
}
</code></pre>
<p>With that style applied, our <code>h1</code> heading will look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarH5.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now we need to display both child elements inside the container <code>h1</code> and <code>nav</code> side-by-side using Flexbox.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-class">.flex</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-between;
  <span class="hljs-attribute">align-items</span>: center;
  <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
}
</code></pre>
<p>First, we set the display mode to <code>flex</code>. This will align the elements side by side <strong>by default</strong>.</p>
<p>We then justify the content, adding a considerable space between each item using the <code>space-between</code> value. We align the items to appear at the center (middle) of the container and set its height to take up the entire container.</p>
<p>This is what our page should now look like:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navrbarH6.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Cool right?</em></p>
<p>However, we also do not want both of our navigation link stacked on top of each other. Instead, we want them to be displayed side-by-side. Guess how we do that? With Flexbox!</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">display</span>: flex;
}

<span class="hljs-comment">/* Changes the color of both links to white, adds padding between them and margin as well */</span>

<span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">9px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">10px</span>;
}
</code></pre>
<p>Our page will now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/navbarH7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The power of CSS flexbox</em></p>
<p>If you watched the brief intro video, you will notice that whenever I hover over any of the links, the text color changes to a lighter shade of purple and a solid border appears below it.</p>
<p>We can implement this transition using the CSS <code>:hover</code> pseudo-element:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#9867C5</span>;
  <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">3px</span> solid <span class="hljs-number">#9867C5</span>;
}
</code></pre>
<p>Now watch this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--1-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Hover effect on the links</em></p>
<p>And with that, we have come to the end of the navbar section.</p>
<h2 id="heading-how-to-build-the-showcase-area">How to Build the Showcase Area</h2>
<p>The showcase area is going to house the headline text, supporting text, a form for signing up new users, as well as a headline image.</p>
<p>This section is going to be divided in two: the left side and the right side. In other words, it will be displayed as a grid of two units.</p>
<p>Here is the markup for this section:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"showcase"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid-item-1"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"showcase-text"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Learn any digital skill of your choice today<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"supporting-text"</span>&gt;</span> Over 30,000 students currently learn with us<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"showcase-form"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">action</span>=<span class="hljs-string">""</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"email"</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">"Enter your email address"</span>&gt;</span>
                    <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"submit"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"btn"</span> <span class="hljs-attr">value</span>=<span class="hljs-string">"Start Learning"</span>&gt;</span>
                  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"little-info"</span>&gt;</span>Try out our free courses today. No risk, no credit card required<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

              <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"grid-item-2"</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"image"</span>&gt;</span>
                  <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/transparent.png"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
                <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
              <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
           <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<p>Currently, our app is going to look a bit messy:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-apply-css-styling-to-our-showcase-area">How to Apply CSS Styling to our Showcase Area</h3>
<p>First, we set the height of the showcase section as well as a background color:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.showcase</span> {
  <span class="hljs-attribute">height</span>: <span class="hljs-number">300px</span>;
  <span class="hljs-attribute">background-color</span>: purple;
}
</code></pre>
<p>Our app will now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Still messy</em></p>
<blockquote>
<p>NOTE: I changed the color of <code>h1</code> to white</p>
</blockquote>
<p>Next, we apply the following styles:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Adds margin below the text */</span>
<span class="hljs-selector-class">.showcase</span> <span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">30px</span>;
}

<span class="hljs-comment">/* Adds a shadow below the image */</span>
<span class="hljs-selector-class">.showcase</span> <span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">7px</span> <span class="hljs-number">7px</span> <span class="hljs-number">7px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.2</span>);
}

<span class="hljs-comment">/* Adds some padding on the form content */</span>
<span class="hljs-selector-class">.showcase-form</span> {
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">7px</span>;
}
</code></pre>
<p>This brings us to the main activity. If you remember, I said that we were going to be creating two sections (grids) inside the showcase container. With the grid class registered on that container, we can align its content using CSS grid display like this:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid</span> {
  <span class="hljs-attribute">overflow</span>: visible;
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">60%</span> <span class="hljs-number">40%</span>;
}
</code></pre>
<p>This will create two columns inside of our showcase container. The first part will take up 60 percent of the container, and the second part will take up the remaining 40 percent of the container.</p>
<p>The overflow visible will ensure that the image (if bigger than the container) will flow beyond the container.</p>
<p>Our app will now look like this</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we need to set some space between the navigation area and the showcase area.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.grid-item-1</span>,
<span class="hljs-selector-class">.grid-item-2</span> {
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">50px</span>;
}
</code></pre>
<p>As a result, it is now spaced out a bit:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Now, we need to style both of our form inputs because they don't look so nice. We select the first input by its type (email) and select the second by its class name, <code>btn</code>.</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.showcase</span> <span class="hljs-selector-tag">input</span><span class="hljs-selector-attr">[type=<span class="hljs-string">'email'</span>]</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">70px</span> <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">14px</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">6px</span>;
}

<span class="hljs-selector-class">.btn</span> {
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">6px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">12px</span> <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">background</span>: <span class="hljs-number">#9867C5</span>;
  <span class="hljs-attribute">border</span>: none;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">16px</span>;
  <span class="hljs-attribute">cursor</span>: pointer;
  <span class="hljs-attribute">box-shadow</span>: <span class="hljs-number">0</span> <span class="hljs-number">10px</span> <span class="hljs-number">10px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.2</span>);
}
</code></pre>
<p>This style will transform both our form inputs to this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH5-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Form input styled better</em></p>
<p>Also maybe change the font of the supporting text:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.showcase-form</span> {
  <span class="hljs-attribute">margin</span>: auto;
}

<span class="hljs-comment">/* Change typeface and its size */</span>
<span class="hljs-selector-class">.little-info</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">15px</span>;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"poppins"</span>, sans-serif;

}
</code></pre>
<p>This is the final look of our showcase section:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/showcaseH7.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look of showcase section</em></p>
<p>That's it for this section!</p>
<h2 id="heading-how-to-build-the-lower-part-of-the-page">How to Build the Lower Part of the Page</h2>
<p>The lower part of the page is going to contain two sections, the <strong>stats</strong> section and the <strong>testimonials</strong> section.</p>
<p>The stats container which displays the services offered by <strong>Skilllz</strong> will be made up of three <code>div</code>s, each of which houses a font awesome icon, an <code>h3</code> of class <code>title</code>, and a paragraph <code>p</code> of class <code>text</code>.</p>
<p>The testimonial container will hold the testimonials of three random people who learned using Skillz. I grabbed the pictures from <a target="_blank" href="https://unsplash.com/s/photos/random-people">unsplash</a>.</p>
<h3 id="heading-how-to-build-the-stats-section">How to Build the Stats Section</h3>
<p>First, we are going to work on the stats section. The text is just a dummy 'lorem50' text to act as a filler for this demo.</p>
<p>Here is the markup for it:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"lower-container container"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"stats"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"flex"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"stat"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fa fa-folder-open fa-2x"</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Over 300 available courses<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text"</span>&gt;</span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
              Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"stat"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fa fa-graduation-cap fa-2x"</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Free certificate offered on all courses<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text"</span>&gt;</span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
              Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"stat"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">i</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fa fa-credit-card-alt fa-2x"</span> <span class="hljs-attr">aria-hidden</span>=<span class="hljs-string">"true"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">i</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"title"</span>&gt;</span>Book 1on1 session for as low as $5<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"text"</span>&gt;</span>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
              Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<p>This section will be displayed as a blank page. This is because we had set the color of our text in the whole body to white. So we have to add some styling.</p>
<h3 id="heading-how-to-apply-css-styling-to-the-stats-section">How to Apply CSS Styling to the Stats Section</h3>
<p>First we need to apply the following styles:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Centers the container, sets a maximum width
.lower-container {
  margin: 120px auto;
  padding: 0;
  max-width: 1400px;
}


/* Targets all h3 with class of title */</span>
<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">text-align</span>: left;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">padding-top</span>: <span class="hljs-number">10px</span>;
}

<span class="hljs-comment">/* Targets the paragraphs with class name of text */</span>
<span class="hljs-selector-class">.text</span> {
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">19px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span>, <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">text-align</span>: justify;
}
</code></pre>
<p>This will now make our text visible:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/lower1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Notice that the font icons from Font Awesome are not visible. We will be working on that pretty soon.</p>
<p>But before then, we will need to do something important. We do intend for all of the three stat <code>div</code>s to be aligned horizontally (side-by-side). To achieve that, we will once again be using CSS Flexbox:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Display horizontally, put a little space around them */</span>
<span class="hljs-selector-class">.flex</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-around;
}

<span class="hljs-comment">/* Add some padding around the container. Align text centrally */</span>
<span class="hljs-selector-class">.stats</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">45px</span> <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-comment">/* Set margin and width */</span>
<span class="hljs-selector-class">.stat</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
}
</code></pre>
<p>This is how our app will now look:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/lower2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Still no icons? Time to fix that!</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.stats</span> <span class="hljs-selector-class">.fa</span> {
  <span class="hljs-attribute">color</span>: purple;
}
</code></pre>
<p>and voilà!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/lower4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-build-the-testimonials-section">How to Build the Testimonials Section</h3>
<p>The second section inside of the lower container <code>div</code> of the page is the testimonials section. This section is going to be made up of three cards, each of which contains the image of the person (clipped inside a circle), their name, and the person's testimonial.</p>
<p>Here is the markup for that:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">section</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"testimonials"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"testimonial grid-3"</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/4.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Aston<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>I have learnt web development using this platfrom and I am going to say this is the best platform for learning. Absolutely
            worth the investment!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/5.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Beth<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>I have learnt copywriting using this platfrom and I am going to say this is the best platform for learning. Absolutely
            worth the investment!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"card"</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"circle"</span>&gt;</span>
              <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./images/6.jpg"</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">""</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span>Chris<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
            <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>I have learnt affilitate marketing using this platfrom and I am going to say this is the best platform for learning. Absolutely
            worth the investment!<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">section</span>&gt;</span>
</code></pre>
<h3 id="heading-how-to-apply-css-styling-to-it">How to Apply CSS Styling to it</h3>
<p>First, we set the text color to black so we can see it:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.testimonial</span> {
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">40px</span>;
}
</code></pre>
<p>When applied, it should make the text visible and add some padding to the section:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/testi1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we set the image to take up the height of its parent container:</p>
<pre><code class="lang-css">
<span class="hljs-comment">/* Wrap the image inside a cirle shape and set height to take up all of parent element */</span>

<span class="hljs-selector-class">.testimonial</span> <span class="hljs-selector-tag">img</span> {
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">clip-path</span>: <span class="hljs-built_in">circle</span>();
}

<span class="hljs-comment">/* Align text centrally */</span>

<span class="hljs-selector-class">.testimonial</span> <span class="hljs-selector-tag">h3</span>{
  <span class="hljs-attribute">text-align</span>: center;
}
</code></pre>
<p>If you check the final layout in the gif, you will notice that all three testimonial cards are aligned side-by-side on the same line.</p>
<p>So we will need to create a div of three equal columns using the CSS grid arrangement.</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Create a grid of three equal columns. Set a gap of 40 px between them */</span>

<span class="hljs-selector-class">.grid-3</span> {
  <span class="hljs-attribute">display</span>: grid;
  <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-built_in">repeat</span>(<span class="hljs-number">3</span>, <span class="hljs-number">1</span>fr);
  <span class="hljs-attribute">gap</span>: <span class="hljs-number">40px</span>;
}


<span class="hljs-comment">/* Create a white card with some shadow around it */</span>
<span class="hljs-selector-class">.card</span> {
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">background-color</span>: white;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
  <span class="hljs-attribute">box-shadow</span>: -<span class="hljs-number">7px</span> -<span class="hljs-number">7px</span> <span class="hljs-number">20px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.2</span>),
               <span class="hljs-number">7px</span>  <span class="hljs-number">7px</span> <span class="hljs-number">20px</span> <span class="hljs-built_in">rgba</span>(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">0.2</span>)
}
</code></pre>
<p>With all of these styles applied, the testimonials section will now look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/testi2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Finally, we style the circle <code>div</code> and position it relative to the top border of the card using CSS:</p>
<pre><code class="lang-css"><span class="hljs-selector-class">.circle</span> {
    <span class="hljs-attribute">background-color</span>: transparent;
    <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> solid purple;
    <span class="hljs-attribute">height</span>:<span class="hljs-number">90px</span>;
    <span class="hljs-attribute">position</span>: relative;
    <span class="hljs-attribute">top</span>: -<span class="hljs-number">30px</span>;
    <span class="hljs-attribute">left</span>: <span class="hljs-number">120px</span>;
    <span class="hljs-attribute">border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">width</span>:<span class="hljs-number">90px</span>;
}
</code></pre>
<p>And here is how everything will look in our browser:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/testi4.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Final look</em></p>
<p>Alright, now we're ready to move on to the footer section. Then we'll learn how to make the site responsive.</p>
<h2 id="heading-how-to-build-the-footer-section">How to build the Footer Section</h2>
<p>The final part of our landing page building process is to create the footer section. The footer section will comprise some copyright text, three extra navigation links, and a group of social media icons from Font Awesome.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--2-.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Here is the HTML Markup for the footer section of our landing page:</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">footer</span>&gt;</span>
   <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"container grid-3"</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"copyright"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>Skilllz<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Copyright <span class="hljs-symbol">&amp;copy;</span> 2021<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">nav</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"links"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">ul</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"outline"</span>&gt;</span>Home<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"outline"</span>&gt;</span>Tutors<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
         <span class="hljs-tag">&lt;<span class="hljs-name">li</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"outline"</span>&gt;</span>Categories<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
       <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">nav</span>&gt;</span>
     <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"profiles"</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">em</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fab fa-twitter fa-2x"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">em</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fab fa-instagram fa-2x"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">em</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fab fa-facebook fa-2x"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
       <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">em</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"fab fa-whatsapp fa-2x"</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">em</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
     <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
   <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
 <span class="hljs-tag">&lt;/<span class="hljs-name">footer</span>&gt;</span>
</code></pre>
<p>The footer section will look unattractive without any styling:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/footer-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>No CSS yet</em></p>
<p>So let's change that.</p>
<h3 id="heading-how-to-style-the-footer">How to Style the Footer</h3>
<p>First, we need to set the background colour for the footer section (as well as the color for all links) to white, like this:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Add padding around the footer as well */</span>

<span class="hljs-selector-tag">footer</span> {
  <span class="hljs-attribute">background-color</span>: purple;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">20px</span> <span class="hljs-number">10px</span>;
}

<span class="hljs-comment">/* Sets all link texts to white and puts margin to the left and right */</span>

<span class="hljs-selector-tag">footer</span> <span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> <span class="hljs-number">10px</span>;
}
</code></pre>
<p>Now the footer will look like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/footer3.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Footer</em></p>
<p>If you check the first gif, you will notice that when I hover over any of the links inside of the footer, their color changes to a lighter shade of purple and a border also appears below them.</p>
<p>We can make this happen using a <code>:hover</code> selector:</p>
<pre><code class="lang-css"><span class="hljs-selector-tag">footer</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#9867C5</span>;
  <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">2px</span> solid <span class="hljs-number">#9867C5</span>;
}
</code></pre>
<p>That's all for the footer!</p>
<h2 id="heading-how-to-set-media-queries-to-make-the-page-responsive">How to Set Media Queries to Make the Page Responsive</h2>
<p>It's now time to make our landing page more responsive. When building a website, it is important to have in mind that users will be viewing the site from different devices. So it is imperative to make the site layout responsive to improve the user experience across multiple devices.</p>
<p>In our CSS, we will define media queries which set breakpoints for the various screen widths of different devices and map a set of CSS rules for each screen size.</p>
<h3 id="heading-how-to-design-for-tablets-and-smaller-screens">How to Design for Tablets and Smaller Screens</h3>
<p>First, we will optimize our site's layout for users viewing from a tablet. In our CSS, we define the following style:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Tablets and Under */</span>

<span class="hljs-keyword">@media</span>(max-width: <span class="hljs-number">768px</span>) {
  <span class="hljs-selector-class">.grid</span>,
  <span class="hljs-selector-class">.grid-3</span> {
    <span class="hljs-attribute">grid-template-columns</span>: <span class="hljs-number">1</span>fr;
  }
</code></pre>
<p>Initially we had set two columns for the <code>.grid</code> class and 3 columns for the <code>grid-3</code> class. Now, we want to make sure that all grid items take up just a single line.</p>
<p>As a result, our form and image which was formerly displayed side-by-side (horizontally) will now be displayed one after the other (vertically), like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/resp1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Next, we'll apply the following styles:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Align all text to the center. This will move all text, including form centrally */</span>

<span class="hljs-selector-class">.showcase</span> {
    <span class="hljs-attribute">height</span>: auto;
    <span class="hljs-attribute">text-align</span>: center;
  }

<span class="hljs-comment">/* This resets the width of the image container and adds margin space to the left */</span> 

<span class="hljs-selector-class">.image</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">600px</span>;
    <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">80px</span>;
  }

<span class="hljs-comment">/* Changes the service sections from side-by-side orientation to each taking its own line. Aligns text to the center */</span>

<span class="hljs-selector-class">.stats</span> <span class="hljs-selector-class">.flex</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
  }

  <span class="hljs-selector-class">.stats</span> {
    <span class="hljs-attribute">text-align</span>: center;
  }

<span class="hljs-comment">/* Makes sure each stat section does not exceed the width of parent */</span>

<span class="hljs-selector-class">.stat</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
    <span class="hljs-attribute">padding-right</span>: <span class="hljs-number">80px</span>;
  }

<span class="hljs-comment">/* (re)Moves the cirle to the center of the card */</span>

<span class="hljs-selector-class">.circle</span> {
      <span class="hljs-attribute">background-color</span>: transparent;
      <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> solid purple;
      <span class="hljs-attribute">height</span>:<span class="hljs-number">90px</span>;
      <span class="hljs-attribute">position</span>: relative;
      <span class="hljs-attribute">top</span>: -<span class="hljs-number">30px</span>;
      <span class="hljs-attribute">left</span>: <span class="hljs-number">270px</span>;
      <span class="hljs-attribute">border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">width</span>:<span class="hljs-number">90px</span>;
  }
</code></pre>
<p>And voilà! Our site now works on tablets and smaller screens.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--3--1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>The result</em></p>
<h3 id="heading-how-to-design-for-mobile-devices">How to Design for Mobile Devices</h3>
<p>Many people may view the site from a mobile device which typically has the smallest screen size out of all devices. Because of this, creating a layout for mobile-sized screens is very important.</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Mobile devices */</span>
<span class="hljs-keyword">@media</span>(max-width: <span class="hljs-number">500px</span>) {
  <span class="hljs-selector-class">.navbar</span> {
    <span class="hljs-attribute">height</span>: <span class="hljs-number">100px</span>;
  }
</code></pre>
<p>First, we increase the height of our navigation area. Since it will be viewed from a smaller screen, we want the area more accentuated for the user.</p>
<p>Then, we define the following styles:</p>
<pre><code class="lang-css"><span class="hljs-comment">/* Changes the alignment. The logo title stays at the top, the nav links will be below it */</span>

<span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-class">.flex</span> {
    <span class="hljs-attribute">flex-direction</span>: column;
  }


<span class="hljs-comment">/* When hovered on, retain white color and change border to black */</span>

  <span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">a</span><span class="hljs-selector-pseudo">:hover</span> {
    <span class="hljs-attribute">color</span>: white;
    <span class="hljs-attribute">border-bottom</span>: <span class="hljs-number">2px</span> solid black;
  }


<span class="hljs-comment">/* Set light purple background on nav links, make it a bit round and add some spacing */</span>

  <span class="hljs-selector-class">.navbar</span> <span class="hljs-selector-tag">ul</span> {
    <span class="hljs-attribute">background</span>: <span class="hljs-number">#9867C5</span>;
    <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">5px</span>;
    <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span> <span class="hljs-number">0</span>;
  }


<span class="hljs-comment">/* Align text to center */</span>

  <span class="hljs-selector-class">.showcase</span> {
    <span class="hljs-attribute">height</span>: auto;
    <span class="hljs-attribute">text-align</span>: center;
  }


<span class="hljs-comment">/* Reduce font size */</span>

<span class="hljs-selector-class">.little-info</span> {
    <span class="hljs-attribute">font-size</span>: <span class="hljs-number">13px</span>;
  }


<span class="hljs-comment">/* Reduce image width */</span>

  <span class="hljs-selector-class">.image</span> {
    <span class="hljs-attribute">width</span>: <span class="hljs-number">350px</span>;
    <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">70px</span>;
  }

  <span class="hljs-selector-class">.stat</span> {
    <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">40px</span>;
  }


<span class="hljs-comment">/* Move circle once again */</span>

<span class="hljs-selector-class">.circle</span> {
      <span class="hljs-attribute">background-color</span>: transparent;
      <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> solid purple;
      <span class="hljs-attribute">height</span>:<span class="hljs-number">90px</span>;
      <span class="hljs-attribute">position</span>: relative;
      <span class="hljs-attribute">top</span>: -<span class="hljs-number">30px</span>;
      <span class="hljs-attribute">left</span>: <span class="hljs-number">150px</span>;
      <span class="hljs-attribute">border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">50%</span>;
      <span class="hljs-attribute">width</span>:<span class="hljs-number">90px</span>;
  }
}
</code></pre>
<p>And voilà!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/ezgif.com-gif-maker--4--1.gif" alt="Image" width="600" height="400" loading="lazy"></p>
<h2 id="heading-wrapping-up"><strong>Wrapping Up</strong></h2>
<p>FlexBox and Grid alignment are very powerful tools for laying out a web page however you want it to look.</p>
<p>Responsive web design is arguably one of the most important design principles in web development. We have to consider the fact that our site will be viewed from various kinds of devices with different screen resolutions. Optimizing our site's layout for different screens will improve the user experience.</p>
<p>In this tutorial, we have designed a simple landing page using CSS Flexbox, Grid, and many other CSS properties. We have also made the page look good on Tablets and Mobile Screens.</p>
<p>The full code for this project can be gotten from this <a target="_blank" href="https://github.com/KingsleyUbah/Skilllz">GitHub repository</a>.</p>
<p>I hope you learned something useful from this tutorial. If you have any suggestions, reach out to me on <a target="_blank" href="https://twitter.com/UbahTheBuilder">Twitter</a>. You can also visit my <a target="_blank" href="https://ubahthebuilder.tech/">blog</a> for posts like this.</p>
<p>Thanks for following along and see you soon.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Array.some() Tutorial – How to Iterate Through Elements in an Array ]]>
                </title>
                <description>
                    <![CDATA[ When you're working with an array in JavaScript, sometimes you might just want to check if at least one element inside that array passes a test. And you might not care about any other subsequent matches. In such a case, you should use the some() Java... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-array-some-tutorial-how-to-iterate-through-elements-in-an-array/</link>
                <guid isPermaLink="false">66d4618d33b83c4378a51857</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Tue, 07 Sep 2021 22:14:16 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/array-some.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're working with an array in JavaScript, sometimes you might just want to check if <strong>at least one</strong> element inside that array passes a test. And you might not care about any other subsequent matches.</p>
<p>In such a case, you should use the <code>some()</code> JavaScript method. So let's see how it works.</p>
<h2 id="heading-how-to-use-the-arraysome-javascript-method">How to Use the Array.some() JavaScript Method</h2>
<p>The <code>some()</code> method is an <code>Array.propotype</code> (built-in) method which takes in a callback function and will test that function on each iteration against the current item.</p>
<p>If any elements in the array pass the test specified in the callback, the method stops iterating and returns <code>true</code>. If no elements pass the test, the method returns <code>false</code>.</p>
<p>The method takes in three parameters:</p>
<ul>
<li><p><code>currentItem</code>: This is the element inside of the array which is currently being iterated over</p>
</li>
<li><p><code>index</code>: This is the index position of the <code>currentItem</code> inside of the array</p>
</li>
<li><p><code>array</code>: This represents the array collection to which the <code>some()</code> method is bound</p>
</li>
</ul>
<p>A simple way to understand the main idea behind <code>Array.some()</code> method is to consider one of our biggest propensities as humans: <strong>generalization</strong>. People tend to make generalizations based on just one single experience or perception.</p>
<p>For example, if a certain person from a certain place behaves a certain way, a lot of people will assume that everyone from that place also behaves the same way. Even though such an assumption was based on just one single experience.</p>
<p>The <code>some()</code> method essentially <strong>makes up its mind</strong> the moment it finds a match, and returns <code>true</code>.</p>
<h2 id="heading-how-to-use-arraysome-in-your-javascript">How to Use Array.some() in Your JavaScript</h2>
<p>In the following examples, we will be taking a practical look at how we can use the <code>some()</code> method inside our JavaScript.</p>
<h3 id="heading-how-to-test-for-at-least-one-match-with-some">How to test for at least one match with <code>some()</code></h3>
<p>In this example, we will check if there is at least one male inside of the <code>people</code> array</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ben"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Male"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Danny"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  }

]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isThereMale</span>(<span class="hljs-params">person</span>) </span>{
    <span class="hljs-keyword">return</span> person.sex === <span class="hljs-string">"Male"</span>
}

<span class="hljs-built_in">console</span>.log(people.some(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> isThereMale(person)) <span class="hljs-comment">// true</span>
</code></pre>
<p>Since a male actually exists, the <code>some()</code> method returns true.</p>
<p>Even if we were to define two males inside the array, the method will still return <code>true</code>. The method doesn't care if there is a second male or not, all it cares about is the first one.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ben"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Male"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Danny"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ethan"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Male"</span>
  }

]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isThereMale</span>(<span class="hljs-params">person</span>) </span>{
    <span class="hljs-keyword">return</span> person.sex === <span class="hljs-string">"Male"</span>
}

<span class="hljs-built_in">console</span>.log(people.some(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> isThereMale(person)) <span class="hljs-comment">// true</span>
</code></pre>
<p>If all items within an array fail the callback test, the <code>some()</code> method will return <code>false</code>.</p>
<p>In this example, since there is no male inside of our people array, <code>false</code> will be returned instead:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Bella"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Danny"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ella"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  }

]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isThereMale</span>(<span class="hljs-params">person</span>) </span>{
        <span class="hljs-keyword">return</span> person.sex === <span class="hljs-string">"Male"</span>
}

<span class="hljs-built_in">console</span>.log(people.some(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> isThereMale(person))) <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="heading-how-to-use-the-index-parameter-with-some">How to use the index parameter with <code>some()</code></h3>
<p>The callback function defined inside <code>some()</code> can access the index property of every item being iterated over. The index is just a numerical value that uniquely identifies the position of each and every element inside an array. That way, you can refer to any element in the array by its index.</p>
<p>Here, we use the index value to construct a message which we log onto the console:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ben"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Male"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Danny"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Female"</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ethan"</span>,
    <span class="hljs-attr">sex</span>: <span class="hljs-string">"Male"</span>
  }

]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">isThereMale</span>(<span class="hljs-params">person, index</span>) </span>{
        <span class="hljs-keyword">if</span> (person.sex === <span class="hljs-string">"Male"</span>) <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`No <span class="hljs-subst">${index+<span class="hljs-number">1</span>}</span>, which is <span class="hljs-subst">${person.name}</span>, is a Male`</span>)
        <span class="hljs-keyword">return</span> person.sex === <span class="hljs-string">"Male"</span>
}

<span class="hljs-built_in">console</span>.log(people.some(<span class="hljs-function">(<span class="hljs-params">person, index</span>) =&gt;</span> isThereMale(person, index)))

<span class="hljs-comment">/* 
"No 2, which is Ben, is a Male"

true
*/</span>
</code></pre>
<h2 id="heading-how-to-use-the-context-object-with-some">How to Use the Context Object with <code>some()</code></h2>
<p>In addition to the callback function, <code>some()</code> can also take in a context object.</p>
<pre><code class="lang-js">some(callbackFn, contextObj)
</code></pre>
<p>We can then refer to the object from inside the <strong>callback</strong> function on each iteration, using <code>this</code> as a reference. This allows us to access any properties or methods defined inside the context object.</p>
<h3 id="heading-example-of-using-the-context-object-with-some">Example of using the context object with <code>some()</code></h3>
<p>In this example, we are looking to check if at least one person in the people array is a <strong>tricenarian</strong>. That is, the person's age falls between 30 and 39.</p>
<p>We can define the rule inside the <code>conditions</code> object and then access it from inside the callback function using the <code>this.property</code> notation. We then perform a check to determine if at least one person matches the criteria.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">20</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ben"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">35</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">8</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Danny"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">17</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ethan"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">28</span>
  }

]

<span class="hljs-keyword">let</span> conditions = {
    <span class="hljs-attr">lowerAge</span>: <span class="hljs-number">30</span>,
  <span class="hljs-attr">upperAge</span>: <span class="hljs-number">39</span>
}



<span class="hljs-built_in">console</span>.log(people.some(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">person</span>) </span>{
    <span class="hljs-keyword">return</span> person.age &gt;= <span class="hljs-built_in">this</span>.lowerAge &amp;&amp; person.age &lt;= <span class="hljs-built_in">this</span>.upperAge
}, conditions)) <span class="hljs-comment">// true</span>
</code></pre>
<p>Since one person (Ben) falls in that range, <code>some()</code> will return <code>true</code>.</p>
<h2 id="heading-wrapping-up"><strong>Wrapping Up</strong></h2>
<p>The <code>some()</code> method is an <code>Array.prototype</code> method which takes in a callback function and calls that function for every item within the bound array.</p>
<p>When an item passes the callback test, the method will return <code>true</code> and stop the loop. Otherwise, it returns <code>false</code>.</p>
<p>In addition to the callback function, the <code>some()</code> method can also take in a context object as the second argument. This will enable you to access any of its properties from the callback function using <code>this</code>.</p>
<p>I hope you got something useful from this article.</p>
<p><strong>I</strong>f you want to learn more about Web Development, feel free to visit my**** <a target="_blank" href="https://ubahthebuilder.tech/the-ultimate-tutorial-on-javascript-dom-js-dom-with-examples">blog**.**</a></p>
<p>Thank you for reading and see you soon.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Array.find() Tutorial – How to Iterate Through Elements in an Array ]]>
                </title>
                <description>
                    <![CDATA[ When you're working with an array collection, sometimes you'll only need to find out if an item exists in the array so you can retrieve it. And you won't care how many other items (if any) exist within the same array. Well, we can use the find() meth... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-array-find-tutorial-how-to-iterate-through-elements-in-an-array/</link>
                <guid isPermaLink="false">66d461893dce891ac3a96848</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Wed, 01 Sep 2021 16:41:26 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/find-method-js.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When you're working with an array collection, sometimes you'll only need to find out if an item exists in the array so you can retrieve it. And you won't care how many other items (if any) exist within the same array.</p>
<p>Well, we can use the <code>find()</code> method to do just that.</p>
<h2 id="heading-how-the-arrayfind-method-works">How the Array.find() Method Works</h2>
<p>The <code>find()</code> method is an <code>Array.prototype</code> (aka built-in) method which takes in a callback function and calls that function for every item it iterates over inside of the array it is bound to.</p>
<p>When it finds a match (in other words, the callback function returns <code>true</code>), the method returns that particular array item and immediately breaks the loop. So the <code>find()</code> method returns the first element inside an array which satisfies the callback function.</p>
<p>The callback function can take in the following parameters:</p>
<ul>
<li><p><code>currentItem</code>: This is the element in the array which is currently being iterated over.</p>
</li>
<li><p><code>index</code>: This is the index position of the <code>currentItem</code> inside the array.</p>
</li>
<li><p><code>array</code>: This represents the target array along with all its items.</p>
</li>
</ul>
<h2 id="heading-how-to-use-the-find-method-in-javascript"><strong>How to Use the</strong> <code>find()</code> Method in JavaScript</h2>
<p>In the following examples, I will demonstrate how you can use the <code>find()</code> method to retrieve the first item from an array which matches a specified condition in JavaScript.</p>
<h3 id="heading-how-to-get-a-single-item-with-find">How to get a single item with find()</h3>
<p>Let's assume you have a dog which goes missing. You report it to the relevant authorities and they bring together a group of recovered dogs.</p>
<p>To be able to find your dog, you need to provide unique information about him. For example, the breed of your dog (a Chihuahua) might be used to identify it.</p>
<p>We can express this scenario in JavaScript using an array collection. The array called <code>foundDogs</code> will contain all the names of the recovered dogs as well as their respective breeds. And we'll use the <code>find()</code> method to find the dog which is a Chihuahua from inside the array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> foundDogs = [{
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Beagle"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Chihuahua"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"yellow"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Pug"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>
  },
]

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findMyDog</span>(<span class="hljs-params">dog</span>) </span>{
  <span class="hljs-keyword">return</span> dog.breed === <span class="hljs-string">"Chihuahua"</span>
}

<span class="hljs-keyword">let</span> myDog = foundDogs.find(<span class="hljs-function"><span class="hljs-params">dog</span> =&gt;</span> findMyDog(dog));

<span class="hljs-built_in">console</span>.log(myDog);


<span class="hljs-comment">/*

{
  breed: "Chihuahua",
  color: "yellow"
}

*/</span>
</code></pre>
<p>The find method stops iterating when a match is found.</p>
<p>There is something very important to remember about <code>find()</code>: it stops executing once the callback function returns a <code>true</code> statement.</p>
<p>To illustrate this, we will once again use the missing dog example. This time, we will assume that two Chihuahuas were found.</p>
<p>But the <code>find()</code> method will only return the first instance of Chihuahua it discovers within the array. Any other instance will then be subsequently ignored.</p>
<p>We can also easily see this by logging the index position of that item into the console:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> foundDogs = [{
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Beagle"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Chihuahua"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"yellow"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Pug"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Chihuahua"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"yellow"</span>
  }
]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findMyDog</span>(<span class="hljs-params">dog, index</span>) </span>{
    <span class="hljs-keyword">if</span> (dog.breed === <span class="hljs-string">"Chihuahua"</span>) <span class="hljs-built_in">console</span>.log(index);
  <span class="hljs-keyword">return</span> dog.breed === <span class="hljs-string">"Chihuahua"</span>
}


<span class="hljs-keyword">let</span> myDog = foundDogs.find(<span class="hljs-function">(<span class="hljs-params">dog, index</span>) =&gt;</span> findMyDog(dog, index));


<span class="hljs-built_in">console</span>.log(myDog);

<span class="hljs-comment">/* 
1

{
  breed: "Chihuahua",
  color: "yellow"
}

*/</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/findreturns1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-use-a-destructuring-assignment">How to use a Destructuring Assignment</h3>
<p>You can make your code more concise by combining both the destructuring assignment and an arrow function expression.</p>
<p>We'll use destructuring to extract only the name property from the object which we then pass in as a parameter to the callback function.</p>
<p>We'll get the same result:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> foundDogs = [{
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Beagle"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"white"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Chihuahua"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"yellow"</span>
  },

  {
    <span class="hljs-attr">breed</span>: <span class="hljs-string">"Pug"</span>,
    <span class="hljs-attr">color</span>: <span class="hljs-string">"black"</span>
  },
]




<span class="hljs-keyword">let</span> myDog = foundDogs.find(<span class="hljs-function">(<span class="hljs-params">{breed}</span>) =&gt;</span> breed === <span class="hljs-string">"Chihuahua"</span>);

<span class="hljs-built_in">console</span>.log(myDog);

<span class="hljs-comment">/*

{
  breed: "Chihuahua",
  color: "yellow"
}

*/</span>
</code></pre>
<h3 id="heading-how-to-find-an-item-by-its-index">How to find an item by its index</h3>
<p>In this example, we will be finding and returning the spot belonging to 'David' from inside the array using its unique index value. This demonstrates one way we can use the <code>index</code> property inside of our <code>callback</code> function with the <code>find()</code> method:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> reservedPositions = [{
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Anna"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">24</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Beth"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">22</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Cara"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"David"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>
  },

  {
    <span class="hljs-attr">name</span>: <span class="hljs-string">"Ethan"</span>,
    <span class="hljs-attr">age</span>: <span class="hljs-number">26</span>
  }
]


<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">findByIndex</span>(<span class="hljs-params">person, index</span>) </span>{
  <span class="hljs-keyword">return</span> index === <span class="hljs-number">3</span>
}


<span class="hljs-keyword">let</span> myPosition = reservedPositions.find(<span class="hljs-function">(<span class="hljs-params">person, index</span>) =&gt;</span> findByIndex(person, index));

<span class="hljs-built_in">console</span>.log(myPosition);

<span class="hljs-comment">/*
{
  age: 30,
  name: "David"
}
*/</span>
</code></pre>
<h2 id="heading-you-can-use-the-context-object-with-find">You Can Use the Context Object with find()</h2>
<p>In addition to the callback function, the <code>find()</code> method can also take in a context object.</p>
<pre><code class="lang-js">find(callback, contextObj)
</code></pre>
<p>We can then refer to this object from inside the <strong>callback</strong> function on each iteration, using the <code>this</code> keyword as a reference. This allows us to access any properties or methods defined inside of the context object.</p>
<h3 id="heading-how-to-use-the-context-object-with-find">How to use the context object with find()</h3>
<p>Let's say we have an array of job applications and want to select just the first applicant who meets all of the criteria.</p>
<p>All criteria is defined inside a context object called <code>criteria</code> and that object is subsequently passed as a second parameter into the <code>find()</code> method. Then, from inside the callback function, we access the object to check if an applicant matches all of the criteria specified there.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> applicants = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"aaron"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">18</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">66</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>, <span class="hljs-attr">yrsOfExperience</span>:  <span class="hljs-number">0</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"cara"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">4</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">22</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">3</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">16</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"ella"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"fin"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">0</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">16</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"george"</span>, <span class="hljs-attr">yrsOfExperience</span>: <span class="hljs-number">6</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">28</span>},
]

<span class="hljs-keyword">let</span> criteria = {
    <span class="hljs-attr">minimumExperience</span>: <span class="hljs-number">3</span>,
  <span class="hljs-attr">lowerAge</span>: <span class="hljs-number">18</span>,
  <span class="hljs-attr">upperAge</span>: <span class="hljs-number">65</span>
}


<span class="hljs-keyword">let</span> luckyApplicant = applicants.find(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">applicant</span>) </span>{
    <span class="hljs-keyword">return</span> applicant.yrsOfExperience &gt;= <span class="hljs-built_in">this</span>.minimumExperience &amp;&amp; applicant.age &lt;= <span class="hljs-built_in">this</span>.upperAge
  &amp;&amp; applicant.age &gt;= <span class="hljs-built_in">this</span>.lowerAge ;
}, criteria)

<span class="hljs-built_in">console</span>.log(luckyApplicant);

<span class="hljs-comment">/*
{
  age: 22,
  name: "cara",
  yrsOfExperience: 4
}
*/</span>
</code></pre>
<p>Technically, three applicants (Cara, Ella and George) all qualify based on the criteria. In other words, the three of them are at least 18 years old, not older than 65, and have at least 3 years of working experience.</p>
<p>However since the <code>find()</code> method always returns ONLY the first instance which evaluates to true, the other two will be ignored and the loop will be broken.</p>
<h2 id="heading-wrapping-up"><strong>Wrapping Up</strong></h2>
<p>The <code>find()</code> method is an <code>Array.prototype</code> method which takes in a callback function and calls that function for every item within the bound array.</p>
<p>When the callback function evaluates to <code>true</code>, the method returns the current item and breaks the loop. It returns just the first match – any other matches present inside of the array will be ignored.</p>
<p>In addition to the callback function, the <code>find()</code> method can also take in a context object as the second argument. This will enable you access any of its properties from the callback function using <code>this</code>.</p>
<p>I hope you got something useful from this article.</p>
<p><strong>I</strong>f you want to learn more about Web Development, feel free to visit my**** <a target="_blank" href="https://ubahthebuilder.tech/the-ultimate-tutorial-on-javascript-dom-js-dom-with-examples">blog**.**</a></p>
<p>Thank you for reading and see you soon.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ React Tutorial – Build a Movie List Generator with React and HarperDB ]]>
                </title>
                <description>
                    <![CDATA[ In this tutorial, we are going to be building a simple movie generator which automatically generates a new movie every 40 seconds. It will also contain a button called “Generate New Movie” to display another movie on demand. This app will display a m... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/react-tutorial-build-a-movie-list-generator-with-react-and-harperdb/</link>
                <guid isPermaLink="false">66d46196787a2a3b05af4418</guid>
                
                    <category>
                        <![CDATA[ database ]]>
                    </category>
                
                    <category>
                        <![CDATA[ React ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Applications ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Mon, 30 Aug 2021 19:07:30 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/HarperDBMovieCover.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In this tutorial, we are going to be building a simple movie generator which automatically generates a new movie every 40 seconds. It will also contain a button called “Generate New Movie” to display another movie on demand.</p>
<p>This app will display a movie along with its title, release date, fan rating, time duration, a short description, star casts, name of directors and finally a button which links to the movie’s IMDb page.</p>
<p>You can access the complete code for this project from its <a target="_blank" href="https://github.com/KingsleyUbah/harperdb-movie-generator">Github</a> repository.</p>
<h2 id="heading-how-are-we-going-to-build-this">How Are We Going to Build This?</h2>
<p>As it is with most other web apps, this app will consist of both a front end and a back end.</p>
<p>The front end is the part the user sees and interacts with. In our app, our front end will be made up of the movie's cover image, its information, and a button which will link to the movie’s IMDb page.</p>
<p>The back end is where our movie data – such as the movie title, description, actors, picture, and so on – will be coming from.</p>
<p>The app is going to automatically generate a new random movie every 40 seconds.</p>
<p>We'll build the front end of our app using React. React is a front end JavaScript library used for building reusable UI components such as buttons, navigation menus, images, cards and more.</p>
<p>We will also style our components using pure CSS.</p>
<h3 id="heading-how-well-build-the-back-end">How We'll Build the Back End</h3>
<p>The back end of a website typically contains a database, which is a program you use to store and manage data. The database also has to be accessible via an API so that our front end can access the data and display it to the user.</p>
<p>To accomplish this, we'll use an interesting and fun tool called <strong>HarperDB</strong>.</p>
<h3 id="heading-what-is-harperdb"><strong>What is HarperDB?</strong></h3>
<p>HarperDB is database and data management software that's incredibly fast – it's even been proven to be 37 times faster than MongoDB.</p>
<p>A database’s speed refers to how fast it can read and write data into its records as well as make computations on such data.</p>
<p>HarperDB is also incredibly flexible. It allows you do the following:</p>
<ul>
<li><p>Make queries to a single endpoint</p>
</li>
<li><p>Use both SQL and NoSQL to query your database</p>
</li>
<li><p>Upload data in JSON and with SQL queries.</p>
</li>
</ul>
<p>If you are working with lots of data, you can import it all in one step in a CSV file. Pretty neat!</p>
<p>You don’t have to define the data types for your data, as HarperDB dynamically does it for you. Not to mention their simple interface for managing your cloud instance without any hassle.</p>
<p>As I said, very flexible.</p>
<h2 id="heading-prerequisites-for-this-tutorial">Prerequisites for this Tutorial</h2>
<p>To build this app, I'll assume you have some basic knowledge of the following languages and tools:</p>
<p><strong>Npm or any other package managers</strong>: We'll need this to install React and a React HarperDB hook called <a target="_blank" href="https://www.npmjs.com/package/use-harperdb">use-harperdb</a> to your project.</p>
<p>NPM stands for Node Package Manager. It's a tool which connects your local project to the npm registry, where millions of public code packages, such as React and <code>useharperdb</code>, are hosted. It also helps you manage this code, once it's installed.</p>
<p>Make sure to have a Node version of at least 12.xx installed on your machine. You can check your node version with this command: <code>node -v</code></p>
<p><strong>SQL</strong>: In this project, we are only going to be using one or two basic queries, so don’t worry if you don’t know much about SQL.</p>
<p>SQL stands for Structured Query Language. It is a popular language used in querying relational databases. We will use it in our hook to query our HarperDB cloud instance for data.</p>
<p><strong>React</strong>: Our user interface is going to be built with React. If you know JavaScript, then learning React is relatively easy.</p>
<p><strong>A HarperDB account</strong>: If you don’t have a HarperDB account, you will need to <a target="_blank" href="https://studio.harperdb.io/sign-up">create one</a>. Don’t worry, it's completely free. I will show you how to create an account below.</p>
<p><strong>And finally, CSS:</strong> we'll use a little CSS to style our elements.</p>
<h2 id="heading-what-are-react-hooks">What are React Hooks?</h2>
<p>In the past, to work with data in a React component, you had to define the component as a class component. This changed when React introduced hooks.</p>
<p>Simply put, hooks are functions which allow you to work with data in a non-class (that is, functional) React component.</p>
<p>Thanks to this, you don’t have to define a React class component just to manage state data inside of it.</p>
<p>The <code>use-harperdb</code> hook allows you to hook your app into your cloud database instance to obtain data. Think of it as a bridge between your React app (front end) and the HarperDB database (back end).</p>
<h2 id="heading-how-to-set-up-the-database">How to Set Up the Database</h2>
<p>HarperDB is a flexible database, as I mentioned before. It allows you to use its services either by setting up your own local HarperDB server or using the serverless architecture.</p>
<p>In this project, we will be using the serverless architecture. This means that we will not be implementing a server (that is, the back end) on our local machine. Instead, we will leverage HarperDB’s cloud infrastructure to manage our movie data and make it available for our app.</p>
<h3 id="heading-set-up-the-harperdb-cloud-instance">Set Up the HarperDB Cloud Instance</h3>
<p>First, I am going to assume that you have created your free account like I asked earlier. If you haven’t, go ahead and <a target="_blank" href="https://studio.harperdb.io/sign-up">sign up</a>.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/HarperDB-Start-for-free-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Sign Up for HarperDB</em></p>
<p>You will be asked to provide your name, a valid e-mail, and a sub-domain name for your cloud instance. With it, HarperDB will create a sub-domain name for you.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB--password-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Make sure to choose a strong password</em></p>
<p>Next, we'll create a cloud instance:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB---instance-info-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Fill in your details</em></p>
<p>Here, you will be asked to add an Instance name. Don’t worry, you can name it whatever you can easily remember, but it is best to make it descriptive.</p>
<p>To create your Instance URL, which you will need on your app when querying for data, HarperDB will combine your Instance Name with your Subdomain Name. You will also be prompted to set your Instance credentials (username and password).</p>
<p>Next, we select the Instance Specifications. For the sake of this tutorial, we will go with the free plans. Also, you will need to pick a region for your instance.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB----more-instance-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Choosing the free plans</em></p>
<p>Click on “Confirm Instance Details” and you will be moved to a page which contains all your instance’s information. Now, copy your instance URL, your username, and your password and save it somewhere as you will need it later.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB---confirm-instance-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Save your URL, username and password</em></p>
<p>When you're done, click the “Add Instance” button. You will be moved to a page which shows your instance card. Your instance will need some time to set up initially before you can use it, but we can do a few things while we wait.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB---creating-instance-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Setting it up</em></p>
<h2 id="heading-how-to-set-up-the-react-app">How to Set up the React App</h2>
<p>While our cloud instance is still being set up, we can take the opportunity to set up the project directory for our app.</p>
<p>First, we initialize our project by running the following command on any command terminal:</p>
<pre><code class="lang-js">npx create-react-app harperdb-movies-generator
</code></pre>
<p>This command will create a folder called harperdb-movies-app as well as install all of the dependencies we need for our project. This includes React and the ReactDOM, so we don’t have to do it manually.</p>
<p>Next we are going to run the command to bring the use-harperdb hook into our project. This hook will help us connect to our cloud instance. To install it, we run the following command on our command line:</p>
<pre><code class="lang-js">npm install use-harperdb
</code></pre>
<p><strong>That’s all for the setup!</strong></p>
<h3 id="heading-how-to-integrate-harperdb-to-your-react-app">How to Integrate HarperDB to your React App</h3>
<p>Now that the use-harperdb hook has been installed, we have to do one more thing to be able to access data from your database and make CRUD operations on it: We have to connect your app to your cloud instance. We will do this with the HarperDBProvider.</p>
<p>Before getting into that, we have to do something first. When building a CRUD application, it is not a good practice to expose private credentials such as our API keys to other people, especially if we are intending on pushing the code to a public repo like GitHub.</p>
<p>To protect any sensitive credentials, we will need to store them as environmental variables. This is just a file where we store sensitive credentials such as our passwords, API keys, and in our current case, our cloud instance credentials (URL, username, and password).</p>
<p>Create an <code>.env</code> in the root of your directory. You create this file in your code editor, right click on the root directory (harperdb-movie-generator) and select the “create new file” option.</p>
<p>Name this file <code>.env</code> and press enter. This will create a .env file inside of harperdb-movie-generator. After this, define the following variables:</p>
<pre><code class="lang-c">REACT_APP_DB_URL=**
REACT_APP_USER=**
REACT_APP_PASSWORD=**
</code></pre>
<p>Make sure you use the same format and pass in the correct details about your own cloud instance in place of the double asterisks. Fill in your Instance URL, your Instance Username, and your Instance Password, which I earlier told you to save somewhere.</p>
<p>React will read all environmental variables that use the REACT_APP as prefix, and will then dynamically pass in the value where needed.</p>
<p>With the .env file created, our next action will be to wrap our entire React app inside the imported HarperDBProvider</p>
<p>HarperDBProvider will ensure that our app has the context of the HarperDB database.</p>
<p>To wrap our React App inside the provider, we'll go over to index.js inside our project, import the provider, and securely pass in those environmental variables into the provider. This lets it know which instance to connect our front end to:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">'react-dom'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./index.css'</span>;
<span class="hljs-keyword">import</span> App <span class="hljs-keyword">from</span> <span class="hljs-string">'./App'</span>;
<span class="hljs-keyword">import</span> { HarperDBProvider } <span class="hljs-keyword">from</span> <span class="hljs-string">'use-harperdb'</span>;


ReactDOM.render(
  <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">React.StrictMode</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">HarperDBProvider</span>
    <span class="hljs-attr">url</span>=<span class="hljs-string">{process.env.REACT_APP_DB_URL}</span>
    <span class="hljs-attr">user</span>=<span class="hljs-string">{process.env.REACT_APP_USER}</span>
    <span class="hljs-attr">password</span>=<span class="hljs-string">{process.env.REACT_APP_PASSWORD}</span>
    &gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">App</span> /&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">HarperDBProvider</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">React.StrictMode</span>&gt;</span></span>,
  <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'root'</span>)
);
</code></pre>
<h2 id="heading-how-to-populate-our-database-with-data">How to Populate Our Database with Data</h2>
<p>If you remember, we left the cloud instance while it was still being set up. By now, we should have our instance all set and ready to serve data. In that case, you will see the OK status on your instance:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB--okay-2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Instance now set</em></p>
<p>Your cloud instance should be good-to-go with your front end hooked up to your instance as well. However, the front end will be useless if it doesn’t have any data (that is, movies) to display to the user.</p>
<p>So first we need to populate our database with data.</p>
<p>But before that, we will need to create a schema for our movie data. You can think of a schema as a collection of tables in our database. I simply call my own schema "collection":</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB---collection-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Creating our schema and a table inside of it</em></p>
<p>Next, we'll create our table. I'll called mine "movie". A table will consist of records of individual movies.</p>
<p>Each movie record must have a hash_attribiute. A hash_attribute is simply a column with unique keys which identifies that particular row of data and distinguishes it from the next row. We are simply using the “id” column as our hash_attribute.</p>
<p>Since we are creating an app with more than one movie, our table will consist of more than one row of movies (that is, records of data). Also, since each movie has many properties such as title, year, release date ,and so on, it will have more than one field of information.</p>
<p>You can upload the movies one by one with a single JSON object or upload a full collection of movies with an array of JSON objects.</p>
<p>HarperDB allows you to upload data in three main ways:</p>
<ol>
<li><p>By making SQL or NoSQL queries to create data on our database.</p>
</li>
<li><p>By defining a single JSON object (for only one record) and an array of JSON data (for multiple records)</p>
</li>
<li><p>By importing and loading data with a CSV file</p>
</li>
</ol>
<p>To upload a single movie's data, we create a JSON object which contains all the movie information. Here is an example of the JSON data:</p>
<pre><code class="lang-js">{
  <span class="hljs-attr">cover</span>: <span class="hljs-string">'https://res.cloudinary.com/ubahthebuilder/image/upload/v1627129180/avengers_endgame_ilqzqj.png'</span>,
  <span class="hljs-attr">date</span>: <span class="hljs-number">2017</span>,
  <span class="hljs-attr">description</span>: <span class="hljs-string">'After the devastating events of Avengers: Infinity War (2018), the universe is in ruins. With the help of remaining allies, the Avengers assemble once more in order to reverse Thanos actions and restore balance to the universe.'</span>,
  <span class="hljs-attr">directors</span>: [
    <span class="hljs-string">'Anthony Russo'</span>,
    <span class="hljs-string">'Joe Russo'</span>
  ],
  <span class="hljs-attr">genres</span>: [
    <span class="hljs-string">'Action'</span>,
    <span class="hljs-string">'Adventure'</span>,
    <span class="hljs-string">'Drama'</span>
  ],
  <span class="hljs-attr">hours</span>: <span class="hljs-number">3</span>,
  <span class="hljs-attr">id</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">minutes</span>: <span class="hljs-number">1</span>,
  <span class="hljs-attr">rating</span>: <span class="hljs-number">8.4</span>,
  <span class="hljs-attr">stars</span>: [
    <span class="hljs-string">'Robert Downey'</span>,
    <span class="hljs-string">'Chris Evans'</span>,
    <span class="hljs-string">'Mark Ruffalo'</span>
  ],
  <span class="hljs-attr">title</span>: <span class="hljs-string">'Avengers: End Game'</span>,
  <span class="hljs-attr">website</span>: <span class="hljs-string">'https://www.imdb.com/title/tt4154796/'</span>,
  <span class="hljs-attr">writers</span>: [
    <span class="hljs-string">'Christopher Markus'</span>,
    <span class="hljs-string">'Stephen McFeely'</span>
  ]
}
</code></pre>
<p>Navigate to the movie table inside the collection and click on the + sign at the top right corner of the page, which is highlighted in the following image:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/addmovie.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Adding a new movie record into our table</em></p>
<p>Copy the previously defined JSON object and paste it into the space provided, replacing everything there for formatting reasons. Click the green button to save the information into the movies table.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/adddata.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Paste in the JSON object</em></p>
<p>Once we are done with uploading, our table should look something like this:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Studio-HarperDB-FULL-TABLE-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Eight Movies inside of our table</em></p>
<p>Note that you can make use of the data from this project’s <a target="_blank" href="https://github.com/KingsleyUbah/harperdb-movie-generator/blob/master/data/movies.json">GitHub repository</a> to insert multiple records of movies at once.</p>
<h2 id="heading-how-to-build-up-our-ui-and-query-the-database">How to Build Up Our UI and Query the Database</h2>
<p>Now that the data is ready, we need to display it on our front end for the user to see and interact with.</p>
<p>First, we need to alter our app.js file:</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> <span class="hljs-string">'./App.css'</span>;
<span class="hljs-keyword">import</span> Movie <span class="hljs-keyword">from</span> <span class="hljs-string">'./components/Movie'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">App</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">return</span> (
    <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"App"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"main-container"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">header</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"heading"</span>&gt;</span>Movie List<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">h3</span>&gt;</span> A Simple Movie Generator built with React and HarperDB<span class="hljs-tag">&lt;/<span class="hljs-name">h3</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">header</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">Movie</span> /&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  );
}

<span class="hljs-keyword">export</span> <span class="hljs-keyword">default</span> App;
</code></pre>
<p>This will be the top level component in our project.</p>
<p>Next, we'll import the React and React DOM libraries as well as the stylesheet App.css for our entire app.</p>
<p>Next, in the App.css file we define our app component which returns the Header elements as well as the Movie component.</p>
<p>Here is the style for our entire app:</p>
<pre><code class="lang-css"><span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Lato:wght@300&amp;display=swap'</span>);
<span class="hljs-keyword">@import</span> url(<span class="hljs-string">'https://fonts.googleapis.com/css2?family=Roboto:wght@500&amp;display=swap'</span>);

<span class="hljs-comment">/* Base Styles */</span>

<span class="hljs-selector-tag">body</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"lato"</span>, sans-serif;
  <span class="hljs-attribute">color</span>: white;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#082032</span>;
}

<span class="hljs-selector-tag">a</span> {
  <span class="hljs-attribute">color</span>: black;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"roboto"</span>, sans-serif;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">text-decoration</span>: none;
  <span class="hljs-attribute">display</span>: inline-block;
}


<span class="hljs-selector-tag">h1</span> {
  <span class="hljs-attribute">text-align</span>: center;
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"roboto"</span>, sans-serif;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">60px</span>;
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">80px</span>;
}

<span class="hljs-selector-tag">h3</span> {
  <span class="hljs-attribute">text-align</span>: center;
}

<span class="hljs-selector-tag">p</span> {
  <span class="hljs-attribute">font-weight</span>: <span class="hljs-number">400px</span>;
}

<span class="hljs-selector-tag">span</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF338</span>;
}

<span class="hljs-selector-tag">ul</span> {
  <span class="hljs-attribute">list-style-type</span>: none;
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">339px</span>;
}


<span class="hljs-selector-tag">li</span> {
  <span class="hljs-attribute">outline-color</span>: <span class="hljs-number">#2C394B</span>;
  <span class="hljs-attribute">outline-style</span>: inset;
  <span class="hljs-attribute">outline-width</span>: <span class="hljs-number">2px</span>;
  <span class="hljs-attribute">outline-offset</span>: <span class="hljs-number">5px</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">11px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">0px</span>, <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-tag">img</span> {
  <span class="hljs-attribute">height</span>: <span class="hljs-number">500px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">100%</span>;
}


<span class="hljs-comment">/* Classes */</span>

<span class="hljs-selector-class">.movie-cover</span> {
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">800px</span>;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
  <span class="hljs-attribute">background-color</span>: <span class="hljs-number">#2C394B</span>;
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">0</span> auto;
  <span class="hljs-attribute">border-radius</span>: <span class="hljs-number">10px</span>;
}

<span class="hljs-selector-class">.circle</span> {
    <span class="hljs-attribute">background-color</span>: transparent;
    <span class="hljs-attribute">margin-right</span>: <span class="hljs-number">37px</span>;
    <span class="hljs-attribute">text-align</span>: center;
    <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">50px</span>;
    <span class="hljs-attribute">border</span>:<span class="hljs-number">3px</span> solid <span class="hljs-number">#FFF338</span>;
    <span class="hljs-attribute">height</span>:<span class="hljs-number">90px</span>;
    <span class="hljs-attribute">border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">-moz-border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">-webkit-border-radius</span>:<span class="hljs-number">50%</span>;
    <span class="hljs-attribute">width</span>:<span class="hljs-number">90px</span>;
}

<span class="hljs-selector-class">.ratings</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">margin-top</span>: <span class="hljs-number">12px</span>;
}

<span class="hljs-selector-class">.big-half</span> , <span class="hljs-selector-class">.small-half</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"roboto"</span>, sans-serif;
  <span class="hljs-attribute">font-style</span>: oblique;
  <span class="hljs-attribute">color</span>: white;
}

<span class="hljs-selector-class">.small-half</span> {
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#DAD0C2</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">19px</span>;
}



<span class="hljs-selector-class">.visit-movie-button</span> {
  <span class="hljs-attribute">margin</span>: <span class="hljs-number">30px</span>, <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">padding</span>: <span class="hljs-number">10px</span>, <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">position</span>: relative;
  <span class="hljs-attribute">top</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">left</span>: <span class="hljs-number">120px</span>;
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
  <span class="hljs-attribute">outline-style</span>: solid;
  <span class="hljs-attribute">color</span>: <span class="hljs-number">#FFF338</span>;
  <span class="hljs-attribute">outline-color</span>: <span class="hljs-number">#FFF338</span>;
  <span class="hljs-attribute">outline-offset</span>: <span class="hljs-number">10px</span>;
}


<span class="hljs-selector-class">.generate-movie-button</span> {
<span class="hljs-attribute">background-color</span>: <span class="hljs-number">#FFF338</span>;
<span class="hljs-attribute">padding</span>: <span class="hljs-number">0.5em</span> <span class="hljs-number">1.2em</span>;
<span class="hljs-attribute">font-size</span>: <span class="hljs-number">20px</span>;
<span class="hljs-attribute">text-decoration</span>: none;
<span class="hljs-attribute">position</span>: relative;
<span class="hljs-attribute">top</span>: <span class="hljs-number">50px</span>;
<span class="hljs-attribute">left</span>: <span class="hljs-number">250px</span>;
<span class="hljs-attribute">text-transform</span>: uppercase;
}

<span class="hljs-selector-class">.action-buttons</span> {
  <span class="hljs-attribute">width</span>: inherit;
}


<span class="hljs-selector-class">.title</span> {
  <span class="hljs-attribute">font-size</span>: <span class="hljs-number">50px</span>;
  <span class="hljs-attribute">padding-top</span>: <span class="hljs-number">40px</span>;
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">0</span>;
}

<span class="hljs-selector-class">.top-information</span> {
  <span class="hljs-attribute">display</span>: flex;
  <span class="hljs-attribute">justify-content</span>: space-between;
}

<span class="hljs-selector-class">.supporting-info</span> {
  <span class="hljs-attribute">padding-left</span>: <span class="hljs-number">30px</span>;
  <span class="hljs-attribute">font-weight</span>: bold;
  <span class="hljs-attribute">margin-bottom</span>: <span class="hljs-number">20px</span>;
}

<span class="hljs-selector-class">.lower-information</span> {
  <span class="hljs-attribute">font-family</span>: <span class="hljs-string">"roboto"</span>, sans-serif;
  <span class="hljs-attribute">width</span>: <span class="hljs-number">800px</span>;
  <span class="hljs-attribute">max-width</span>: <span class="hljs-number">800px</span>;
  <span class="hljs-attribute">margin-left</span>: <span class="hljs-number">380px</span>;
}
</code></pre>
<p>Once again, you can access the complete code for this project from its <a target="_blank" href="https://github.com/KingsleyUbah/harperdb-movie-generator">Github</a> repository.</p>
<h3 id="heading-how-to-add-the-movie-component">How to Add the Movie Component</h3>
<p>We now need to add our movie component. We will start by creating a new folder under the 'src' directory named <code>component</code>. We then need to create a new file inside that new file named 'movie.js'<em>.</em> This is where the spicy things start to happen.</p>
<p>In addition to the React and ReactDOM libraries, we are going to import the use-harperdb hook (function) as well.</p>
<p>We are going to execute the use-harperdb function, passing in an object as an argument. Inside the object, we need to supply at least a single query property. This property determines what kind of operation we want to carry out on our database.</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React <span class="hljs-keyword">from</span> <span class="hljs-string">'react'</span>;
<span class="hljs-keyword">import</span> { useHarperDB } <span class="hljs-keyword">from</span> <span class="hljs-string">'use-harperdb'</span>;

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Movie</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">let</span> [data, loading, error, refresh] = useHarperDB({
    <span class="hljs-attr">query</span>: {
      <span class="hljs-attr">operation</span>: <span class="hljs-string">'sql'</span>,
      <span class="hljs-attr">sql</span>: <span class="hljs-string">`select * from collection.movie where id = <span class="hljs-subst">${<span class="hljs-built_in">Math</span>.floor(<span class="hljs-built_in">Math</span>.random() * <span class="hljs-number">8</span>) + <span class="hljs-number">1</span>}</span>`</span>
    },
    <span class="hljs-attr">interval</span>: <span class="hljs-number">40000</span> <span class="hljs-comment">// 40 Seconds</span>
  }
  )

  <span class="hljs-comment">// CODE CONTINUES</span>
</code></pre>
<p>The first property, which is the operation property, specifies how you want to query the data. In our example, we are going to do so with a SQL command.</p>
<p>The second property within the query is the SQL property. This is where we write our SQL queries for any CRUD operation we want to carry out. In our case, we simply want to select all fields from a randomly selected movie between 1-8, from the database, which we have denoted by the following SQL clause:</p>
<pre><code class="lang-c">select * from collection.movie where id = ${Math.<span class="hljs-built_in">floor</span>(Math.random() * <span class="hljs-number">8</span>) + <span class="hljs-number">1</span>}`
</code></pre>
<p>After the query, another optional property we can define is the interval property. With this property, you can specify how long you want your app to wait before it automatically generates a fresh query to the database.</p>
<p>Executing the <code>useHarperDB</code> function with those parameters correctly passed in will return us an array containing some important things. Below are four important items we will get from <code>useHarperdDB</code>:</p>
<ul>
<li><p><code>loading</code>: This is a Boolean which specifies if the database is still processing data or not. That way, you can optionally display a “loading” spinner</p>
</li>
<li><p><code>error</code>: This indicates if an error was encountered while querying the database.</p>
</li>
<li><p><code>refresh</code>: Assuming you don’t set an interval property, you can call this function whenever you want to fetch new data.</p>
</li>
<li><p><code>data</code>: The main thing. If all things go well, HarperDB will return our data to this variable.</p>
</li>
</ul>
<h2 id="heading-how-to-display-data-in-our-front-end">How to Display Data in our Front End</h2>
<p>With our data now successfully returned from the database, it’s time pass it into our React template:</p>
<pre><code class="lang-js"><span class="hljs-keyword">if</span>(loading) {
    <span class="hljs-keyword">return</span> <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span> Loading... <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  }

<span class="hljs-keyword">if</span>(data) {
      <span class="hljs-keyword">return</span> (
<span class="xml"><span class="hljs-tag">&lt;&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"movie-cover"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"top-information"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h2</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"title"</span>&gt;</span>{data[0].title}<span class="hljs-tag">&lt;/<span class="hljs-name">h2</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"circle"</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"ratings"</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"big-half"</span>&gt;</span>{data[0].rating}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>/<span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"small-half"</span>&gt;</span>10<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"supporting-info"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"year"</span>&gt;</span>{data[0].date}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span> -
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"time"</span>&gt;</span>{data[0].hours}h:{data[0].minutes}m<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"image"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">img</span> <span class="hljs-attr">src</span>=<span class="hljs-string">{data[0].cover}</span> <span class="hljs-attr">alt</span>=<span class="hljs-string">"Movie Image"</span> /&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"genres"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">ul</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"movie-genres"</span>&gt;</span>
    {data[0].genres.map((genre, index) =&gt; {
    return (
    <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"movie-genre-item"</span>&gt;</span>{genre}<span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  )
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">ul</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"lower-information"</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{data[0].description}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Starring: {data[0].stars.map((star, index) =&gt; {
    return (
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span>&gt;</span>{star} - <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    )
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span> Writers:
    {data[0].writers.map((writer, index) =&gt; {
      return (
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"writer"</span>&gt;</span>{writer} - <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    )
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>Directors:
    {data[0].directors.map((director, index) =&gt; {
      return (
    <span class="hljs-tag">&lt;<span class="hljs-name">span</span> <span class="hljs-attr">key</span>=<span class="hljs-string">{index}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"director"</span>&gt;</span>{director} - <span class="hljs-tag">&lt;/<span class="hljs-name">span</span>&gt;</span>
    )
    })}
  <span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">hr</span> /&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"action-buttons"</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">{data[0].website}</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"visit-movie-button"</span>&gt;</span>Visit Movie<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">""</span> <span class="hljs-attr">className</span>=<span class="hljs-string">"generate-movie-button"</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{refresh}</span>&gt;</span>GENERATE NEW MOVIE<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
<span class="hljs-tag">&lt;/&gt;</span></span>
)
} <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span> (
      <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>Sorry No Data
        {error}
      <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span></span>
  )
}

}
</code></pre>
<p>If you are familiar with React, this shouldn’t be foreign to you. But I am still going to explain what we did here:</p>
<ul>
<li><p>As I stated, the <code>useHarperDB</code> function will return our data. If you query to get all of the movies, it will return an array of movies. Since we queried for only a single movie, it will return an object containing the data of a single movie.</p>
</li>
<li><p>Next, we have to check if the data was returned. If there is no data, we display a simple div displaying a "Sorry No Data" message.</p>
</li>
<li><p>Since we did receive data, we passed in the data into our template. We extract each field from the object and pass it into the correct template.</p>
</li>
</ul>
<p>When finished, we run the following command on the command line:</p>
<pre><code class="lang-c">npm start
</code></pre>
<p>That should start our development server at <a target="_blank" href="https://localhost:3000">https://localhost:3000</a>. If everything goes well, we should see our app live on the browser with some cool movie data!</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/MovieListCover.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>How our app should look</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Generate-new-movie.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><strong>That's it for our app!</strong></p>
<h2 id="heading-how-to-deploy-the-app-to-github-pages">How to Deploy the App to GitHub Pages</h2>
<p>Welcome to the last section of the tutorial. We will be deploying our new app to GitHub pages for the world to see.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Github-pages-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Deploy on GitHub Pages</em></p>
<p>If you have another hosting provider, then you are absolutely welcome to deploy your app there. If you don’t, or you want something free, then GitHub Pages is great.</p>
<p>First, you need to have a GitHub account. If you don’t, you can create one for yourself <a target="_blank" href="https://github.com/join">here</a>.</p>
<p>Also, you need to have Git version control software installed on your local machine. This is something every software developer should already have. However, if you don’t, you can install it from <a target="_blank" href="https://git-scm.com/downloads">here</a>.</p>
<p>The first thing to do is to create a new repository for your project on your GitHub account:</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/Create-a-New-Repository-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Creating a new GitHub repo</em></p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/repo-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Then go back to the terminal and run the following command:</p>
<pre><code class="lang-c">npm install gh-pages --save-dev
</code></pre>
<p>This will save GitHub Pages to your project as a dev dependency.</p>
<p>When this is done, go over to your project folder and open the package.json file. You should find gh-page safely installed there under the dev dependency:</p>
<pre><code class="lang-c"><span class="hljs-string">"devDependencies"</span>: {
    <span class="hljs-string">"gh-pages"</span>: <span class="hljs-string">"^3.2.3"</span>
  }
</code></pre>
<p>Next, we'll do the following three things:</p>
<ol>
<li>Navigate to your project directory (harperdb-movie-generator) and select the package.json file. On the top of your package json, you will add the following data (replace the template with yours):</li>
</ol>
<pre><code class="lang-c"><span class="hljs-string">"homepage"</span>:  https:<span class="hljs-comment">//{Your GitHub username here}.github.io/{Your Project Name}.git</span>
</code></pre>
<p>To find your GitHub username and the name of your repository, navigate to the newly created repo on GitHub. At the top, you can find your GitHub username and the project name next to it. Copy both of them and fill it into the aforementioned template – make sure to append .git at the end of your project name.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/project-name-and-username.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Github information for your package.json</em></p>
<p>The essence of adding the “homepage” field is to specify the URL where our app will eventually get hosted. Make sure to put a comma at the end, so your package json will be parsed correctly.</p>
<ol start="2">
<li>Go over to the "scripts" field within the same file and pass in the following data being sure you maintain proper indentation:</li>
</ol>
<pre><code class="lang-c"><span class="hljs-string">"predeploy"</span>: <span class="hljs-string">"npm run build"</span>,
<span class="hljs-string">"deploy"</span>: <span class="hljs-string">"gh-pages -d build"</span>
</code></pre>
<p>This is what you will run when you are ready to deploy to GitHub pages.</p>
<ol start="3">
<li>Finally, you are going to initialize Git in your project. To do this, simply navigate to your project directory on the command line and run the following command:</li>
</ol>
<pre><code class="lang-c">cd projects/harperbd-movie-generator

git init
</code></pre>
<p>Now, everything is set!</p>
<p>The only thing left to do is deploy your app to GitHub pages. To do this, run the following command:</p>
<pre><code class="lang-c">npm run deploy
</code></pre>
<p>And voilà! Your app will immediately be deployed to GitHub pages.</p>
<h2 id="heading-how-to-view-your-live-app">How to View Your Live App</h2>
<p>Your app is now live at this point, but you have to see what it looks like. So you have to get its URL.</p>
<p>Go over to your GitHub profile and click the repository tab. Select your newly created repo, go to the settings page, and scroll down a bit. You will find the GitHub pages section. Click on “check it out here!”</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/GIT1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>In the following page, inside Source, switch the Branch to “gh-pages” and file-path to “root”. Within a few minutes, your app will be all set. Copy the URL from the page and paste into a new browser window.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/git2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Project now live at the provided URL</em></p>
<p>And behold, you'll see your live project.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>We built this project with React and HarperDB. HarperDB is a great choice for your data management and back-end operations.</p>
<p>Not only is it flexible but also very easy to integrate, as we have seen in this tutorial.</p>
<p>You shouldn’t stop here. You can improve on your skills by building some other cool projects with this same stack. Thanks to HarperDB’s free plan, you don’t have to pay anything.</p>
<p>You can grab the code for this project from its <a target="_blank" href="https://github.com/KingsleyUbah/harperdb-movie-generator">GitHub repository</a>.</p>
<p>Want to reach out for any suggestions? You can get me on <a target="_blank" href="https://twitter.com/ubahthebuilder">Twitter</a></p>
<p>That’s it. Thank you for following along and have a great week.</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Array.filter() Tutorial – How to Iterate Through Elements in an Array ]]>
                </title>
                <description>
                    <![CDATA[ The Array.filter() method is arguably the most important and widely used method for iterating over an array in JavaScript. The way the filter() method works is very simple. It entails filtering out one or more items (a subset) from a larger collectio... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-array-filter-tutorial-how-to-iterate-through-elements-in-an-array/</link>
                <guid isPermaLink="false">66d46187787a2a3b05af4416</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Web Development ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Thu, 26 Aug 2021 16:07:48 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/filter-cover.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>The <code>Array.filter()</code> method is arguably the most important and widely used method for iterating over an array in JavaScript.</p>
<p>The way the <code>filter()</code> method works is very simple. It entails filtering out one or more items (a subset) from a larger collection of items (a superset) based on some condition/preference.</p>
<p>We all do this everyday, whether we're reading, choosing friends or our spouse, choosing what movie to watch, and so on.</p>
<h2 id="heading-the-javascript-arrayfilter-method">The JavaScript <code>Array.filter()</code> Method</h2>
<p>The <code>filter()</code> method takes in a callback function and calls that function for every item it iterates over inside the target array. The callback function can take in the following parameters:</p>
<ul>
<li><p><code>currentItem</code>: This is the element in the array which is currently being iterated over.</p>
</li>
<li><p><code>index</code>: This is the index position of the <code>currentItem</code> inside the array.</p>
</li>
<li><p><code>array</code>: This represents the target array along with all its items.</p>
</li>
</ul>
<p>The filter method creates a new array and returns all of the items which pass the condition specified in the callback.</p>
<h2 id="heading-how-to-use-the-filter-method-in-javascript">How to Use the <code>filter()</code> Method in JavaScript</h2>
<p>In the following examples, I will demonstrate how you can use the <code>filter()</code> method to filter items from an array in JavaScript.</p>
<h3 id="heading-filter-example-1-how-to-filter-items-out-of-an-array"><code>filter()</code> Example 1: How to filter items out of an array</h3>
<p>In this example, we filter out every person who is a toddler (whose age falls between 0 and 4 ).</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"aaron"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">65</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">2</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"cara"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">13</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">3</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"ella"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">25</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"fin"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">1</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"george"</span>,<span class="hljs-attr">age</span>: <span class="hljs-number">43</span>},
]

<span class="hljs-keyword">let</span> toddlers = people.filter(<span class="hljs-function"><span class="hljs-params">person</span> =&gt;</span> person.age &lt;= <span class="hljs-number">3</span>)

<span class="hljs-built_in">console</span>.log(toddlers)



<span class="hljs-comment">/*
[{
  age: 2,
  name: "beth"
}, {
  age: 3,
  name: "daniel"
}, {
  age: 1,
  name: "fin"
}]
*/</span>
</code></pre>
<h3 id="heading-filter-example-2-how-to-filter-out-items-based-on-a-particular-property"><code>filter()</code> Example 2: How to filter out items based on a particular property</h3>
<p>In this example, we will only be filtering out the team members who are developers.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> team = [
    {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"aaron"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"ui designer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"cara"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
      },
     {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"content manager"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"ella"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"cto"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"fin"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"backend engineer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"george"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
  },

]

<span class="hljs-keyword">let</span> developers = team.filter(<span class="hljs-function"><span class="hljs-params">member</span> =&gt;</span> member.position == <span class="hljs-string">"developer"</span>)

<span class="hljs-built_in">console</span>.log(developers)


<span class="hljs-comment">/*
[{
  name: "aaron",
  position: "developer"
}, {
  name: "cara",
  position: "developer"
}, {
  name: "george",
  position: "developer"
}]
*/</span>
</code></pre>
<p>In the above example, we filtered out the developers. But what if we want to filter out every member who is <strong>not</strong> a developer instead?</p>
<p>We could do this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> team = [
    { 
        <span class="hljs-attr">name</span>: <span class="hljs-string">"aaron"</span>,
           <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>,
           <span class="hljs-attr">position</span>: <span class="hljs-string">"ui designer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"cara"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"content manager"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"ella"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"cto"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"fin"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"backend engineer"</span>
      },
      {
          <span class="hljs-attr">name</span>: <span class="hljs-string">"george"</span>,
        <span class="hljs-attr">position</span>: <span class="hljs-string">"developer"</span>
      },

]

<span class="hljs-keyword">let</span> nondevelopers = team.filter(<span class="hljs-function"><span class="hljs-params">member</span> =&gt;</span> member.position !== <span class="hljs-string">"developer"</span>)

<span class="hljs-built_in">console</span>.log(nondevelopers)


<span class="hljs-comment">/*
[
    {
          name: "beth",
          position: "ui designer"
    }, 
    {
          name: "daniel",
          position: "content manager"
    }, 
    {
          name: "ella",
          position: "cto"
    }, 
    {
          name: "fin",
          position: "backend engineer"
    }
]

*/</span>
</code></pre>
<h3 id="heading-filter-example-3-how-to-access-the-index-property"><code>filter()</code> Example 3: How to access the index property</h3>
<p>This is a competition. In this competition, there are three winners. The first will get the gold medal, the second will get silver, and the third, bronze.</p>
<p>By using <code>filter</code> and accessing the <code>index</code> property of every item on each iteration, we can filter out each of the three winners into different variables.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> winners = [<span class="hljs-string">"Anna"</span>, <span class="hljs-string">"Beth"</span>, <span class="hljs-string">"Cara"</span>]

<span class="hljs-keyword">let</span> gold = winners.filter(<span class="hljs-function">(<span class="hljs-params">winner, index</span>) =&gt;</span> index == <span class="hljs-number">0</span>)
<span class="hljs-keyword">let</span> silver = winners.filter(<span class="hljs-function">(<span class="hljs-params">winner, index</span>) =&gt;</span> index == <span class="hljs-number">1</span>)
<span class="hljs-keyword">let</span> bronze = winners.filter(<span class="hljs-function">(<span class="hljs-params">winner, index</span>) =&gt;</span> index == <span class="hljs-number">2</span>)

<span class="hljs-built_in">console</span>.log(Gold winner: ${gold}, Silver Winner: ${silver}, Bronze Winner: ${bronze})

<span class="hljs-comment">// "Gold winner: Anna, Silver Winner: Beth, Bronze Winner: Cara"</span>
</code></pre>
<h3 id="heading-filter-example-4-how-to-use-the-array-parameter"><code>filter()</code> Example 4: How to use the array parameter</h3>
<p>One of the most common uses of the third parameter (array) is to inspect the state of the array being iterated over. For example, we can check to see if there is another item left in the array. Depending on the outcome, we can specify that different things should happen.</p>
<p>In this example, we are going to define an array of four people. However, since there can only be three winners, the fourth person in the list will have to be discounted.</p>
<p>To be able to do this, we need to get information about the target array on every iteration.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> competitors = [<span class="hljs-string">"Anna"</span>, <span class="hljs-string">"Beth"</span>, <span class="hljs-string">"Cara"</span>, <span class="hljs-string">"David"</span>]

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">displayWinners</span>(<span class="hljs-params">name, index, array</span>) </span>{
    <span class="hljs-keyword">let</span> isNextItem = index + <span class="hljs-number">1</span> &lt; array.length ? <span class="hljs-literal">true</span> : <span class="hljs-literal">false</span>
    <span class="hljs-keyword">if</span> (isNextItem) {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`The No<span class="hljs-subst">${index+<span class="hljs-number">1</span>}</span> winner is <span class="hljs-subst">${name}</span>.`</span>);
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Sorry, <span class="hljs-subst">${name}</span> is not one of the winners.`</span>)
    }
}

competitors.filter(<span class="hljs-function">(<span class="hljs-params">name, index, array</span>) =&gt;</span> displayWinners(name, index, array))

<span class="hljs-comment">/*
"The No1 winner is Anna."
"The No2 winner is Beth."
"The No3 winner is Cara."
"Sorry, David is not one of the winners."
*/</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/sorry.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>Oops, sorry David!</em></p>
<h3 id="heading-how-to-use-the-context-object">How to Use the Context Object</h3>
<p>In addition to the callback function, the <code>filter()</code> method can also take in a context object.</p>
<pre><code class="lang-js">filter(callbackfn, contextobj)
</code></pre>
<p>This object can then be referred to from inside the callback function using the <code>this</code> keyword reference.</p>
<h3 id="heading-filter-example-5-how-to-access-the-context-object-with-this"><code>filter()</code> Example 5: How to access the context object with <code>this</code></h3>
<p>This is going to be similar to <code>example 1</code>. We are going to be filtering out every person who falls between the ages of 13 and 19 (teenagers).</p>
<p>However, we will not be hardcoding the values inside of the callback function. Instead, we will define these values 13 and 19 as properties inside the <code>range</code> object, which we will subsequently pass into <code>filter</code> as the context object (second parameter).</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> people = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"aaron"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">65</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">15</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"cara"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">13</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">3</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"ella"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">25</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"fin"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">16</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"george"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">18</span>},
]

<span class="hljs-keyword">let</span> range = {
  <span class="hljs-attr">lower</span>: <span class="hljs-number">13</span>,
  <span class="hljs-attr">upper</span>: <span class="hljs-number">16</span>
}


<span class="hljs-keyword">let</span> teenagers = people.filter(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">person</span>) </span>{
    <span class="hljs-keyword">return</span> person.age &gt;= <span class="hljs-built_in">this</span>.lower &amp;&amp; person.age &lt;= <span class="hljs-built_in">this</span>.upper;
}, range)

<span class="hljs-built_in">console</span>.log(teenagers)

<span class="hljs-comment">/*
[{
  age: 15,
  name: "beth"
}, {
  age: 13,
  name: "cara"
}, {
  age: 16,
  name: "fin"
}]
*/</span>
</code></pre>
<p>We passed the <code>range</code> object as a second argument to <code>filter()</code>. At that point, it became our context object. Consequently, we were able to access our upper and lower ranges in our callback function with the <code>this.upper</code> and <code>this.lower</code> reference respectively.</p>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The <code>filter()</code> array method filters out item(s) which match the callback expression and returns them.</p>
<p>In addition to the callback function, the <code>filter</code> method can also take in a context object as the second argument. This will enable you access any of its properties from the callback function using <code>this</code>.</p>
<p>I hope you got something useful from this article.</p>
<p><strong>I</strong>f you want to learn more about Web Development, feel free to visit my**** <a target="_blank" href="https://ubahthebuilder.tech/my-top-10-youtube-channels-for-programmers">Blog</a><strong>.</strong></p>
<p>Thank you for reading and see you soon.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Array.forEach() Tutorial – How to Iterate Through Elements in an Array ]]>
                </title>
                <description>
                    <![CDATA[ In JavaScript, you'll often need to iterate through an array collection and execute a callback method for each iteration. And there's a helpful method JS devs typically use to do this: the forEach() method. The forEach() method calls a specified call... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/javascript-array-foreach-tutorial-how-to-iterate-through-elements-in-an-array-with-map/</link>
                <guid isPermaLink="false">66d4618b47a8245f78752ad4</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Tue, 24 Aug 2021 17:16:41 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/forEachtwo.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>In JavaScript, you'll often need to iterate through an array collection and execute a callback method for each iteration. And there's a helpful method JS devs typically use to do this: the <code>forEach()</code> method.</p>
<p>The <code>forEach()</code> method calls a specified callback function once for every element it iterates over inside an array. Just like other array iterators such as <code>map</code> and <code>filter</code>, the callback function can take in three parameters:</p>
<ul>
<li><p>The current element: This is the item in the array which is currently being iterated over.</p>
</li>
<li><p>Its index: This is the index position of that item within the array</p>
</li>
<li><p>The target array: This is the array which is being iterated over</p>
</li>
</ul>
<p>The <code>forEach</code> method does not return a new array like other iterators such as <code>filter</code>, <code>map</code> and <code>sort</code>. Instead, the method returns <code>undefined</code> itself. So it's not chainable like those other methods are.</p>
<p>Another thing about <code>forEach</code> is that you cannot terminate the loop (with the break statement) or make it skip one iteration (with the continue statement). In other words, you can’t control it.</p>
<p>The only way to terminate a <code>forEach</code> loop is by throwing an exception inside the callback function. Don’t worry, we will see all of this in practise soon.</p>
<h2 id="heading-how-to-use-the-foreach-method-in-javascript">How to Use the <code>forEach()</code> Method in JavaScript</h2>
<p>Imagine that a group of students lined up for a routine roll call. The class coordinator moves through the line and calls out the name of each student while marking whether they're present or absent.</p>
<p>It is important to note that the coordinator doesn't change the students' order in the line. He also keeps them in the same line after he is finished with the roll call. All he does is perform an action (his inspection) on each of them.</p>
<p>In the following examples, keeping this scenario in mind, we'll see how you can use the <code>forEach</code> method in JavaScript to solve real world problems.</p>
<h2 id="heading-foreach-method-examples-in-javascript"><code>forEach()</code> Method Examples in JavaScript</h2>
<h3 id="heading-how-to-remove-the-first-odd-number-in-an-array-with-foreach">How to Remove the First Odd Number in an Array with <code>forEach()</code></h3>
<p>In this example, we have an array which has one odd number at the first spot and several even numbers following it. But we only want the numbers in this array to be even. So we are going to remove the odd number from the array using the <code>forEach()</code> loop:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">3</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>, <span class="hljs-number">12</span>]
<span class="hljs-keyword">let</span> odd = <span class="hljs-number">3</span>;

numbers.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">number</span>) </span>{
    <span class="hljs-keyword">if</span> (number === odd) {
        numbers.shift() <span class="hljs-comment">// 3 will be deleted from array</span>
    }
})

<span class="hljs-built_in">console</span>.log(numbers);

[<span class="hljs-number">6</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>, <span class="hljs-number">12</span>] <span class="hljs-comment">// All even!</span>
</code></pre>
<h3 id="heading-how-to-access-the-index-property-with-foreach">How to Access the Index Property with <code>forEach()</code></h3>
<p>In this example, we are going to execute a <code>rollCall</code> function for each student looped over inside of the array. The <code>rollcall</code> function simply logs a string pertaining to each of the students to the console.</p>
<pre><code class="lang-js">names = [<span class="hljs-string">"anna"</span>, <span class="hljs-string">"beth"</span>, <span class="hljs-string">"chris"</span>, <span class="hljs-string">"daniel"</span>, <span class="hljs-string">"ethan"</span>]

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rollCall</span>(<span class="hljs-params">name, index</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Is the number <span class="hljs-subst">${index + <span class="hljs-number">1</span>}</span> student - <span class="hljs-subst">${name}</span> present? Yes!`</span>)
    ;}

names.forEach(<span class="hljs-function">(<span class="hljs-params">name, index</span>) =&gt;</span> rollCall(name, index));


<span class="hljs-comment">/*
"Is the number 1 student - anna present? Yes!"
"Is the number 2 student - beth present? Yes!"
"Is the number 3 student - chris present? Yes!"
"Is the number 4 student - daniel present? Yes!"
"Is the number 5 student - ethan present? Yes!"
*/</span>
</code></pre>
<p>In this example, the only information we had about each student was their name. However, we also want to know what pronouns each student uses. In other words, we want a pronoun property defined for each student.</p>
<p>So we define each student as an object with two properties, name and pronoun:</p>
<pre><code class="lang-js">names = [
    {<span class="hljs-attr">name</span>:<span class="hljs-string">"anna"</span>,<span class="hljs-attr">pronoun</span>: <span class="hljs-string">"she"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"beth"</span>,<span class="hljs-attr">pronoun</span>: <span class="hljs-string">"they"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"chris"</span>,<span class="hljs-attr">pronoun</span>: <span class="hljs-string">"he"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"daniel"</span>,<span class="hljs-attr">pronoun</span>: <span class="hljs-string">"he"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"ethan"</span>,<span class="hljs-attr">pronoun</span>: <span class="hljs-string">"he"</span>}
]

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rollCall</span>(<span class="hljs-params">student, index</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`The number <span class="hljs-subst">${index + <span class="hljs-number">1</span>}</span> student is <span class="hljs-subst">${student.name}</span>. Is <span class="hljs-subst">${student.pronoun}</span> present? Yes!`</span>);
}

names.forEach(<span class="hljs-function">(<span class="hljs-params">name, index</span>) =&gt;</span> rollCall(name, index));

<span class="hljs-comment">/*
"The number 1 student is anna. Is she present? Yes!"
"The number 2 student is beth. Is they present? Yes!"
"The number 3 student is chris. Is he present? Yes!"
"The number 4 student is daniel. Is he present? Yes!"
"The number 5 student is ethan. Is he present? Yes!"
*/</span>
</code></pre>
<p>We're logging the roll call message of each student to the console, then we perform a check to see what pronoun the student uses, and finally we dynamically pass the accurate pronoun as part of the string.</p>
<h3 id="heading-how-to-copy-an-array-into-a-new-array-with-foreach-in-javascript">How to Copy an Array into a New Array with <code>forEach()</code> in JavaScript</h3>
<p>After three years of studying, it is now time for each of the students to graduate. In our JavaScript, we define two arrays: <code>stillStudent</code> and <code>nowGraduated</code>. As you probably guessed, <code>stillStudent</code> holds the students right before their graduation.</p>
<p>Then the <code>forEach</code> loop takes in each of the students and calls the <code>graduateStudent</code> function on it.</p>
<p>In this function we construct an object with two properties: the name of the student as well as the position at which they graduated. Then we pass the new object to the <code>nowGraduated</code> array. At that point, the student has become a graduate.</p>
<p>This example also demonstrates how you can use the <code>forEach()</code> method to copy an array into a new array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> stillStudent = [<span class="hljs-string">"anna"</span>, <span class="hljs-string">"beth"</span>, <span class="hljs-string">"chris"</span>, <span class="hljs-string">"daniel"</span>, <span class="hljs-string">"ethan"</span>]
<span class="hljs-keyword">let</span> nowGraduated = []

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">graduateStudent</span>(<span class="hljs-params">student, index</span>) </span>{
    <span class="hljs-keyword">let</span> object = { <span class="hljs-attr">name</span>: student, <span class="hljs-attr">position</span>: index + <span class="hljs-number">1</span>}
    nowGraduated[index] = object
}

stillStudent.forEach(<span class="hljs-function">(<span class="hljs-params">name, index</span>) =&gt;</span> graduateStudent(name, index));

<span class="hljs-built_in">console</span>.log(nowGraduated);

<span class="hljs-comment">/*
[
    { name: "anna", position: 1}, 
    { name: "beth", position: 2}, 
    { name: "chris", position: 3}, 
    { name: "daniel", position: 4}, 
    { name: "ethan", position: 5}]
]
*/</span>
</code></pre>
<h3 id="heading-how-to-check-for-the-next-item-in-an-array-with-the-array-parameter">How to Check for the Next Item in an Array with the <code>array</code> Parameter</h3>
<p>At some point, the teacher will need to check if the list has a particular item next on the list. In such a case, the teacher will need to have a broad view of the whole list. That way, he can tell if there is a next student to call for.</p>
<p>In our JavaScript, we can replicate this because the callback function can also access the <code>array</code> (the third) parameter. This parameter represents the target array, which is <code>name</code>.</p>
<p>We check if there is a next item (student) on the array. If there is, we pass the string <code>positive</code> to the <code>nextItem</code> variable. If there is none, we pass the string <code>negative</code> to the variable. Then for every iteration, we check if <strong>that</strong> student is indeed the last.</p>
<pre><code class="lang-js">names = [<span class="hljs-string">"anna"</span>, <span class="hljs-string">"beth"</span>, <span class="hljs-string">"chris"</span>, <span class="hljs-string">"daniel"</span>, <span class="hljs-string">"ethan"</span>]

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">rollCall</span>(<span class="hljs-params">name, index, array</span>) </span>{
    <span class="hljs-keyword">let</span> nextItem = index + <span class="hljs-number">1</span> &lt; array.length ? <span class="hljs-string">"postive"</span> : <span class="hljs-string">"negative"</span>
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Is the number <span class="hljs-subst">${index + <span class="hljs-number">1</span>}</span> student - <span class="hljs-subst">${name}</span> present? Yes!. Is there a next student? <span class="hljs-subst">${nextItem}</span>!`</span>);
}

names.forEach(<span class="hljs-function">(<span class="hljs-params">name, index, array</span>) =&gt;</span> rollCall(name, index, array))

<span class="hljs-comment">/*
"Is the number 1 student - anna present? Yes!. Is there a next student? postive!"
"Is the number 2 student - beth present? Yes!. Is there a next student? postive!"
"Is the number 3 student - chris present? Yes!. Is there a next student? postive!"
"Is the number 4 student - daniel present? Yes!. Is there a next student? postive!"
"Is the number 5 student - ethan present? Yes!. Is there a next student? negative!"
*/</span>
</code></pre>
<h2 id="heading-you-cant-exit-a-foreach-loop-so-use-every-instead">You can't exit a <code>forEach</code> Loop, So Use <code>every()</code> Instead</h2>
<p>Remember when I mentioned that, by nature, you can't break out of (aka exit) a <code>forEach</code> loop? Once it's started, it will run until it reaches the last item in the array. So if you insert a <code>break</code> statement, it will return a <code>SyntaxError</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">12</span>]
<span class="hljs-keyword">let</span> odd = <span class="hljs-number">5</span>;

numbers.forEach(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">number</span>) </span>{
    <span class="hljs-keyword">if</span> (number === odd) {
        <span class="hljs-keyword">break</span>; <span class="hljs-comment">// oops, this isn't gonna work!</span>
    }
})
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/illegal.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p>Normally you would want to break out of a loop if you end up achieving what you intend to before reaching the last item. In our example above, we already found the odd number (5), so there was then no need to keep iterating over the remaining items (8 and 12).</p>
<p>If you want to break out of the loop on some condition, then you will have to use any of the following methods:</p>
<ul>
<li><p><code>for</code> loop</p>
</li>
<li><p><a target="_blank" href="https://futurestud.io/tutorials/node-js-for-of-vs-for-in-loops"><code>for…of</code> or <code>for…in</code> loop</a></p>
</li>
<li><p><code>Array.some()</code></p>
</li>
<li><p><code>Array.every()</code></p>
</li>
<li><p><code>Array.find()</code></p>
</li>
</ul>
<p>Here is how you can break out of a loop with <code>Array.every()</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">8</span>, <span class="hljs-number">12</span>]
<span class="hljs-keyword">let</span> odd = <span class="hljs-number">5</span>;

numbers.every(<span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> {
  <span class="hljs-keyword">if</span> (number == odd) {
    <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
  }

  <span class="hljs-built_in">console</span>.log(number);

  <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
});

<span class="hljs-comment">// 2 4</span>
</code></pre>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>In this tutorial, I have introduced the <code>forEach</code> method, illustrated how it works with a simple analogy, and I've also given you some practical examples of its usage in JavaScript code.</p>
<p>Hopefully you got something useful from this piece.</p>
<p><strong>If you want to learn more about Web Development, feel free to visit my</strong> <a target="_blank" href="https://ubahthebuilder.tech">Blog</a><strong>.</strong></p>
<p>Thank you for reading and see you soon.</p>
<blockquote>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
</blockquote>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ JavaScript Array.map() Tutorial – How to Iterate Through Elements in an Array with map() ]]>
                </title>
                <description>
                    <![CDATA[ When ES6 (EmcaScript 2015) came out, it ushered in a whole new set of methods for iterating over an array. And one of the most useful is the map() method. Array.prototype.map() is a built-in array method for iterating through the elements inside an a... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/array-map-tutorial/</link>
                <guid isPermaLink="false">66d4617133b83c4378a51847</guid>
                
                    <category>
                        <![CDATA[ arrays ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Kingsley Ubah ]]>
                </dc:creator>
                <pubDate>Thu, 19 Aug 2021 16:16:57 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/08/array-map.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>When ES6 (EmcaScript 2015) came out, it ushered in a whole new set of methods for iterating over an array. And one of the most useful is the <code>map()</code> method.</p>
<p><code>Array.prototype.map()</code> is a built-in array method for iterating through the elements inside an array collection in JavaScript. Think of looping as a way to progress from one element to another in a list, while still maintaining the order and position of each element.</p>
<p>This method <a target="_blank" href="https://www.freecodecamp.org/news/what-is-a-callback-function-in-javascript/">takes in a callback function</a> which gets called for every new element it iterates over.</p>
<p>The callback function takes in three parameters:</p>
<ul>
<li><p>The current value</p>
</li>
<li><p>It's index</p>
</li>
<li><p>The target array</p>
</li>
</ul>
<p>If you are a beginner struggling to understand how to use the <code>map()</code> method or what exactly it does, then this article is for you.</p>
<p>In this article, I will explain the <code>map()</code> method and illustrate how it works with some simple examples.</p>
<h2 id="heading-how-the-map-method-works-in-javascript">How the map() Method Works in JavaScript</h2>
<p>Imagine this: there is a queue of people outside a hospital waiting to be vaccinated. This means that they are yet to be vaccinated.</p>
<p>One-by-one, a doctor administers the vaccination to all of them. The doctor does this by iterating through the line. On one end, there is a group of people who are yet to be vaccinated. The doctor took each and every one of them, administered the vaccine to them, and returned them into a <strong>new</strong> line of vaccinated people.</p>
<p>On one end, there is an array (A) you want to operate on. <code>map()</code> takes in all elements in that array (A), performs a consistent action on each of those elements, and returns them into a new array (B).</p>
<h2 id="heading-how-to-use-the-map-method-avengers-example">How to Use the map() method – Avengers Example</h2>
<p>To illustrate how <code>map()</code> works in JavaScript, let's consider a list of names of some of our favourite Avengers. The problem is that the names in this list are not complete – they are missing an important suffix.</p>
<p>With <code>map()</code>, we can take all names in the array and append the "man" suffix to each and every one of them:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> firstNames = [<span class="hljs-string">"super"</span>, <span class="hljs-string">"spider"</span>, <span class="hljs-string">"ant"</span>, <span class="hljs-string">"iron"</span>]
<span class="hljs-keyword">let</span> lastName = <span class="hljs-string">"man"</span>;

<span class="hljs-keyword">let</span> fullNames = firstNames.map(<span class="hljs-function"><span class="hljs-params">firstName</span> =&gt;</span> firstName + lastName);

<span class="hljs-built_in">console</span>.log(fullNames);

<span class="hljs-comment">// ["superman", "spiderman", "antman", "ironman"]</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map1-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>VISUAL CODE</em></p>
<h3 id="heading-what-about-the-women">What about the women?</h3>
<p>Sorry, my bad. I realized my mistake and decided to include a female character at the <strong>first</strong> position in the Array. Each item within an array is identified by a unique value, its <strong>index</strong> (or position in the array). The first item will have an index of <code>0</code>, the second an index of <code>1</code>, and so on.</p>
<p>Since there is now a female superhero on the list, we will want to make sure we append the right suffix to the appropriate superhero.</p>
<p>Since <code>map()</code> also takes in the index of the item we are currently iterating over, we can do this by checking for the index of our hero and making sure we use the "woman" suffix for the first item on our array:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> firstNames = [<span class="hljs-string">"wonder"</span>, <span class="hljs-string">"spider"</span>, <span class="hljs-string">"ant"</span>, <span class="hljs-string">"iron"</span>]
<span class="hljs-keyword">let</span> male = <span class="hljs-string">"man"</span>;
<span class="hljs-keyword">let</span> female = <span class="hljs-string">"woman"</span>;

<span class="hljs-keyword">let</span> fullNames = firstNames.map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">firstName, index</span>) </span>{
    <span class="hljs-keyword">return</span> (index == <span class="hljs-number">0</span>) ? firstName + female : firstName + male;
 });

<span class="hljs-built_in">console</span>.log(fullNames);

[<span class="hljs-string">"wonderwoman"</span>, <span class="hljs-string">"spiderman"</span>, <span class="hljs-string">"antman"</span>, <span class="hljs-string">"ironman"</span>]
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map--8.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-use-the-index-argument">How to Use the Index Argument</h3>
<p>In addition to the value being iterated over, map takes in its index position as well. This is very useful if you want to perform different kinds of operations depending on the index position of the item.</p>
<p>In the previous example, we appended a different suffix by checking for the index.</p>
<p>To find out the index position of each of our items within the array, we can do this:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> fullNames = [<span class="hljs-string">"wonderwoman"</span>, <span class="hljs-string">"spiderman"</span>, <span class="hljs-string">"antman"</span>, <span class="hljs-string">"ironman"</span>]

fullNames.map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">firstName, index</span>) </span>{
    <span class="hljs-built_in">console</span>.log(${firstName} is at ${index} position)
});

<span class="hljs-comment">/*
"wonderwoman is at 0 position"
"spiderman is at 1 position"
"antman is at 2 position"
"ironman is at 3 position"
*/</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map--1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<p><em>RESULT</em></p>
<h3 id="heading-how-to-multiply-all-items-in-the-array-by-2">How to Multiply All Items in the Array by 2</h3>
<p>Let's work a bit with numbers now. In this example, we simply want to multiply every number in the target array by two and then return their products into a new array:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> numbers = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>]
<span class="hljs-keyword">let</span> multiplier = <span class="hljs-number">2</span>;

<span class="hljs-keyword">let</span> products = numbers.map(<span class="hljs-function"><span class="hljs-params">number</span> =&gt;</span> number * multiplier);

<span class="hljs-built_in">console</span>.log(products);

<span class="hljs-comment">// [20, 40, 60, 80]</span>
</code></pre>
<h3 id="heading-how-to-round-to-the-nearest-integer">How to Round to the Nearest Integer</h3>
<p>What if we have an array of decimals but we want each of those decimal numbers to be rounded to the nearest integer?</p>
<pre><code class="lang-c">let numbers = [<span class="hljs-number">3.7</span>, <span class="hljs-number">4.9</span>, <span class="hljs-number">6.2</span>]
let rounded = numbers.<span class="hljs-built_in">map</span>(function(number) {
    <span class="hljs-keyword">return</span> Math.round(number);
})

console.<span class="hljs-built_in">log</span>(rounded);

<span class="hljs-comment">// [4, 5, 6]</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map--2.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-change-strings-to-numbers">How to Change Strings to Numbers</h3>
<p>We have a list of numbers which are of the string type. However, we want to convert each one to the number type:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> strings = [<span class="hljs-string">"10"</span>,<span class="hljs-string">"20"</span>,<span class="hljs-string">"30"</span>]

<span class="hljs-keyword">let</span> numbers = strings.map(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">string</span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">Number</span>(string);
})

<span class="hljs-built_in">console</span>.log(numbers);

<span class="hljs-comment">// [10, 20, 30]</span>
</code></pre>
<h3 id="heading-how-to-get-the-avengers-real-names">How to Get the Avengers' Real Names</h3>
<p>In this example, we are working with objects. We have five avengers in the array, and each one has both a real name and a hero name. However, we only want to retrieve their real names into the new array.</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> avengers = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"steve rogers"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"captain america"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tony stark"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"iron man"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"bruce banner"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"the hulk"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"peter parker"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"spiderman"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tchalla"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"black panther"</span>}
]

<span class="hljs-keyword">let</span> realNames = avengers.map(<span class="hljs-function"><span class="hljs-params">avenger</span> =&gt;</span> avenger.name);

<span class="hljs-built_in">console</span>.log(realNames);

<span class="hljs-comment">// ["steve rogers", "tony stark", "bruce banner", "peter parker", "tchalla"]</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map--5-1.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-get-the-avengers-hero-names">How to Get the Avengers' Hero Names</h3>
<p>To get only their hero names, we do almost the exact same thing, only in this case we access the <code>heroName</code> property:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> avengers = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"steve rogers"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"captain america"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tony stark"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"iron man"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"bruce banner"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"the hulk"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"peter parker"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"spiderman"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tchalla"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"black panther"</span>}
]

<span class="hljs-keyword">let</span> heroNames = avengers.map(<span class="hljs-function"><span class="hljs-params">avenger</span> =&gt;</span> avenger.heroName);

<span class="hljs-built_in">console</span>.log(heroNames);

<span class="hljs-comment">// ["captain america", "iron man", "the hulk", "spiderman", "black panther"]</span>
</code></pre>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/08/map--10.png" alt="Image" width="600" height="400" loading="lazy"></p>
<h3 id="heading-how-to-separate-out-a-function">How to Separate Out a Function</h3>
<p>Instead of defining a function directly inside of <code>map()</code>, we can define the function outside and then call it inside our <code>map()</code> function:</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> avengers = [
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"steve rogers"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"captain america"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tony stark"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"iron man"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"bruce banner"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"the hulk"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"peter parker"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"spiderman"</span>},
    {<span class="hljs-attr">name</span>: <span class="hljs-string">"tchalla"</span>, <span class="hljs-attr">heroName</span>: <span class="hljs-string">"black panther"</span>}
]

<span class="hljs-keyword">let</span> getName = <span class="hljs-function"><span class="hljs-params">avenger</span> =&gt;</span>avenger.name;

<span class="hljs-keyword">let</span> realNames = avengers.map(getName);

<span class="hljs-built_in">console</span>.log(realNames);

<span class="hljs-comment">// ["steve rogers", "tony stark", "bruce banner", "peter parker", "tchalla"]</span>
</code></pre>
<h3 id="heading-how-the-array-argument-works">How the Array Argument Works</h3>
<p>Earlier I stated that on every iteration, the <code>map()</code> method takes in the value being iterated over and also its index position. There is another argument to add to those two, the <code>Array</code> argument.</p>
<p>The <code>arr</code> argument represents the target array being looped over, along with its entire content. With this argument, you can essentially look into the full array to find something.</p>
<p>In this example, we will access the <code>arr</code> parameter to look in and check if the current item is the last item in the list. If it is not, we access the next item and subtract it from the current item. If it is the last, we just return it.</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> oldArray = [<span class="hljs-number">33</span>, <span class="hljs-number">20</span>, <span class="hljs-number">10</span>, <span class="hljs-number">5</span>];
<span class="hljs-keyword">const</span> newArray = oldArray.map(<span class="hljs-function">(<span class="hljs-params">currentVal, index, arr</span>) =&gt;</span> {
    <span class="hljs-keyword">let</span> nextItem = index + <span class="hljs-number">1</span> &lt; arr.length ? arr[index + <span class="hljs-number">1</span>] : <span class="hljs-number">0</span>
    <span class="hljs-keyword">return</span> currentVal - nextItem;
    });


<span class="hljs-built_in">console</span>.log(newArray);

<span class="hljs-comment">// [13, 10, 5, 5]</span>
</code></pre>
<h2 id="heading-wrapping-up">Wrapping Up</h2>
<p>The <code>map()</code> method was introduced in ES6. With this method, we can access and perform a consistent action on every single item inside an array collection.</p>
<p>It takes in a callback function which it calls for every new element it iterates over.</p>
<p>In this tutorial, I have introduced the map() method, illustrated how it works with an analogy and given some practical examples of its usage in JavaScript code.</p>
<p>I hope you got something useful from this piece. If you like programming-related tutorials like this, you should <a target="_blank" href="https://ubahthebuilder.tech">check out my blog</a>. I regularly publish articles on software development there.</p>
<p>Thank you for reading and see you soon.</p>
<p><strong>P/S</strong>: If you are learning JavaScript, I created an eBook which teaches 50 topics in JavaScript with hand-drawn digital notes. <a target="_blank" href="https://ubahthebuilder.gumroad.com/l/js-50">Check it out here</a>.</p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
