<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: EidorianAvi</title>
    <description>The latest articles on Forem by EidorianAvi (@eidorianavi).</description>
    <link>https://forem.com/eidorianavi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F416752%2Fd9079d15-3148-4fc8-8885-0c3757db808d.jpg</url>
      <title>Forem: EidorianAvi</title>
      <link>https://forem.com/eidorianavi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/eidorianavi"/>
    <language>en</language>
    <item>
      <title>Forms in React</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Sat, 13 Mar 2021 18:18:09 +0000</pubDate>
      <link>https://forem.com/eidorianavi/forms-in-react-4752</link>
      <guid>https://forem.com/eidorianavi/forms-in-react-4752</guid>
      <description>&lt;p&gt;Something almost any application or website will include will undoubtedly be a form in some shape or manner. Whether it be simply for user login or to modify profile data or for a search function a form is simply a core building block in development. Chances are if you're reading this you have at least some idea as how to build a form out with HTML. This post is to demonstrate how a form is handled differently in a React application and how to build it out so it functions within the single page application. &lt;/p&gt;

&lt;h4&gt;
  
  
  Why is it different in React?
&lt;/h4&gt;

&lt;p&gt;Generally inputs and form elements maintain their own state on a web page this is how you're able to type into them and they maintain the script. React needs access to all of the state on the page including form data. So the forms state and the applications state needs to be the same thing. That's where building out how your form is handled differs in React.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Controlled Components&lt;/strong&gt; is how its done. What do I mean by that? Well the same way react is rendering the form onto the page it is also in control of rendering the users input onto the form. This is done through the use of connecting the users input to the state of the form component. &lt;/p&gt;

&lt;h3&gt;
  
  
  A Demonstration
&lt;/h3&gt;

&lt;p&gt;From this point I'll walk you through how to build out a basic dog form component that takes in name, age, and breed. I'll be personally using functional arrow components and the useState hook built in to React as opposed to a class component to handle state.&lt;/p&gt;

&lt;p&gt;Okay so first things first lets just create our component and make sure to bring in useState.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ReactForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;


&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;ReactForm&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next thing we can do is build out our form in our return statement with labels, inputs, and a submit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;htmlFor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;htmlFor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;label&lt;/span&gt; &lt;span class="nx"&gt;htmlFor&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/label&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;breed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
                &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/form&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks like a pretty standard HTML form at this point right? Well this is where it will start to differ and we will begin tying the inputs to the state of the component.&lt;/p&gt;

&lt;p&gt;We will need state to store the input values for name, age, and breed since that's the case. Lets set that up in our component with the useState hook.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setAge&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setBreed&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alright so now that we have a place in our component to store the inputs state we can then tie associate the value of the input to that state variable. Do this for each input. It should look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that the value of the input is associated with the state variable and the state variable is an empty string "" you'll notice in your form you can no longer type into the input box. Go ahead try it. &lt;/p&gt;

&lt;p&gt;That's a good sign. Means your input is associated to the components state. The problem at hand now is how do we allow for actual user input. Well it's associated with state so we have to change state right?&lt;/p&gt;

&lt;p&gt;Let's create a function for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleChange&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;age&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nx"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;breed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="nx"&gt;setBreed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Woah woah woah what's going on in there? Let's break it down&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A handleChange function is created. It takes in an event. I've chosen to deconstruct the target out from the event because what we're looking at is the event.target which in turn will be the input tag this function is associated with&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Since there are multiple inputs I've set up a switch statement that will consider the targets name value for each case.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Dependent on the value of the target.name the switch statement will use our earlier created hook to change the values state.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that the function for handling state change is created we just have to associate it with each input tag by assigning an onChange to our function like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Voila! Now that the change handler is associated with the input you should successfully be able to type into the input boxes. What does that mean then? That your changing the state of the component by typing into these inputs, and more importantly that the inputs state and the components state are one and the same.&lt;/p&gt;

&lt;p&gt;Okay so what else do we have to do with the form? Probably work on making sure you can submit the form. Lets create a handleSubmit function for that. I will be making it so the form makes a POST request to a dummy backend through the use of &lt;a href="https://httpbin.org"&gt;https://httpbin.org&lt;/a&gt;. It's an excellent resource for testing a request without it actually going anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleSubmit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="nx"&gt;breed&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://httpbin.org/post&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;headers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
              &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Content-Type&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="na"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;})&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's walk through this one.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A handleSubmit function is created that takes in an event as an argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;First we use preventDefault() on our event because what does a form generally do on submit? Refresh or redirect and that's not what we would like to do with a single page application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A dog object is created and we can assign each of its key value pairs to the state variables that store them.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A fetch POST request is made to the dummy backend and submits that previously mentioned dog object with our states values. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After a response is received and turned into JSON it is then locked to the console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alright now that we have determined the functionality of our submit let's associate it with our form by assigning our function to onSubmit like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="nx"&gt;onSubmit&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleSubmit&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now when you fill out the inputs and hit submit it will make a POST request that will in turn send a response which will then be logged to your console. Test it out.&lt;/p&gt;

&lt;p&gt;Success. Your form is functional. &lt;/p&gt;

&lt;p&gt;One last thing you can do is since the inputs are tied to state (Have I mentioned that yet?) is to clear the inputs on a submit. &lt;/p&gt;

&lt;p&gt;The way I handled that is created a super simple helper function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;clearForm&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;setBreed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And invoked it during the then chain on my handleSubmit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clearForm&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
          &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You should now have a controlled form component that allows for form submission and the form will clear on submit. How about that. Anyways that will be it from me today I hope you picked something up and as always feel free to reach out to me about any questions or comments. Happy Coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Recursion in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 08 Mar 2021 06:12:39 +0000</pubDate>
      <link>https://forem.com/eidorianavi/recursion-in-javascript-59l2</link>
      <guid>https://forem.com/eidorianavi/recursion-in-javascript-59l2</guid>
      <description>&lt;p&gt;Recursion is a common concept in coding and one everyone should understand down the road. I'd like to introduce it to anyone who's heard the term but isn't familiar with what it is or how it works within JavaScript.&lt;/p&gt;

&lt;p&gt;So first thing is to talk about what it is. Recursion is where a function calls itself within itself. So when the function is invoked it calls itself. Which then calls itself. Which then calls itself. Which then.. you get the point.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callMyself&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;callMyself&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//DONT DO THIS IT WILL INFINITELY RUN&lt;/span&gt;
&lt;span class="nx"&gt;callMyself&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Sounds like an infinite loop right? Well that's because it is. So what's the point of doing that? Well there isn't.. unless you enable a way out of the infinite loop. This will come in the form of conditional statements such as our good friends if/else statements. &lt;/p&gt;

&lt;p&gt;You can set this up so it either keeps recurring while a condition is true or it keeps recurring until it isn't true.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callMyself&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;condition&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;callMyself&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//Do something else besides invoking self&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;A super common example and one that helps to visualize the concept is counting numbers up or down until a condition is met.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newYearCountDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;nextNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextNumber&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
    &lt;span class="nx"&gt;newYearCountDown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nextNumber&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Happy New Year!!!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;So that's cool and all but counting can be done a great number of ways. The point is using recursion until a condition is met and using conditionals to exit the loop once the condition is satisfied. &lt;/p&gt;

&lt;p&gt;A second common example when it comes to recursion is finding the factorial of a given number. A factorial is the product of a number and every number preceding it. &lt;/p&gt;

&lt;p&gt;The factorial of 5 would be:&lt;/p&gt;

&lt;p&gt;5 x 4 x 3 x 2 x 1 = 120&lt;/p&gt;

&lt;p&gt;With recursion this would look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;factorial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;//factorial(5) returns 5 * factorial(4)&lt;/span&gt;
&lt;span class="c1"&gt;//factorial(4) returns 5 * 4 * factorial(3)&lt;/span&gt;
&lt;span class="c1"&gt;//factorial(3) returns 5 * 4 * 3 * factorial(2)&lt;/span&gt;
&lt;span class="c1"&gt;//factorial(2) returns 5 * 4 * 3 * 2 * factorial(1)&lt;/span&gt;
&lt;span class="c1"&gt;//factorial(1) returns 5 * 4 * 3 * 2 * 1 * factorial(0)&lt;/span&gt;
&lt;span class="c1"&gt;//factorial(0) returns 120&lt;/span&gt;


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

&lt;/div&gt;



&lt;p&gt;So now that you have a grasp of what recursion is doing you might be thinking this is doing the same thing as a loop. It's true these things could be done with a loop or via iteration and a lot of the time in JavaScript those may be a more fitting solution than recursion. So when is recursion most useful?&lt;/p&gt;

&lt;p&gt;Well one advantage is you can make your code a lot more readable if you use recursion. It can also shorten your code in some cases compared to loops. Some downsides with recursion is that it can take up more memory and produce slower code depending on usage. &lt;/p&gt;

&lt;p&gt;That'll be it from me this week.. happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Switch Statements in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Sun, 28 Feb 2021 22:49:53 +0000</pubDate>
      <link>https://forem.com/eidorianavi/switch-statements-in-javascript-2hjb</link>
      <guid>https://forem.com/eidorianavi/switch-statements-in-javascript-2hjb</guid>
      <description>&lt;p&gt;Today I'll be doing a quick tutorial on how to use a switch statement in JavaScript and when it's appropriate to use. I bring this up because I found my self in a situation where I was writing a series of if statements that a switch made for a much better solution. &lt;/p&gt;

&lt;h4&gt;
  
  
  When to use a switch statement?
&lt;/h4&gt;

&lt;p&gt;Switch statements are perfect for when different outputs may require a different following reaction. For example x + y can have many different outcomes but what if you need a different reaction depending on what the output turns out to be? You could write a series of if statements or you can contain the logic entirely within a switch statement.&lt;/p&gt;

&lt;h4&gt;
  
  
  How to use it:
&lt;/h4&gt;

&lt;p&gt;The switch itself will take in an expression and depending on what case the expression fulfills will be the code block that performs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;// Does something&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;// Does something else&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;// If none are met this one performs&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the basic structure of how it will look. Notice how at the end of each case we break out of the code. If you don't it will keep running through the cases.&lt;/p&gt;

&lt;p&gt;Here's a full on example of it in use. It will be a function that takes in a species as a string and return a name of a famous member of said species.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;famousMemberOfSpecies&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;famousMember&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;switch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;species&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Wookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;famousMember&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Chewbacca&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Twi'lek&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;famousMember&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hera Syndulla&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Weequay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;famousMember&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hondo Ohnaka&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hutt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="nx"&gt;famousMember&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jabba&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;break&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; 
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;No famous members found&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;famousMember&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;famousMemberOfSpecies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hutt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;&lt;span class="c1"&gt;//Should log "Jabba" to the console.&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;famousMemberOfSpecies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Nexu&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="c1"&gt;//Should log "No famous members found" to the console.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how I used the default case to handle an output that might not need a response.&lt;/p&gt;

&lt;p&gt;You can get much more complicated with these and perform entire code blocks in each case but the point is you can handle different outputs with different responses using this and avoid overusing if/else statements that are more appropriate for more complicated conditional statements. &lt;/p&gt;

&lt;p&gt;Anyways that'll be it for today. Happy Coding!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Uploading Images to your Node/MongoDB Backend</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 22 Feb 2021 02:38:18 +0000</pubDate>
      <link>https://forem.com/eidorianavi/uploading-images-to-your-node-mongodb-backend-l1a</link>
      <guid>https://forem.com/eidorianavi/uploading-images-to-your-node-mongodb-backend-l1a</guid>
      <description>&lt;p&gt;Today I will be breaking down how to upload an image to your node backend. This will be uploading the image file to the actual back end keep in mind so if you're looking to uploading to the database this isn't the article. I won't be showing anything about creating a front end form or anything but I will be testing my routes with POSTMAN.&lt;/p&gt;

&lt;p&gt;Something else to note is I will assume you have basic knowledge of express and routes in your backend. If you don't I have a prior post that might be helpful &lt;em&gt;nudge nudge&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let's get started. &lt;/p&gt;

&lt;p&gt;So first things first you will need to install &lt;strong&gt;multer&lt;/strong&gt; which is a package that will help with file handling.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i multer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Good. Now that that's done lets go ahead and create an 'uploads' folder in your root directory for storing these images.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir uploads
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following that lets set up some middleware so that we can access our uploads folder in a request. This is set up in my server file app.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.use('/uploads', express.static('uploads'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome now that that's all good to go let's head to whichever route you are trying to set up image upload with.&lt;/p&gt;

&lt;p&gt;In there you will bring in that multer mentioned earlier...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;multer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;multer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...and then we will configure it.&lt;/p&gt;

&lt;p&gt;First off lets set up its storage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;storage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;diskStorage&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;destination&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./uploads/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;-&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;originalname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Woah woah woah what's going on in there? &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A storage variable is created and assigned to multer.diskStorage()&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A couple options are set up inside of their in the form of an object. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The first being the destination which takes a  function with the request, the file, and the callback.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The callback takes in two parameters the first being for error handling which I won't be working with and the destination you'd like the image file to be stored. In our case the uploads folder we set up earlier.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second option is the filename which also takes a function with the same parameters. You can set that up how you like but I set it up so It prepends my original files name with the Date.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Sweet. Our storage options are created.&lt;/p&gt;

&lt;p&gt;Following that we will be setting up our upload middleware which invokes multer. We will also be configuring that with an object.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;upload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multer&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; 
    &lt;span class="na"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
    &lt;span class="na"&gt;limits&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;fileSize&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1024&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="nx"&gt;fileFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;originalname&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.jpg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.jpeg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
            &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Must be an image file&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I assign our invoked multer to the upload constant.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I assign its storage option to that storage constant we just configured.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A second option it takes limits. I assigned it so that it can only take a fileSize up to 5MB.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A third option is the fileFilter which will determine that the file is actually an image. I did the check by checking if the file name ends with any of the image tags. If not it throws an error and doesn't save if it does it saves the file.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We're mostly there.&lt;/p&gt;

&lt;p&gt;In your Schema make sure and include the image and in this case we will be accessing the path so it will be type: String.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;NovelSchema&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Schema&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;required&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;book_cover&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;timestamps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have it all set configured you just pass it into your post request as middleware.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Post a new novel&lt;/span&gt;
&lt;span class="nx"&gt;router&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;single&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;image&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;novel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Novel&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
            &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;

        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;savedNovel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;novel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;savedNovel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;&lt;p&gt;I passed our upload(multer) in as middleware to handle the file. I used the single option and passed in the argument for image. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The rest is a standard post response but notice how in our image instead of accessing our requests body we access the file.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I assigned it the path in our backend to access that image.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now what is stored in our backend is the image in our uploads folder and what is stored in MongoDB is a String with the path to that image. It should look something like:&lt;/p&gt;

&lt;p&gt;"uploads/1613958234056-Dark_Disciple_Cover.png"&lt;/p&gt;

&lt;p&gt;Since we setup middleware to access our uploads folder as a route earlier you can use that as your URL param and viola your image should appear. &lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Now you should be able to successfully save an image to your backend and store its path in your database. If you'd like to know how to store the actual image to the database.. well me too. I'm working on that currently and will be making a future blog post once I get it figured out myself. If you have any questions or comments feel free to reach out. As always happy coding! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Scope and Closure in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Sun, 14 Feb 2021 06:15:32 +0000</pubDate>
      <link>https://forem.com/eidorianavi/scope-and-closure-in-javascript-32e7</link>
      <guid>https://forem.com/eidorianavi/scope-and-closure-in-javascript-32e7</guid>
      <description>&lt;p&gt;Today I'd like to briefly go over what closure is in JavaScript and how it works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scope
&lt;/h2&gt;

&lt;p&gt;The first thing we have to talk about when it comes to closure is scope. Scope is the level at which you have access to a point of data. There are multiple levels of scope. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Global Scope: This is when a variable is declared globally in a file and is able to thus be accessed at any point in that same file. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Local Scope: This is a functions own degree of scope. Say a variable is declared inside the function this is considered local. If you try to access this variable outside of its function or local environment you will not have access to it. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Outer Functions Scope: This is a big part as far as closure is concerned. If you have a function inside of a function that inner function will have access to all variables declared in the outer function. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Examples
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Global&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jediMaster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Avar Kriss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;//This will log the string "Avar Kriss"&lt;/span&gt;

&lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log the string "Hello Avar Kriss"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main takeaway here is since the variable jediMaster is declared at a global level in the file it is accessible both outside and inside the function. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Local&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jediMaster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Avar Kriss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//This will give you a Reference Error as jediMaster is not defined&lt;/span&gt;

&lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log the string "Hello Avar Kriss"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now you see that the variable is only available to its local environment or the function as to where it was declared in. You cannot access it outside of the function even if in the same file. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Outer Function&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;jediMaster&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Avar Kriss&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;honorMaster&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello esteemed Jedi Master &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="nx"&gt;honorMaster&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;jediMaster&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;//This will give you a Reference Error as jediMaster is not defined&lt;/span&gt;

&lt;span class="nx"&gt;greetMaster&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log "Hello esteemed Jedi Master Avar Kriss"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's happening in here is that the function inside of the function still has access to the outer functions declared variable. This is due to it still being within the scope of the environment the variable was created in. &lt;/p&gt;

&lt;h2&gt;
  
  
  Closure
&lt;/h2&gt;

&lt;p&gt;So now that we know what scope is how does that have to do with closure? Well pretty much everything. &lt;/p&gt;

&lt;p&gt;A closure allows you to use scoping to your advantage and enclose an inner scope into a returned function allowing for encased behavior. It sounds tricky I know I'm still wrapping my head around it. &lt;/p&gt;

&lt;p&gt;Here's a basic example of what it can look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;incrementBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;firstClosure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;incrementBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;firstClosure&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log 3 to the console&lt;/span&gt;

&lt;span class="nx"&gt;firstClosure&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log 6 to the console&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;secondClosure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;incrementBy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;secondClosure&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log 5 to the console&lt;/span&gt;

&lt;span class="nx"&gt;secondClosure&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="c1"&gt;//This will log 10 to the console. &lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Let's talk about it. &lt;/p&gt;

&lt;p&gt;I created an incrementBy outer function that returns an innerFunction. That is closure to its core. Now let's walk it through step by step. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;First thing is I created the outer function incrementBy that takes in an integer as an argument.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I created a local count variable that will start at 0 within that outer function. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;I created the inner function that will increment and then log the count of the outer functions scope by the argument provided. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The inner function will be what the outer function returns. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;From there the incrementBy function is assigned to a variable firstClosure and invoked with the argument of 3. This effectively assigns the inner functions with an incrementor of 3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you invoke the variable that again is just the inner function you will see that it will now perform the inner functions purpose and log and increment that counter by 3 every time invoked. &lt;strong&gt;This is where it's important to take note. We are using the outer functions variable by invoking the inner function outside of the outer function. Sounds confusing I know. That means we've created enclosed(closure) behavior with this function.&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can verify this by assigning the same function incrementBy to another variable and see that it has its own count variable by invoking it. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;Now this example is extremely simple and can absolutely be performed without closure but it's the overall concept I'm trying to drive home and have performed it using closure. The point is that that count variable is encased and available for manipulation or use by the inner function even when the inner functions functionality is exported to an external scope. &lt;/p&gt;

&lt;p&gt;If you have any questions or comments please feel free to reach out to me. Happy Coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Async and Await in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Sun, 07 Feb 2021 23:00:53 +0000</pubDate>
      <link>https://forem.com/eidorianavi/async-and-await-in-javascript-3l7e</link>
      <guid>https://forem.com/eidorianavi/async-and-await-in-javascript-3l7e</guid>
      <description>&lt;p&gt;Do you really know JavaScript if you don't know how to use an asynchronous function? Sure, I'm not here to gatekeep but it doesn't mean you shouldn't know what they are and how to use them. Today I'll be briefly going over exactly that using the more recent async and await format.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is an asynchronous function and why use them?
&lt;/h4&gt;

&lt;p&gt;I'm glad you asked.&lt;/p&gt;

&lt;p&gt;Asynchronous functions in JavaScript are functions that enable promise based behavior to be used within your code. &lt;/p&gt;

&lt;p&gt;First thing you'll need to know is what the heck is a promise? Think of it as a placeholder you can work with for incoming data. This is necessary for handling data from an external API that is part of your page but takes a moment to fetch. So you're able to use a Promise in place of the data and set conditions for what happens to do the data if it successfully comes through or doesn't. &lt;/p&gt;

&lt;p&gt;A promise can be in three separate states: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;pending: initial state, neither fulfilled nor rejected.&lt;/li&gt;
&lt;li&gt;fulfilled: meaning that the operation was completed successfully.&lt;/li&gt;
&lt;li&gt;rejected: meaning that the operation failed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;async&lt;/strong&gt; and &lt;strong&gt;await&lt;/strong&gt; keywords were added to make promise handling more readable. It use to be more prevalent to do callback functions but I won't be getting into that today.&lt;/p&gt;

&lt;h3&gt;
  
  
  Async
&lt;/h3&gt;

&lt;p&gt;To create an asynchronous function you must first prepend it with the async keyword. When you do this you're stating that the function will be handling a Promise or a placeholder for incoming data. This could be done in the older function format or through the use of an arrow function like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt;&lt;span class="p"&gt;(){&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fetched&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;fetched&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now these functions clearly aren't doing much at the moment except logging the string "fetched". What matters is what they actually return upon invocation. If you invoke you will see in your console that actually logs a Promise object that should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise {&amp;lt;fulfilled&amp;gt;: "fetched"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The promise is in the fulfilled state since the data was immediately available considering it's just a string within the function.&lt;/p&gt;

&lt;p&gt;Where it counts is when the data takes time to load such as from a fetch. Today I will be using jsonplaceholder as my API to fetch from for examples sake. If  you're not familiar with it it's a super useful resource for fetching dummy data for practice.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com/todos&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now what happens when you log the return of this fetch? It still logs a Promise yes but if you'll notice it is now pending.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise {&amp;lt;pending&amp;gt;}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In both cases we have a Promise object to work with which is the point entirely. Even if the data you're reaching out is pending or still being fetched you at least have the Promise to work with which is crucial for working with APIs. &lt;/p&gt;

&lt;h3&gt;
  
  
  Await
&lt;/h3&gt;

&lt;p&gt;Great we got a Promise after fetching an API. Now what?&lt;/p&gt;

&lt;p&gt;Now my friend comes the await keyword. &lt;/p&gt;

&lt;p&gt;We can place the await keyword inside async functions to indicate that piece of data will be the one that may take a moment to fulfill. We simply include it prior to the fetch.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com/todos/1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will now turn that promise into it's response once fulfilled.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Response {type: "cors", url: "https://jsonplaceholder.typicode.com/todos/1", redirected: false, status: 200, ok: true, …}body: (...)bodyUsed: falseheaders: Headers {}ok: trueredirected: falsestatus: 200statusText: ""type: "cors"url: "https://jsonplaceholder.typicode.com/todos/1"__proto__: Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're getting somewhere. &lt;/p&gt;

&lt;p&gt;You will want to translate it to it's actual data so you can perform a .json() on your fetch or do it to the variable its assigned. Since that data is asynchronous make sure to still include the await keyword.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com/todos/1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which will turn that response into it's JSON format and it should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{userId: 1, id: 1, title: "delectus aut autem", completed: false}completed: falseid: 1title: "delectus aut autem"userId: 1__proto__: Object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Error Handling: Try/Catch
&lt;/h3&gt;

&lt;p&gt;The thing about pulling data from an external source is there can be hiccups along the way. Maybe it's on their end or maybe your fetch has a typo. Either way there can be errors when it comes to Promise handling. &lt;/p&gt;

&lt;p&gt;In an async function you can use a try/catch block to determine how to process the Promise whether it was properly fulfilled or rejected.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchAPI&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;https://jsonplaceholder.typicode.com/todos/1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;json&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First the function tries to get the response and if it works it will log the data in JSON format. If it doesn't work out it hits the catch and you will see what kind of error is occurring logged the console.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wrap Up
&lt;/h4&gt;

&lt;p&gt;There's just an introduction to the async/await format of promise handling JavaScript. There's of course always more to it but I'd recommend exploring the topic further on your own as there are plenty of nuances and approaches to how you work with these. If you're new to async functions I hope this shed some light on how they work and what they do. That'll be it for today. Happy coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>An introduction to animation and keyframes in CSS</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 01 Feb 2021 02:12:27 +0000</pubDate>
      <link>https://forem.com/eidorianavi/an-introduction-to-animation-and-keyframes-in-css-30mi</link>
      <guid>https://forem.com/eidorianavi/an-introduction-to-animation-and-keyframes-in-css-30mi</guid>
      <description>&lt;p&gt;A topic I've recently caught interest in is how to animate things through the use of CSS. It's not something I'm entirely unfamiliar with I've used it in previous projects but it's something I was hesitant to use thinking they'd be so much more difficult to do than they actually were. Go figure.  &lt;/p&gt;

&lt;p&gt;Today I'd like to break down how to build out your own personal(basic) animations using CSS. &lt;/p&gt;

&lt;h3&gt;
  
  
  A Quick Setup
&lt;/h3&gt;

&lt;p&gt;I'll be playing with two basic shapes a square and a circle. All I have in my project is an index.html file as well as a styles.css file and I of course linked the stylesheet to the index.&lt;/p&gt;

&lt;p&gt;The body of my HTML looks like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 HTML
&amp;lt;body&amp;gt;
    &amp;lt;div class="blue-box"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="red-circle"&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;/body&amp;gt;


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

&lt;/div&gt;

&lt;p&gt;And my styles sheet looks like: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
body {
    margin: auto;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

.blue-box {
    height: 200px;
    width: 200px;
    background-color: blue;
}


.red-circle {
    height: 200px;
    width: 200px;
    background-color: red;
    border-radius: 50%;
}


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

&lt;/div&gt;

&lt;p&gt;Which should look like:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6r9k5w3z29u32hz8ojjw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F6r9k5w3z29u32hz8ojjw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're all set to start playing with some animations. &lt;/p&gt;

&lt;h2&gt;
  
  
  Keyframes
&lt;/h2&gt;

&lt;p&gt;What are keyframes you ask? Well they're the CSS rule we use to assign how an animation will proceed at specific keyframes(think checkpoints) in its sequence. The directions of the animation if you will. &lt;/p&gt;

&lt;p&gt;To build out a keyframe animation in your CSS file you use the @keyframes at-rule and then assign it a name. This will be the name you call later so it should be self-descriptive.&lt;/p&gt;

&lt;p&gt;From there you can assign it a &lt;strong&gt;to&lt;/strong&gt; and &lt;strong&gt;from&lt;/strong&gt; value in terms of what you're animation should be doing. For more control(and my personal preference) you can also use multiple percentages in place of. &lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
@keyframes rotate {
    from {
        transform: rotate(0);
    }

    to {
        transform: rotate(360deg);
    }
}

/* Percentage Version */

@keyframes rotate {
    0% {
        transform: rotate(0);
    }

    50% {
        transform: rotate(180deg)

    100% {
        transform: rotate(180deg);
    }
}


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

&lt;/div&gt;

&lt;p&gt;Notice how in the percentage version I can specify where I want the animation to be at its 50% keyframe, or any keyframe for that matter. Much more control. If it's a two step animation by all means use from and to but for more customized animations percentage is the way to go. &lt;/p&gt;

&lt;h3&gt;
  
  
  Using the Animations
&lt;/h3&gt;

&lt;p&gt;So now that you created a keyframe animation that will rotate whatever you assign it to a full 360 degrees let's check out how you would implement it. &lt;/p&gt;

&lt;p&gt;There are multiple properties you can work with with animations but I will be going over the main three I've run into.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;animation-name: Takes in the name of your created animation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;animation-duration: The amount of time you would like the animation to last in its entirety.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;animation-iteration-count: The amount of times you would like the animation to repeat itself.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's check out how this might look on our blue box:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
.blue-box {
    height: 200px;
    width: 200px;
    background-color: blue;
    animation-name: rotate;
    animation-duration: 5s;
    animation-iteration-count: infinite;
}


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

&lt;/div&gt;

&lt;p&gt;If you're following along you'll notice the box is now rotating infinitely and it takes 5 seconds to make the full 360 rotation animation. &lt;/p&gt;

&lt;p&gt;There's also this nifty little shorthand in the form of:&lt;/p&gt;

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

animation: name duration timing-function


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

&lt;/div&gt;

&lt;p&gt;So our rule can now look like...&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
.blue-box {
    height: 200px;
    width: 200px;
    background-color: blue;
    animation: rotate 5s infinite;
}


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

&lt;/div&gt;

&lt;p&gt;... and work just the same! &lt;/p&gt;

&lt;h3&gt;
  
  
  A Couple More Animations
&lt;/h3&gt;

&lt;p&gt;Okay so now that we have an idea as to how make and use a keyframe animation let's check out a couple more quick ones.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
@keyframes color-changer {
    0% {
        background-color: red;
    }

    50% {
        background-color: green;
    }

    100% {
        background-color: blue;
    }
}

@keyframes pulsate {
    0% {
        width: 200px;
        height: 200px;
    }

    50% {
        width: 250px;
        height: 250px;
    }

    100% {
        width: 200px;
        height: 200px;
    }
}


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

&lt;/div&gt;

&lt;p&gt;Again these names are supposed to be descriptive as to what they perform. The first will cycle through RGB and the second will make the element pulsate.&lt;/p&gt;

&lt;p&gt;I made two to show you how you could use both on a single element. This time I will be working with the red circle.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
 CSS
.red-circle {
    height: 200px;
    width: 200px;
    background-color: red;
    border-radius: 50%;
    animation: pulsate 5s infinite, color-changer 10s 2;
}


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

&lt;/div&gt;

&lt;p&gt;I used the shorthand method to show that you can call on both animations on the same element and specify different rule sets for each animation. &lt;/p&gt;

&lt;p&gt;The red circle will now pulsate at the rate of 5 seconds per pulse infinitely. It will also change colors at the rate of 10 seconds per rotation but it will only occur twice. It will continue pulsating red after the color-changer animation is done running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Go Animate!
&lt;/h2&gt;

&lt;p&gt;That'll be it from me today on keyframes and animations. I know for a fact that there are some insanely creative and talented people out there who can work magic with this tool, and I'm sure you can do so much more than the basic stuff I showcased today. I do hope you either learned something new today or just enjoyed the read. Always open to feedback and discussion but until next time... happy coding! &lt;/p&gt;

</description>
      <category>css</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Two Sum Problem in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 25 Jan 2021 04:03:46 +0000</pubDate>
      <link>https://forem.com/eidorianavi/the-two-sum-problem-in-javascript-2gm7</link>
      <guid>https://forem.com/eidorianavi/the-two-sum-problem-in-javascript-2gm7</guid>
      <description>&lt;p&gt;The very first technical question I got asked was the classic two sum algorithm problem. I was fresh to algorithms and could solve it but I couldn't optimize it to a requested time complexity. Turns out this is a super common problem and you will likely encounter it either in an interview or down the road practicing algorithms. &lt;/p&gt;

&lt;p&gt;It's a pattern useful to recognize and it shows up in different types of problems so it's ideal to know how to tackle it if it ever rears its head.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;So the general gist of a two sum is that you have a list or an array of numbers and a target sum to hit. You're looking to return the indexes of the two numbers that when added together hit the target sum. There should only be one solution to the problem from the list of numbers and a number can not be used twice.&lt;/p&gt;

&lt;h3&gt;
  
  
  My first solution
&lt;/h3&gt;

&lt;p&gt;Let's assume the input is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;array = [1, 3, 10, 11, 14]&lt;/li&gt;
&lt;li&gt;goal = 13
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;twoSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
       &lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
          &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
       &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;indexes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return an array of [1, 2].&lt;/p&gt;

&lt;p&gt;It works but if you check it out you'll notice it's running a loop inside of a loop to figure out which two numbers add up to the goal. That puts us at a time complexity of O(n^2). Pretty slow. Not a big deal for a small array like ours but it's far from optimized and I could say without a shadow of a doubt that if you're doing this type of problem they will be looking for you to improve the time complexity.&lt;/p&gt;

&lt;h3&gt;
  
  
  A more optimized solution
&lt;/h3&gt;

&lt;p&gt;Lets assume the same input:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;array = [1, 3, 10, 11, 14]&lt;/li&gt;
&lt;li&gt;goal = 13
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;twoSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;goal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;mapOfNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;twoIndexes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;mapOfNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;goal&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapOfNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;mapOfNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;twoIndexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;twoIndexes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mapOfNumbers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;twoIndexes&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay so let's talk about what's going on in this.&lt;/p&gt;

&lt;p&gt;First thing is I mapped out the numbers and their indexes. I used the first for loop to accomplish this. Notice I assigned the number as the key and the index as its value.&lt;/p&gt;

&lt;p&gt;Second thing is a ran a second for loop through the array and first determined what the value would have to be to equal to goal at that index.&lt;/p&gt;

&lt;p&gt;Then inside that loop I do an if statement to check two things. One being if that target value exists in the map we created. The second making sure it's not at the same index as the one we are currently working with.&lt;/p&gt;

&lt;p&gt;If they both pass then I push those two indexes into my output array and simply return it.&lt;/p&gt;

&lt;p&gt;So while this one has two loops they're not nested so the time complexity lands at O(n) power. Much better.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;Okay so that's all I'm covering for today but if you have any questions at all feel free to reach out to me. I hope this is helpful to somebody in solving this problem you will no doubt encounter at some point. Happy Coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>The Math object in JavaScript</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Sun, 17 Jan 2021 05:10:25 +0000</pubDate>
      <link>https://forem.com/eidorianavi/the-math-object-in-javascript-25li</link>
      <guid>https://forem.com/eidorianavi/the-math-object-in-javascript-25li</guid>
      <description>&lt;p&gt;I have recently been using the Math object and its methods in my algorithms and I can't help but wonder why I didn't start using it sooner. This may be a super basic topic to some people but I personally had little to no exposure to using it until recently and thought I'd go over a few of the useful methods just to get them out there.&lt;/p&gt;

&lt;p&gt;One thing to clarify if you have no idea what I'm talking about JavaScript has a built in &lt;strong&gt;Math&lt;/strong&gt; object with many methods to perform mathematical calculations or to provide a common mathematical value such as Pi or the constant e. &lt;/p&gt;

&lt;p&gt;Now that we cleared that up let's get started. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Math.round(), Math.ceil() and Math.floor()
&lt;/h3&gt;

&lt;p&gt;These are all very similar in that they round to the nearest whole integer. &lt;/p&gt;

&lt;p&gt;Math.round() is used to round to the nearest whole integer whether it be lower or higher. Just like you would round UP at the .5 mark and down at anything lower.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.round(1.4);
// Returns 1

Math.round(2.5);
// Returns 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Math.ceil() also rounds to the nearest whole integer but it only rounds UP no matter the decimal value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.ceil(10.2);
// Returns 11

Math.ceil(13.7);
// Returns 14
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Math.floor() does the opposite. It will always round DOWN to the nearest whole value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.floor(100.3);
// Returns 100

Math.floor(56.9);
// Returns 56
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So they all have the same purpose but you have options depending on what you the situation might demand.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Math.min() and Math.max()
&lt;/h3&gt;

&lt;p&gt;These do exactly what they seem like they will do and will return the minimum or maximum value of a set of data.&lt;/p&gt;

&lt;p&gt;Don't let the simplicity fool you though that's incredibly useful.&lt;/p&gt;

&lt;p&gt;My favorite way to use these is when determining the minimum or maximum value in an array. To do this all you have to do is pass the array into the method but first make sure to use the spread operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.min(100, 4, 13, 8,56);
// Returns 4

Math.max(10, 2000, 26, 1701, 235);
// Returns 2000

const numberArray = [3, 6, 1, 4, 9];

Math.max(...numberArray);
// Returns 9

Math.min(...numberArray);
// Returns 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Math.sqrt() and Math.pow()
&lt;/h3&gt;

&lt;p&gt;Math.sqrt() allows you to find the square root of a given value. Nothing to fancy but definitely handy to know.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.sqrt(9);
// Returns 3

Math.sqrt(64);
// Returns 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Math.pow() takes in two values. The first being the base value, the second the power you'd like to apply to it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.pow(2, 2);
// Returns 4

Math.pow(2, 8);
// Returns 256
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Math.abs() and Math.sign()
&lt;/h3&gt;

&lt;p&gt;Math.abs() gives you the absolute value of a number so if you have  a case where you need a negative to be it's positive counterpart you use this, which happened to me recently incidentally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.abs(-100);
// Returns 100

Math.abs(200);
// Returns 200
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Math.sign() tells you the sign of the input whether it be positive, negative or zero. It returns it in the form of -1, 0, or 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.sign(-13);
// Returns -1

Math.sign(13);
// Returns 1

Math.sign(0);
// Returns 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This has proven to be useful for testing if a value passes a threshold.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;These are all methods I've recently used in my algorithm practice that have helped tremendously. They've also helped me shorten my solutions in multiple cases. &lt;/p&gt;

&lt;p&gt;There are quite a few methods used for calculation purposes and a couple common values like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.E
// Returns Eulers constant which is 2.718

Math.PI
// Returns 3.14159 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Really though there a ton of methods to use on the object for your calculation needs and here's a reference for your future mathematical needs.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anywho that'll it from me. Happy Coding! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>An Introduction to Web Accessibility</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 11 Jan 2021 03:23:03 +0000</pubDate>
      <link>https://forem.com/eidorianavi/an-introduction-to-web-accessibility-3mfl</link>
      <guid>https://forem.com/eidorianavi/an-introduction-to-web-accessibility-3mfl</guid>
      <description>&lt;p&gt;This week I'd like to talk about web accessibility. It's a topic I find not only interesting but also really important. I was asked once during an interview how to make your page more accessible and I blanked. I had all the answers I just didn't piece them together properly. Recently I've reexamined the topic and I'd like to share some of the points I've come across.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Web Accessibility?
&lt;/h3&gt;

&lt;p&gt;Well it's a pretty fitting name. It's making your web page more accessible to a larger audience. This means designing it in a way that allows for people of all varieties, disabilities included, to have equal access to your page. This is important for a multitude of reasons. From a business standpoint it makes sense you'd want your product to reach as many people as humanely possible. From an inclusivity standpoint it makes sense to build your page out in a way that allows those with disabilities to partake in standard societal practices such as browsing the web.&lt;/p&gt;

&lt;p&gt;Luckily HTML5 brought along many features that allow for much more inclusive practices. It allowed for writing much more semantic HTML. Semantic HTML meaning the code is much more descriptive to what it performs and contains. This allowed for screen readers, that someone with a visual disability might use, to sort through much more organized and sectioned off web pages. &lt;/p&gt;

&lt;h3&gt;
  
  
  Ways to Improve Accessibility:
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Header/Body/Footer
&lt;/h4&gt;

&lt;p&gt;Using tags to identify the three major parts of the page is easy to overlook considering they're not required when building out your page. It's important when making your page accessible to not section it out into its proper segments however.&lt;/p&gt;

&lt;p&gt;Use the header tags to store stuff such as your h1 tag which is the primary focus of the page as well as stuff like navigation bars or logo images. &lt;/p&gt;

&lt;p&gt;Use the body tags to store your pages actual content whatever that may be. You can only use these once on each page which makes sense there shouldn't be two body sections. This is used to store the mass majority of your page, the content the user is likely on the page to see. This can include tags of almost all varieties to be frank and will take up the majority of your HTML document. &lt;/p&gt;

&lt;p&gt;Use the footer tags to store stuff such as copyright information, contact info, a sitemap or a link to the top of the page. All things that are necessary for your page but aren't necessarily actual content of the page.&lt;/p&gt;

&lt;p&gt;These not only help you organize your page but a lot of screen readers can use them to quickly navigate the page for the user. &lt;/p&gt;

&lt;h4&gt;
  
  
  2. h1 - h6 tags
&lt;/h4&gt;

&lt;p&gt;So we all use these already on our pages but are we using them correctly? To be honest I definitely wasn't for a good while.&lt;/p&gt;

&lt;p&gt;Here's the thing when it comes to the header tags. A lot of people myself included use them for their sizing as opposed to their importance. Don't do that. I'm going to say it one more time just to drive it home. Don't do that. &lt;/p&gt;

&lt;p&gt;This matters for not only web accessibility but also for search engine optimization(SEO). &lt;/p&gt;

&lt;p&gt;From what I've gathered the h1 tag should be used only once per page. This is the overall point of the page and will be the first thing a search engine is seeking to learn if the page is relevant to a search. If you use it all over the place because you need larger text your pages meaning is lost. &lt;/p&gt;

&lt;p&gt;Following h2-h6 should be used in accordance with overall relevancy to the pages topic and to each other. These can be used multiple times throughout the page but each should be tagged accordingly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;header&amp;gt;
   &amp;lt;h1&amp;gt;This page is about Star Wars!&amp;lt;/h1&amp;gt;
&amp;lt;header&amp;gt;
&amp;lt;body&amp;gt;
   &amp;lt;h2&amp;gt;The Prequel Trilogy&amp;lt;/h2&amp;gt;
     &amp;lt;h3&amp;gt;Episode I: The Phantom Menace&amp;lt;/h3&amp;gt;
     &amp;lt;h3&amp;gt;Episode II: Attack of the Clones&amp;lt;/h3&amp;gt;
     &amp;lt;h3&amp;gt;Episode III: Revenge of the Sith&amp;lt;/h3&amp;gt;
   &amp;lt;h2&amp;gt;The Original Trilogy&amp;lt;/h2&amp;gt;
      &amp;lt;h3&amp;gt;Episode IV: A New Hope&amp;lt;/h3&amp;gt;
      &amp;lt;h3&amp;gt;Episode V: The Empire Strikes Back&amp;lt;/h3&amp;gt;
      &amp;lt;h3&amp;gt;Episode VI: Return of the Jedi&amp;lt;/h3&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how the tags are used in relation to their importance to the overall structure of the page. Not only that but I didn't jump from h2 to h5. The h3's are related to the h2 that precedes them. Structure your headers in such a fashion. &lt;/p&gt;

&lt;p&gt;One last thing I have to mention on using your headers properly is to just use your CSS styling to adjust their sizing and style to what you need.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Labels on forms / alt attribute on images
&lt;/h4&gt;

&lt;p&gt;When creating a form it's important to associate a label with a provided input field. It's easy to do you simply pass in the id of the input field into the 'for' attribute of the label. This identifies exactly what is supposed to go into the input field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form action=""&amp;gt;
  &amp;lt;label for="username"&amp;gt;Male&amp;lt;/label&amp;gt;
  &amp;lt;input type="text" id="username" value="male"&amp;gt;&amp;lt;br&amp;gt;
  &amp;lt;input type="submit" value="Submit"&amp;gt;
&amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When using an image that is a visual representation of a topic on the page it's important to provide a value for the alt attribute.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;img src="deathstar.jpg" alt="The Death Star looming over Alderaan"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attribute is important for not if the image fails to load but if a user can't personally experience the image. &lt;/p&gt;

&lt;p&gt;An instance of when not to use it is if the image is purely for styling effects on the page or if it already has a caption under it detailing it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrap Up
&lt;/h3&gt;

&lt;p&gt;These are just an introduction into making your page more accessible. There's really a lot more to it but I feel like these are a strong starting point in understanding what it is, why it matters and how to approach it. These are things you likely knew about but maybe didn't know why it mattered in using them correctly like I didn't initially. &lt;/p&gt;

&lt;p&gt;I'll be wrapping up there but here are some great resources to further read on the topic: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.w3.org/WAI/fundamentals/accessibility-intro/"&gt;https://www.w3.org/WAI/fundamentals/accessibility-intro/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/learn/responsive-web-design/applied-accessibility/"&gt;https://www.freecodecamp.org/learn/responsive-web-design/applied-accessibility/&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks for checking this out if you did and as always.. Happy Coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>a11y</category>
      <category>html</category>
    </item>
    <item>
      <title>Some Array methods I've recently encountered</title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 04 Jan 2021 03:32:16 +0000</pubDate>
      <link>https://forem.com/eidorianavi/some-array-methods-i-ve-recently-encountered-4ped</link>
      <guid>https://forem.com/eidorianavi/some-array-methods-i-ve-recently-encountered-4ped</guid>
      <description>&lt;p&gt;Recently I've been working on my algorithms and data structure problems which has of course led to a ton of array manipulation. As I've done so I've run into some less common and less used array methods and thought I could share a few of my favorite encounters. I'll be running through 3 array methods that are less common so I wont be doing the fan favorites such as .map() or .forEach()  but ones I found useful or that I just found interesting. &lt;/p&gt;

&lt;p&gt;Let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  .some()
&lt;/h3&gt;

&lt;p&gt;This one falls closer to the interesting category but absolutely has its uses as well. It's very similar to .includes() but not identical. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;: checks if any values in an array meet a certain condition. It returns a truthy value if any pass the condition. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const oddNumbers = [1, 3, 5, 7, 9, 11];

console.log(oddNumbers.some(number =&amp;gt; number % 2 === 0);
// This will log as false.

console.log(oddNumbers.some(number =&amp;gt; number &amp;gt; 8);
// This will log as true.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can of course be accomplished with other methods but if you're strictly lookingo see if any values in an array meet a certain condition and that's all this is the method you're looking for. &lt;/p&gt;

&lt;h3&gt;
  
  
  .from()
&lt;/h3&gt;

&lt;p&gt;So this one is more on the interesting side than useful in my opinion simply because I feel like I would rather use other methods to perform the same things. It creates an array from a string among other things and is performed on the actual Array global object not on an array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;: creates an array from a string. It can also act similar to a map however in that it can transform each index in an array. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const chewbaccaArray = Array.from('chewbacca');

console.log(chewbaccaArray);
// This will log ['c', 'h', 'e', 'w', 'b', 'a', 'c', 'c', 'a'];

const numbers = [1, 2, 3, 4, 5]

const tenDigits = Array.from(numbers, number =&amp;gt; number * 10);

console.log(tenDigits);
//This will log [10, 20, 30, 40, 50]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again my opinion on this is while it's interesting to know about it I feel like I'd rather either map an array or split a string for the same effect.&lt;/p&gt;

&lt;h3&gt;
  
  
  Set
&lt;/h3&gt;

&lt;p&gt;Alright so I don't know if this counts as an array method to be honest but it's easily my favorite of the bunch. This one hands down wins as the most useful of the three. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What it does&lt;/strong&gt;: It creates an array of only unique values so there are no duplicates. This isn't performed directly on an array but an array is passed into it. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const badCounting = [1, 2, 3, 3, 4, 5, 5, 5, 6];

const goodCounting = [...new Set(badCounting)];

console.log(goodCounting);
//This will log [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty sweet I know.&lt;/p&gt;

&lt;p&gt;I think this one was the most interesting and useful encounter and I'm stoked to have it in my arsenal. I can think of many times I was in need of exactly this. &lt;/p&gt;

&lt;p&gt;Anyways those are just some methods I've run into that I thought were worth sharing. Let me know if there are any other neat or niche ones that you can use on arrays or objects I'd love to see more.&lt;/p&gt;

&lt;p&gt;Happy Coding and Happy New Year!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>.slice() vs .splice() </title>
      <dc:creator>EidorianAvi</dc:creator>
      <pubDate>Mon, 28 Dec 2020 02:36:36 +0000</pubDate>
      <link>https://forem.com/eidorianavi/slice-vs-splice-34ce</link>
      <guid>https://forem.com/eidorianavi/slice-vs-splice-34ce</guid>
      <description>&lt;p&gt;Everyone has that one thing that they need to look up ever single time. For JavaScript mine happens to be when to use slice and when to use splice on an array. Writing this post should end that once and for all (maybe).&lt;/p&gt;

&lt;p&gt;The confusion stems from the fact that they do something very similar but at the same time in very contrasting ways so they have different use cases. They are both used to grab sections of an array. I'm going to break down how to use both and give my thoughts as to when one would be appropriate over the other. &lt;/p&gt;

&lt;h2&gt;
  
  
  .slice()
&lt;/h2&gt;

&lt;p&gt;So slice is used to grab a section of an array and it returns a new &lt;em&gt;COPY&lt;/em&gt; of that section while leaving the original array intact. The method can take in either one or two parameters. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first parameter being the index of the array at which you want to start. The starting index will be included in the new copied array.&lt;/li&gt;
&lt;li&gt;The second parameter is the index that you would like to stop at. It is not inclusive though so this index will not be included in the copy.&lt;/li&gt;
&lt;li&gt;If you don't use the second parameter and designate an end index the copied array will start at the first parameter and carry on all the way to the end of the array.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const planets = ['Tatooine', 'Alderaan', 'Coruscant', 'Kashyyyk', 'Naboo', 'Hoth'];

// This will return indexes 1-4

const middleFourPlanets = planets.slice(1,5); 
console.log(middleFourPlanets);
//expected output: ['Alderaan', 'Coruscant', 'Kashyyyk', 'Naboo']

// This will return everything from index 3 on

const lastThreePlanets = planets.slice(3);
console.log(lastThreePlanets);
//expected output: ['Kashyyyk', 'Naboo', 'Hoth']

//The original array is still intact with all of its indexes;

console.log(planets);
//expected output: ['Tatooine', 'Alderaan', 'Coruscant', 'Kashyyyk', 'Naboo', 'Hoth']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the use case for .slice() is when you need to grab parts of an array but still need the original array in its entirety.&lt;/p&gt;

&lt;h2&gt;
  
  
  .splice()
&lt;/h2&gt;

&lt;p&gt;The splice method is also used for grabbing a section of an array, the key difference being that it does it destructively. Beyond the fact that it can pull out parts of an array it can also add/replace sections to an array. It takes in three different types of parameters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first parameter is the index you would like to start the splice at. This index is included.&lt;/li&gt;
&lt;li&gt;The second is different than slice. It is not the index you want sliced up to, but instead &lt;em&gt;how many&lt;/em&gt; items you want spliced out of the array. If this isn't declared then it will cut off the array from the first parameter indicated. Something worth noting here is if you're just trying to insert into the array you can specify 0 and it won't remove any items.&lt;/li&gt;
&lt;li&gt;The third parameter type are items you would like to be inserted in place of what was removed or just at the position specified. Everything from the third parameter on will be inserted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since this is destructive it will alter the original array so keep that in mind before using it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Example:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const planets = ['Tatooine', 'Alderaan', 'Coruscant', 'Kashyyyk', 'Naboo', 'Hoth'];

//This will start at index 2, remove 0 items, but insert two more strings at that position.

planets.splice(2, 0, 'Mustafar', 'Bespin');
console.log(planets);
// expected output: ['Tatooine', 'Alderaan', 'Coruscant', 'Mustafar', 'Bespin', 'Kashyyyk', 'Naboo', 'Hoth']

//This will start at index 0 and remove 1 item. 

planets.splice(0,1);
console.log(planets)
// expected output:  ['Alderaan', 'Coruscant', 'Mustafar', 'Bespin', 'Kashyyyk', 'Naboo', 'Hoth']


//This will remove all items after the indicated position

planets.splice(3);
console.log(planets);
// expected output: ['Alderaan', 'Coruscant', 'Mustafar']

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

&lt;/div&gt;



&lt;p&gt;So this is effective for adding items to an array at a specific position. It is also useful to remove segments of an array that maybe aren't relevant. &lt;/p&gt;

&lt;p&gt;This shouldn't be used if you will need the original array at any point later. &lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap Up
&lt;/h2&gt;

&lt;p&gt;I'm feeling a little bit better on the two and think moving forward I'll know which one to use and when to use it. &lt;/p&gt;

&lt;p&gt;While this post was largely for me to solidify the difference on the two I hope if you read this you gained something from it too. As always.. Happy Coding!&lt;/p&gt;

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