<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jeff Swisher </title>
    <description>The latest articles on DEV Community by Jeff Swisher  (@jtswisher).</description>
    <link>https://dev.to/jtswisher</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%2F508268%2Fac295df5-87ab-4ded-92c6-7deec3286f35.jpeg</url>
      <title>DEV Community: Jeff Swisher </title>
      <link>https://dev.to/jtswisher</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jtswisher"/>
    <language>en</language>
    <item>
      <title>Data Structures: What's a Queue?</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 21 Nov 2020 18:18:39 +0000</pubDate>
      <link>https://dev.to/jtswisher/data-structures-what-s-a-queue-5bf6</link>
      <guid>https://dev.to/jtswisher/data-structures-what-s-a-queue-5bf6</guid>
      <description>&lt;p&gt;For the second article in my Data Structures series, we are going to be diving into queues. Queues are the polar opposite of the stack data structure. If you're not aware of what a stack real quickly go ahead and check out my article on them &lt;a href="https://dev.to/jtswisher/data-structures-what-s-a-stack-2jfd"&gt;here&lt;/a&gt; and come on back.&lt;/p&gt;

&lt;h2&gt;
  
  
  Queue
&lt;/h2&gt;

&lt;p&gt;Just like a stack, we can easily represent a queue's functionality with a real-world example. Think of a line of people waiting to go on a ride at your favorite amusement park. Naturally, the person that was in line first will be the first person to leave the line to go on their favorite ride. People are added to the line at the end and leave the line from the beginning.&lt;/p&gt;

&lt;p&gt;That is very similar to how a queue operates, the first piece of data added to our queue will be the first piece of data to be removed FIFO (First In First Out). When referencing adding an element to the queue we use the term &lt;strong&gt;Enqueue&lt;/strong&gt; and when referencing removing an element we use the term &lt;strong&gt;Dequeue&lt;/strong&gt;. When we enqueue an element we are adding it to the &lt;strong&gt;tail&lt;/strong&gt;(end) of the data structure and when we dequeue an element we are removing it from the &lt;strong&gt;head&lt;/strong&gt;(beginning) of the data structure. &lt;/p&gt;

&lt;p&gt;When creating a queue in JavaScript we have a couple of options at our disposal. Let's dive into two of them, we will be implementing a queue data structure with an array and then creating a queue from scratch.&lt;/p&gt;

&lt;p&gt;With an array implementation of a queue we can add to the end and remove from the beginning like below:&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;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&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="nx"&gt;queue&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dog&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mouse&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;3&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;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;dog&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;cat&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;mouse&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dog&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mouse&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or we can add to the beginning of the array and remove from the end:&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;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&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="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lion&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tiger&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bear&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;3&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;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;bear&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;tiger&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;lion&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lion&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;tiger&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;bear&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While both of the above implementations adhere to the queue's FIFO (First In First Out) operations think about the following:&lt;/p&gt;

&lt;p&gt;In the first example where we are adding to the end of the array and removing from the beginning, each time we remove an element from the beginning we have to re-index the entire array.&lt;/p&gt;

&lt;p&gt;In the second example where we are adding to the beginning of the array and removing from the end, each time we add an element to the beginning of the array we have to re-index the entire array.&lt;/p&gt;

&lt;p&gt;This re-indexing of the array gives us a linear O(n) time complexity which can have negative performance implications when dealing with very large data sets.&lt;/p&gt;

&lt;p&gt;Now let's create our own queue data structure from scratch which will give us a constant O(1) time complexity when we Enqueue or Dequeue elements.&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;class&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&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="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// enqueue(val) - adds element to our queue,&lt;/span&gt;
&lt;span class="c1"&gt;// returns number of elements in queue&lt;/span&gt;
    &lt;span class="nx"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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;newNode&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;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newNode&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="o"&gt;++&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// dequeue() - removes first element from queue&lt;/span&gt;
&lt;span class="c1"&gt;// returns value of element removed&lt;/span&gt;
    &lt;span class="nx"&gt;dequeue&lt;/span&gt;&lt;span class="p"&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="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&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;removedNode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="o"&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;removedNode&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="p"&gt;};&lt;/span&gt;


&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&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="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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;Console:&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;gt;&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;queue&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;Queue&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;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;first&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="nx"&gt;last&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="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;dog&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;1&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cat&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;2&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;mouse&lt;/span&gt;&lt;span class="dl"&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="mi"&gt;3&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Node&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;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mouse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;size&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="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dog&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mouse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="p"&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="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dequeue&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;dog&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dequeue&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;cat&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;Queue&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;first&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mouse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Node&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mouse&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;next&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="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&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;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dequeue&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;mouse&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dequeue&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Take some time to review the code and the sample outputs above. We have created a Queue class to create our queue data structure object which also allows us to Enqueue and Dequeue are elements. The Node class allows us to create an object housing our value and a pointer linking to the next node in our queue. While there is a lot more code in the above implementation, which may be hard to understand at first the performance gains will be worth it in the end when dealing with large data sets. When we Enqueue(adding to the tail) and Dequeue(removing from the head) in the above example we are getting a constant O(1) time complexity.&lt;/p&gt;

&lt;p&gt;I hoped this helped you understand the queue data structure better. If you have any questions or anything to add drop them in the comments below.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Data Structures: What's a Stack?</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Tue, 17 Nov 2020 00:47:27 +0000</pubDate>
      <link>https://dev.to/jtswisher/data-structures-what-s-a-stack-2jfd</link>
      <guid>https://dev.to/jtswisher/data-structures-what-s-a-stack-2jfd</guid>
      <description>&lt;p&gt;Among the many computer science fundamentals, Data Structures can be found near the top of the list of essential knowledge topics software developers should be well versed in. Data structures allow developers to efficiently manage large amounts of data and can have an impact on the performance of our program or algorithm depending on the data structure of choice. This will be a weekly series diving into some of the most common data structures with accompanying problems showing them in use. &lt;/p&gt;

&lt;h2&gt;
  
  
  First up is the STACK!
&lt;/h2&gt;

&lt;p&gt;Now you're probably wondering what a stack is, the simplest way of understanding this data structure can be easily represented with a real-world example. If you go into your kitchen and open the cabinet that contains your plates you're likely to see them neatly placed in a stack, unless you're like me, and still need to unload the dishwasher 😆 Now think about how your plates ended up in this stack and how they are removed when you need to use them. It is highly likely that the last plate you placed on the stack will be the first one removed when you go to grab a plate.&lt;/p&gt;

&lt;p&gt;That is exactly how the stack data structure operates, allowing operations at only one end of the data structure. Two easy acronyms that describe a stack: &lt;strong&gt;&lt;em&gt;LIFO (Last In First Out)&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;FILO (First In Last Out)&lt;/em&gt;&lt;/strong&gt;. When referencing the operations of a stack the insertion operation is called &lt;strong&gt;&lt;em&gt;Push&lt;/em&gt;&lt;/strong&gt; and the removal is called &lt;strong&gt;&lt;em&gt;Pop&lt;/em&gt;&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Now let's look at a problem where the stack data structure can be used to help solve the problem at hand. &lt;/p&gt;

&lt;h4&gt;
  
  
  Valid Parentheses
&lt;/h4&gt;

&lt;p&gt;Given an input &lt;code&gt;str&lt;/code&gt; containing the characters &lt;code&gt;'('&lt;/code&gt;, &lt;code&gt;')'&lt;/code&gt;, &lt;code&gt;'{'&lt;/code&gt;, &lt;code&gt;'}'&lt;/code&gt;, &lt;code&gt;'['&lt;/code&gt;, &lt;code&gt;']'&lt;/code&gt;, determine if the given string is valid.&lt;/p&gt;

&lt;p&gt;Input &lt;code&gt;str&lt;/code&gt; is valid if: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Opening brackets are closed by the same type of bracket &lt;code&gt;'()' =&amp;gt; true&lt;/code&gt;,  &lt;code&gt;'(]' =&amp;gt; false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;2. Opening brackets are closed in the correct order &lt;code&gt;'([])' =&amp;gt; true&lt;/code&gt;, &lt;code&gt;'([)]' =&amp;gt; false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If &lt;code&gt;str&lt;/code&gt; is valid return &lt;code&gt;true&lt;/code&gt; otherwise return &lt;code&gt;false&lt;/code&gt;. &lt;em&gt;For simplicity we are not going to worry about any edge cases in this problem&lt;/em&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;isValid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;map&lt;/span&gt; &lt;span class="o"&gt;=&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="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="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="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="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="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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;stack&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;str&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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="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;str&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="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;str&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="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;stack&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;str&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;else&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;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;str&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;stack&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&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;stack&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;stack&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&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="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&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;Output:&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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="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="p"&gt;[]&lt;/span&gt;
&lt;span class="kc"&gt;true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the &lt;code&gt;isValid&lt;/code&gt; function above we are using the stack to keep track of the opening brackets in the specific order that we encounter them. When an opening bracket is encountered we &lt;code&gt;push()&lt;/code&gt;(add) it onto the stack. When a closing bracket is encountered we check to see if the last opening bracket added to the stack is of the same bracket type as the current closing bracket, if it is we &lt;code&gt;pop()&lt;/code&gt;(remove) the opening bracket from the stack. If the last opening bracket added to the stack is not of the same type as the closing bracket we have encountered we return false. &lt;/p&gt;

&lt;p&gt;In the resulting output from running our function, you can see that the stack is adhering to the FILO &amp;amp; LIFO principles through each iteration of the &lt;code&gt;for&lt;/code&gt; loop. &lt;/p&gt;

&lt;p&gt;I hoped this helped you understand the stack data structure better and you feel comfortable implementing it in the future. If you have any questions or any other fun problems where a stack can be utilized drop them in the comments below.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>What's a Palindrome!?!?</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 23:01:40 +0000</pubDate>
      <link>https://dev.to/jtswisher/what-s-a-palindrome-221o</link>
      <guid>https://dev.to/jtswisher/what-s-a-palindrome-221o</guid>
      <description>&lt;p&gt;Technical coding interviews or code challenges on websites like LeetCode, CodeWars or HackerRank can often include varying questions that revolve around a singular idea. For example, a palindrome can be the foundation of a multitude of different problems that can have varying requirements for solving the problem based on the specific question. In the event, you're presented with a problem that revolves around palindromes it would be beneficial to know what a palindrome is in the first place. Let's dive into what exactly makes a palindrome and a couple of problems that are centered around them. &lt;/p&gt;

&lt;p&gt;A palindrome can be a word, number, phrase, or sequence of varying characters that reads the same forwards as it does backwards. &lt;/p&gt;

&lt;p&gt;A palindrome can be made out of a string of characters provided that all characters in the string occur an equal number of times. Or, all characters in the string occur an equal number of times except for &lt;strong&gt;&lt;em&gt;one&lt;/em&gt;&lt;/strong&gt; character that occurs an odd number of times. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Examples:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;noon&lt;/em&gt;&lt;/strong&gt; is a valid palindrome&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;racecar&lt;/em&gt;&lt;/strong&gt; is a valid palindrome&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;racecars&lt;/em&gt;&lt;/strong&gt; is not a valid palindrome&lt;/p&gt;

&lt;p&gt;In the first example above all characters occur an even number of times. In the second example, all characters occur an even number of times except for &lt;strong&gt;&lt;em&gt;'e'&lt;/em&gt;&lt;/strong&gt; that occurs only once(an odd frequency). In the third &lt;strong&gt;&lt;em&gt;invalid&lt;/em&gt;&lt;/strong&gt; example, we have &lt;strong&gt;&lt;em&gt;two&lt;/em&gt;&lt;/strong&gt; occurrences of a character that occurs an odd number of times making it impossible to create a palindrome out of that string of characters.&lt;/p&gt;

&lt;p&gt;Now let's solve a few problems that are centered around palindromes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1. Given a string of lowercase characters [a-z] determine if it is a palindrome. &lt;em&gt;For simplicity we are not going to worry about any edge cases in this problem&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&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;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;const&lt;/span&gt; &lt;span class="nx"&gt;reversedString&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&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="nf"&gt;reverse&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;join&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;reversedString&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;racecar&lt;/span&gt;&lt;span class="dl"&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="kc"&gt;true&lt;/span&gt;

&lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;racecars&lt;/span&gt;&lt;span class="dl"&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="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty simple right, we just use a few built-in functions to reverse the string and then return the value of a strict comparison between the original and the reversed string.&lt;/p&gt;

&lt;p&gt;However, we need to always be aware of the performance implications of our code. Let's solve that again without the use of any built-in functions...&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;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;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;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;str&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
       &lt;span class="c1"&gt;// loops through characters on the front half &lt;/span&gt;
       &lt;span class="c1"&gt;// of string and compares against the opposing &lt;/span&gt;
       &lt;span class="c1"&gt;// character on the back half of the string&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;racecar&lt;/span&gt;&lt;span class="dl"&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="kc"&gt;true&lt;/span&gt;

&lt;span class="nf"&gt;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;racecars&lt;/span&gt;&lt;span class="dl"&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="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I benchmarked the performance of the two above solutions using the string "racecar" on &lt;a href="https://jsbench.me/" rel="noopener noreferrer"&gt;JSbench&lt;/a&gt;. That tool stated that the first solution was 89.56% slower than the second solution. Sometimes less code is actually slower code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fa5yu7awduh1wtpvr4jqc.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%2Fa5yu7awduh1wtpvr4jqc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;2. Given a string of lowercase characters [a-z] determine if a palindrome can be created from all the given characters. &lt;em&gt;For simplicity we are not going to worry about any edge cases in this problem&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&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;isPalindromePossible&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&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;const&lt;/span&gt; &lt;span class="nx"&gt;frequencyCounter&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;c&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;frequencyCounter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;frequencyCounter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&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="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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;charsWithOddFrequency&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;c&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;frequencyCounter&lt;/span&gt;&lt;span class="p"&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;frequencyCounter&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;2&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;charsWithOddFrequency&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;charsWithOddFrequency&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;isPalindromePossible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;acerrac&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//can be rearranged into 'racecar'&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

&lt;span class="nf"&gt;isPalindromePossible&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;acerracs&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//cannot be rearranged into a palindrome, more than one character with odd frequency count&lt;/span&gt;
&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above solution, I created a frequency counter to keep track of the numerical occurrences of each character in the string using the for/of loop. In the for/in loop we are checking for odd occurrences of each character in the string, if we have more than one character with an odd frequency count we cannot create a palindrome out of the given characters.&lt;/p&gt;

&lt;p&gt;I hope this helped you understand what a palindrome is and the restrictions around how you can create one. If you have any questions or any other fun problems revolving around palindromes drop them in the comments below.&lt;/p&gt;

&lt;p&gt;Cheers!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Reflection: Mock Technical Interview via SkilledInc</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:31:27 +0000</pubDate>
      <link>https://dev.to/jtswisher/reflection-mock-technical-interview-via-skilledinc-35o2</link>
      <guid>https://dev.to/jtswisher/reflection-mock-technical-interview-via-skilledinc-35o2</guid>
      <description>&lt;p&gt;Your first technical interview can be a stressful and nerve-racking experience. The pressure that comes along with a technical interview can make even the most seasoned coders choke. Luckily for me, my first technical was a mock online interview on the Skilled Inc platform. However, even though it was a mock interview I was dreading the experience all day, a fact that my interviewer and I joked about once the experience was over. So let's dive into a high-level overview of the process and my thoughts on the experience.&lt;/p&gt;

&lt;p&gt;Skilled Inc's technical interviewing experience pairs you with a real Software Engineer that has been sourced from a top company to facilitate the interviewing experience. The interviewers on Skilled's roster are thoroughly vetted to ensure they have extensive industry experience and knowledge so you know you are working with the real deal. &lt;/p&gt;

&lt;p&gt;For my interview, I decided to use Javascript as my preferred testing language as I felt the most comfortable with it at the time. Unfortunately, I am not going to go into the specifics of the problems I was given out of respect to their process. However, I do want to go over some points that I took away from the process that should hopefully help others when approaching these types of interviews.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Understand the problem&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;em&gt;I cannot stress this enough, it is imperative that you understand the problem to its core to ensure you can solve it correctly.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This means thinking of the expected inputs. What is the expected output and how should any edge cases be handled? These are things you should be discussing with your interviewer in depth before you even start to solve the problem. Ask as many questions as you can, they are not going to look down on you for doing so!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Explore real-life examples&lt;/strong&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a very simple example, imagine you're asked to reverse a string? What are the inputs going to look like and what is the expected output after running your code?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;IN&lt;/em&gt; -&amp;gt; "Hello World!" &lt;em&gt;OUT&lt;/em&gt; -&amp;gt; "!dlroW olleH" &lt;/p&gt;

&lt;p&gt;Writing this out to see a visual representation of the expected behavior can help determine your approach.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Outline your process&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For me personally, this is very helpful as I can outline my approach at a very high level and step through my process to ensure I am not missing a step before diving straight into code.&lt;/p&gt;

&lt;p&gt;Using the reverse string example from above an easy straightforward approach could be the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Check the length of string..(don't forget your edge cases)&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Convert string into array use the built-in split function&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Reverse array, use the built-in reverse function&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Convert reversed array back to a string, use the built-in join function&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Return final value&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Seeing what you need to do in plain English makes it easier to then code your solution. It also gives your interviewer insight into your thought process which is very important&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Be aware of performance constraints&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You need to be consciously aware of the performance of your solution. For me, I don't know exactly what a built-in function like &lt;em&gt;reverse()&lt;/em&gt; from the above example is doing under the hood. Built-in functions can often times have negative effects on the performance of our solutions when taking into account the worst-case scenario for our input.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Refactor your solution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you ended up using a brute-force approach to solve the problem and you're completely aware of this please make it known to your interviewer. Them knowing that you are actively thinking about the most optimal solution from a performance standpoint will definitely help you stand out. If you have time left in your interview use that time to optimize your solution, sometimes good enough isn't really good enough...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Clever code isn't always good code&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm not going to lie I tried to be pretty clever in my solutions and unfortunately under the pressure of the interview my &lt;em&gt;clever&lt;/em&gt; code didn't quite work as expected. If you have spent any time on Codewars or any of the other similar platforms it's quite common to see one-liner solutions to problem sets. While these may give you the impression that the person who wrote the solution is a god among coders, these types of solutions are not optimal in a professional setting.&lt;/p&gt;

&lt;p&gt;Writing code that is hard to understand requires more cognitive output from your other teammates when maintaining said code. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” - Martin Fowler&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Have Fun!&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I love spending time on Leetcode, Hackerrank &amp;amp; Codewars. Everyone loves getting a win under their belt and those types of sites give me thousands of opportunities to do so. Don't let the pressure of the interview setting take the fun out of the process, I mean you love to code right!?&lt;/p&gt;

&lt;p&gt;I hope the points outlined above can help you when approaching a technical interview as they have definitely helped me. Oh, and if you're wondering I did receive a pass on my Skilled Inc interview. I would recommend that platform and their process to anyone, although it is a little expensive. If the cost is out of reach at the moment try to link up with a friend or someone senior to conduct your own mock interview. You can never have too much practice.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Async Logic with Redux-Thunk</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:26:06 +0000</pubDate>
      <link>https://dev.to/jtswisher/async-logic-with-redux-thunk-4n39</link>
      <guid>https://dev.to/jtswisher/async-logic-with-redux-thunk-4n39</guid>
      <description>&lt;p&gt;If you have worked with React before you have most likely encountered the built-in state object available within React components. The state object allows us to store property values that belong to the component where they have been declared. Our ability to access state declared in one component from another component is entirely possible within our app but can become convoluted as our application grows and we need to re-use the information contained in a state object within other components in our app.&lt;/p&gt;

&lt;p&gt;Redux is a state management tool that helps alleviate some of the issues we encounter when working with local component state and provides us with the ability to store all of our state in a Javascript object separate from all of our components. This separation provides us the ability to allow any component within our app to access our state just by connecting it to our store which contains all of our application state. That’s a very high-level overview of Redux and the opportunities it provides us. If you have not worked with Redux before I recommend reviewing their &lt;a href="https://react-redux.js.org/" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; as the remainder of this article will be covering an async request life cycle while utilizing the Redux Thunk middleware.&lt;/p&gt;

&lt;p&gt;For a project at Flatirons Software Engineering program, I created an application that utilized a React front end with a Rails API backend. This application utilized Redux to manage the state for the entire application on the client-side. Within a normal Redux store, we can only implement synchronous updates when we dispatch our actions. However, when we are requesting information from an external API or the backend API of our app we need to use an asynchronous approach when fetching and persisting data. This is where the Thunk middleware comes into play.&lt;/p&gt;

&lt;p&gt;Redux Thunk allows us to complete these asynchronous requests by returning a function within our action creator instead of an action. The inner function that is being returned within our action creator takes in “dispatch” and “getState” as arguments. The action creator below utilizes the dispatch function being passed in as an argument to dispatch two different actions to the reducer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fwfux02m20rw0404908l6.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%2Fwfux02m20rw0404908l6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The first dispatch call tells our reducer that we are requesting the news data from the external API. This action will hit a case in our switch statement that will update our state changing the value of the “requesting” flag to true. The value of the requesting attribute in our state object allows us to update what is being rendered on the client-side while we are fetching our data, like a spinner or loading bar. When the Fetch call returns the Promise and the Response object we can then manipulate that data as we see fit and send the second dispatch call changing the value of the “requesting” flag and update our applications state completing the request cycle.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ffddgjdpc8ix2lywcpn1c.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%2Ffddgjdpc8ix2lywcpn1c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hopefully, this quick overview gave you a better understanding of Thunk in Redux and how you can implement it within your action creators to complete asynchronous operations.&lt;/p&gt;

&lt;p&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/async-logic-with-redux-thunk-2fa63e5d10ec" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>redux</category>
    </item>
    <item>
      <title>Hoisting in Javascript</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:22:31 +0000</pubDate>
      <link>https://dev.to/jtswisher/hoisting-in-javascript-3af7</link>
      <guid>https://dev.to/jtswisher/hoisting-in-javascript-3af7</guid>
      <description>&lt;p&gt;In JavaScript, there can be a lot of concepts that are hard to grasp for new Developers. Hoisting being one of them, in an attempt to help myself understand it better and hopefully, you as well let's dive into this concept further.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hoisting
&lt;/h2&gt;

&lt;p&gt;Hoisting will occur at both the variable and function level. A general definition would state that the declarations of variables and functions are moved(hoisted) to the top of the code within their respective scope. If the declaration occurs within the global scope it is moved to the top of that scope and the same can be said for local scope declarations. Hoisting will only move the declarations, it does not execute assignment statements. Although it may sound like your code is being physically moved within your codebase, that is not the case. These declarations are simply being stored in memory during the creation phase of the execution context.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Variables are initially set to a value of &lt;strong&gt;&lt;em&gt;undefined&lt;/em&gt;&lt;/strong&gt; at the time of &lt;strong&gt;&lt;em&gt;hoisting&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F90pr7ut5huhf911nd1g0.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%2F90pr7ut5huhf911nd1g0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As we can see above we have “console.log(a)” on both lines 2 and 4. But we are getting two different results from both “console.log” functions. What is happening here exactly? I think it would be safe to assume that we would expect to receive &lt;em&gt;“ReferenceError: a is not defined”&lt;/em&gt; from our first “console.log” as “a” did not exist before we referenced it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;But take a look below at a representation of what our code would need to look like to achieve the same results as above if hoisting did not occur.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Flv3a5irn3240js6ztdwq.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%2Flv3a5irn3240js6ztdwq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is important to understand in regards to the above example that when hoisting occurred for “var a;” it did not physically move to the top of our function as shown. That first “console.log(a)” is referencing the original declaration of “a” from the first example that was stored in memory and initialized with a value of “undefined”.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Take a look at the example below! Weird huh? Where is our undefined value that was associated with “var a;” in the example above? While it is true that all declarations (var, let, const, and class) are hoisted, let and const declarations remain uninitialized. They will only get initialized during the execution phase of the execution context. This means we cannot access the variable before the point in which it was declared and is evaluated during runtime.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fk93t3cabm6khwntpa81t.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%2Fk93t3cabm6khwntpa81t.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So far we have touched mainly on variable hoisting and haven't talked much about function hoisting. We can classify functions as either &lt;strong&gt;&lt;em&gt;“function declarations”&lt;/em&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;em&gt;“function expressions,”&lt;/em&gt;&lt;/strong&gt; declarations will be hoisted while expressions will not.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Consider the following example. We would expect that we would have to declare and call our function in this manner for it to work.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fzzrczkabb8t9i1cvqd9g.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%2Fzzrczkabb8t9i1cvqd9g.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;However, thanks to function hoisting we are allowed to call our functions before they have been declared.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2pt2g0mg6y497ok4mw5f.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%2F2pt2g0mg6y497ok4mw5f.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While we could dive deeper into hoisting the information above should give you a basic understanding of hoisting and put you in a position to either benefit from what’s happening under the hood or avoid some pesky bugs down the road! If you have anything to add leave a comment below, happy coding!&lt;/p&gt;

&lt;p&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/hoisting-in-javascript-8efa9a391598" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Javascript Scope</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:13:58 +0000</pubDate>
      <link>https://dev.to/jtswisher/javascript-scope-3k4h</link>
      <guid>https://dev.to/jtswisher/javascript-scope-3k4h</guid>
      <description>&lt;p&gt;The concept of Scope in regards to JavaScript can be a bit hard to wrap your head around as a new Developer. Lets dive into this concept further for a more in-depth look.&lt;/p&gt;

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

&lt;p&gt;In JavaScript, there are two types of “scoping” that you will encounter. There is Global scope and Local scope. These scopes in relation to a variable will be determined by the location of the variable declaration.&lt;/p&gt;

&lt;h4&gt;
  
  
  Global Scope
&lt;/h4&gt;

&lt;p&gt;Global scope applies to any variable that is declared outside of a function in the top-level scope. Variables that have a global scope can be accessed from anywhere in your code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mVzgaNnF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fqkj4o70zsj4mckhb6gj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mVzgaNnF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/fqkj4o70zsj4mckhb6gj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“b” is accessible from within the functions local scope due to “b” being scoped globally.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YlI1hKMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9edifkpi6ulw9phe10ja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YlI1hKMF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9edifkpi6ulw9phe10ja.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Also important to note, variables declared without (var, const or let) are scoped globally.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Local Scope
&lt;/h4&gt;

&lt;p&gt;Each function you create with JavaScript has its own local scope, as such when you declare a variable from within a function it is scoped locally to that function. This means the locally scoped variable is unavailable outside of that function, however, it will be available to any nested functions within the parent function through the scope chain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6IWLED0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1lmkwwvidiobqbcrkw89.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z6IWLED0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1lmkwwvidiobqbcrkw89.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“b” is not accessible outside of its local scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RUx-pIV7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yjewyrs7tcp7ausvf3o5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RUx-pIV7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/yjewyrs7tcp7ausvf3o5.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“b” is available to the “nestedFunc” function through the scope chain or what's called “lexical scoping.” The nested function has access to all variables that are declared in its parent functions local scope and so on up the scope chain all the way to the global scope.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mBRjVDQ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/973tcbkfei5mxupxawyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mBRjVDQ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/973tcbkfei5mxupxawyh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In this example, we have two separate declarations of the variable “b”, while in the previous example we logged the value from the parent scope. The nested function will always check for a local instance of the variable we are referencing, if it does not find what it is looking for in its current scope it will move up the scope chain until it does or throws an error.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--aLjrsJsH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vr9kcuor9o0dj8di7mk7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--aLjrsJsH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vr9kcuor9o0dj8di7mk7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“b” is locally scoped to the “nestedScope” function here. We cannot access it from the parent “scope” function, the scope chain does not work from the top down.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thus far we have looked at local scope from a function scope standpoint. With ECMA script 6 (ES6) the concept of block scope was introduced together with the new variable declaration keywords const and let.&lt;/p&gt;

&lt;h4&gt;
  
  
  Block Scope
&lt;/h4&gt;

&lt;p&gt;Block scope is defined within for/while loops and if/switch statements. Const and let are the only variable declaration keywords that support local scope as noted in the code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oqazPjg4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nyykfuwn1jfjbh4xtq67.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oqazPjg4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/nyykfuwn1jfjbh4xtq67.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whew, that was a lot of scope talk, I hope this helps you understand this concept a bit better. If I missed anything or you have anything to add leave a comment below. Happy Coding!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was migrated over from Medium. Original article can be found &lt;a href="https://medium.com/@jefftswisher/scope-in-javascript-48e08329f090"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>GifBook JavaScript SPA w/Rails API </title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:11:22 +0000</pubDate>
      <link>https://dev.to/jtswisher/gifbook-javescript-spa-w-rails-api-5e3p</link>
      <guid>https://dev.to/jtswisher/gifbook-javescript-spa-w-rails-api-5e3p</guid>
      <description>&lt;p&gt;After completing the JavaScript portion of Flatiron Schools engineering program and before we got to dive into the React framework we were tasked with building a SPA(Single Page Application) showcasing our newfound knowledge of the language. This application was required to utilize a pure JavaScript, HTML &amp;amp; CSS front end with a Rails API back end.&lt;/p&gt;

&lt;p&gt;For my project, I decided on creating an app that would allow a user to search for GIFs utilizing the GIPHY API and store their favorites to their account. Additionally, through the use of the Twilio API, the user would be provided the ability to send a GIF via SMS text to their desired recipient. Below I walk through the surprisingly easy integration of what I thought would be the most difficult aspect of my app, SMS messaging via Twilio.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7l9ggtlgoe4k3adhfaog.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%2F7l9ggtlgoe4k3adhfaog.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Twilio API can be easily integrated into a Rails application through the use of the Twilio Ruby helper library by installing the ‘twilio-ruby’ gem. After the gem was installed I integrated the Twilio client into my message model as a class method. In this method I needed to initialize the Twilio client and authenticate via the provided ‘account_sid’ &amp;amp; ‘auth_token’ I was given by Twilio. I was then able to call the ‘messages.create’ methods on the ‘client’ while passing in the associated arguments to include the phone number for the recipient, the message ‘body,’ and the associated URL for the GIF provided by the end-user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqr8ij649red7n2bs3m7s.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%2Fqr8ij649red7n2bs3m7s.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The ‘new_message’ class method gets called from the create action in the message controller when an associated POST fetch request is made by the user on the front end. The form on the front end captures the data passed in by the user via an event listener listening for the ‘submit’ event of the form. Once the event is triggered the parameters then get passed into the ‘create’ action in the Message controller via the ‘createMessage’ function in the main JavaScript file. This sequence of events then fires off the SMS message to the recipient.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb44b9i0kxq2c4vhjhrq3.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%2Fb44b9i0kxq2c4vhjhrq3.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Having never worked with Twilio before it was something I wanted to integrate into my app as I like to test my knowledge working with new systems and implementations that I am unfamiliar with. The integration of this feature was a last priority for me as I wanted the MVP up and running as quick as possible and I anticipated a lot more work then what was actually required to get it up and running. Thanks go to Twilio’s helper library, and ill be looking for something new and more difficult for the next project!&lt;/p&gt;

&lt;p&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/gif-book-f87e205064b7" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rails</category>
      <category>twilio</category>
    </item>
    <item>
      <title>Rails Cocktail Recipe APP</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 19:05:30 +0000</pubDate>
      <link>https://dev.to/jtswisher/rails-cocktail-recipe-app-34jp</link>
      <guid>https://dev.to/jtswisher/rails-cocktail-recipe-app-34jp</guid>
      <description>&lt;p&gt;Up until now my time at Flatiron Schools software engineering program has been focused on the Ruby programming language. We have worked through the procedural operations of the language and covered the object-orientated aspect as well for our foundation. After having our solid foundation we delved into Ruby frameworks and learned how to use them to build a well-rounded web application.&lt;/p&gt;

&lt;p&gt;The first framework we utilized was Sinatra which is more lightweight, less feature-rich, and not as abstract as rails. Learning Sinatra was a huge stepping stone in bringing everything thus far full circle and providing a deeper understanding of all the aspects of a full-fledged web application. This was very exciting and I could not wait to dive into rails to see its benefits and additional features.&lt;/p&gt;

&lt;p&gt;For my Rails project, I decided to create a recipe app specific to cocktails named, “Cocktail Book.” The basic functionality needed to allow users to create an account, create a cocktail recipe, favorite and save to their account a pre-existing cocktail recipe, as well as provide a rating and any additional comments to a favorite cocktail they save to their account.&lt;/p&gt;

&lt;p&gt;One of the most difficult things throughout this process was handling the nested form for cocktails and the associated nested attributes each cocktail would have. To build a successful cocktail I needed to implement three tables and their associated models: cocktails, ingredients &amp;amp; cocktail_ingredients. The cocktails &amp;amp; ingredients table was standard while the cocktail_ingredients table was the join table for this resource.&lt;/p&gt;

&lt;p&gt;The cocktail_ingredients join table would include the cocktail_id, ingredient_id, and a quantity that was editable by the user. I could have easily included the quantity in the ingredients table and gotten away without utilizing the join table. However, without the join table, my ingredients would not be reusable across different cocktails as each ingredient would then have a set quantity that may or may not be usable in the cocktail recipe being created by a user. Thus requiring ingredients to be duplicated in the DB with their own unique quantities.&lt;/p&gt;

&lt;p&gt;Throughout my application, I used Bootstrap for styling. This being said I wanted to utilize Bootstrap’s form components to keep the styling uniform. I stumbled upon the gem: “bootstrap_form”, “~&amp;gt; 4.0.” This allowed me to continue to utilize the Rails Action View Form helper form_for with Bootstraps styling added.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsg0ya0o2ha3lbl4w9hsf.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%2Fsg0ya0o2ha3lbl4w9hsf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the above form, you can see the nested “cocktail_ingredients” under “cocktail” and the nested “ingredients” under “cocktail_ingredients.” In the cocktails controller, I had to instantiate placeholder “cocktail_ingredients” &amp;amp; “ingredient” objects to allow the user to associate multiple ingredients with their cocktail.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2nrium9u23kczcy5dqa7.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%2F2nrium9u23kczcy5dqa7.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This renders 5 fields for both the “cocktail_ingredients” &amp;amp; “ingredient” attributes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjn8qdc2kavfg6b0r7lvv.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%2Fjn8qdc2kavfg6b0r7lvv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After the user submits the form I needed to whitelist the incoming params to make sure no foreign attributes were being introduced. In the cocktails controller, I created a strong_params method to ensure we are only getting the desired attributes passed through to the create action in the controller.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ftsowy1wj0f1bqwkjxazi.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%2Ftsowy1wj0f1bqwkjxazi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Strong params method&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fl2e9yj4pouyo1z06fvw4.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%2Fl2e9yj4pouyo1z06fvw4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Cocktail create action&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The create action above builds us 3 objects across all 3 models: Cocktail, Ingredient &amp;amp; CocktailIngredient, as well as creating the associations between all three objects thanks to the association declarations in the models themselves.&lt;/p&gt;

&lt;p&gt;This experience was very beneficial and provided many great learning opportunities. Going forward I would like to add more functionality to the app to include leveraging Rails’ Active Storage component to allow cocktail images to be uploaded at the time of recipe while utilizing an Amazon S3 bucket to hold those images in the cloud.&lt;/p&gt;

&lt;p&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/cocktail-book-a-rails-app-9ee17bb483a2" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Ruby Sinatra App &amp; Heroku Deploy</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 18:53:58 +0000</pubDate>
      <link>https://dev.to/jtswisher/ruby-sinatra-app-heroku-deploy-3oc8</link>
      <guid>https://dev.to/jtswisher/ruby-sinatra-app-heroku-deploy-3oc8</guid>
      <description>&lt;p&gt;At the time of this article’s writing, I had just wrapped up my second project for Flatiron School’s Software Engineering program. Our task for this project was to create a Sinatra app that incorporated full CRUD(create, read, update &amp;amp; delete) functionality utilizing the Model View Controller software design pattern.&lt;/p&gt;

&lt;p&gt;My project was designed to allow a user to create an account and search for their favorite movie, provide a rating and any other comments, and save that movie to their account. This being made possible by the incorporation of an API from TMDb (The movie database.) After the build process for my application was complete I wanted to deploy my app for the world to see without a user having to clown down its repo, install dependencies and run it on their local machine.&lt;/p&gt;

&lt;p&gt;Heroku is a cloud hosting platform that was the perfect solution to help me do this. My app was built utilizing Sinatra which is a web application library and domain-specific language written in Ruby. This was not the cause of my issues though, the culprit was the relational database management system I was utilizing within my app - SQLite. Unfortunately for me, Heroku does not support SQLite on its platform. I ended up changing my database management system to PostgreSQL to get my app deployed. Let's go over it below and if you encounter the same issues you will be able to bypass them and deploy your app.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove the ‘sqlite3' gem and add the ‘pg’ gem to your gemfile&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fqlv2bakpc83jmusl0a9q.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%2Fqlv2bakpc83jmusl0a9q.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Remove specified versioning from ‘activerecord’ &amp;amp; ‘sinatra-activerecord’ gems in your gemfile if present.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fde0l03v584bvl0dbw7id.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%2Fde0l03v584bvl0dbw7id.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Delete the Gemfile.lock file and run ‘bundle install’ in your terminal.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate to your ‘DB’ folder and delete ‘development.sqlite’ , ‘test.sqlite’ &amp;amp; ‘schema.rb’ if those files exist. Ensure you only have a ‘migrate’ folder. Also make sure you are specifying your versioning for your ActiveRecord::Migration&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fv2vc4b820l2gyy2ymi5j.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%2Fv2vc4b820l2gyy2ymi5j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your ‘config’ folder and create a file ‘database.yml’, this is where we will set up our PostgreSQL database.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5u1rtm0vzxwk3aeqm9f6.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%2F5u1rtm0vzxwk3aeqm9f6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your environment.rb file and remove:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fayqr8idjitz7nazzqglh.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%2Fayqr8idjitz7nazzqglh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your ‘config.ru’ file and remove:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F8fl5byau017l9efg9hg0.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%2F8fl5byau017l9efg9hg0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;At this point, you need to be sure you have the Postgres app installed on your machine. Ensure that the app is running. Once it is running navigate back to your terminal and run the following commands.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rake db:create&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rake db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;With Postgres, you have to create the databases before you migrate them. When you migrate for the first time you will run ‘rake db:create’ to create the databases. You will only need to run this once unless you drop your databases.&lt;/p&gt;

&lt;p&gt;At this point, you will have switched from utilizing a ‘sqlite’ database to ‘postgres’ allowing you to now deploy your app and finish migrating your databases in Heroku. I hope this article was helpful in getting your app deployed if you were encountering the same issue as I did.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/sinatra-app-heroku-57a918f6c5be" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Ruby CLI - NFL Ticket Generator</title>
      <dc:creator>Jeff Swisher </dc:creator>
      <pubDate>Sat, 07 Nov 2020 18:41:44 +0000</pubDate>
      <link>https://dev.to/jtswisher/ruby-cli-nfl-ticket-generator-1046</link>
      <guid>https://dev.to/jtswisher/ruby-cli-nfl-ticket-generator-1046</guid>
      <description>&lt;p&gt;Tasked with my first project for Flatiron’s Software Engineering program I decided to first begin with some research on APIs. Now, this wasn’t the first time I had heard of an API but at this point in the program, we had not covered them in any capacity. However, my cohort had been given the opportunity of choosing either an API or Nokogiri to complete our first project.&lt;/p&gt;

&lt;p&gt;If you haven’t heard of Nokogiri before, it is a Ruby gem that allows you to parse the HTML of a website and operate on it via CSS selectors to retrieve specific pieces of information. Web scraping with Nokogiri had been covered in the program at this point and I felt comfortable using it thus far.&lt;br&gt;
However, with my current lack of knowledge and the more widespread use of APIs in the professional world, I decided to take the option of learning something new. At first, the thought of using an API to get the data I needed was very intimidating, however with the help of Postman and my chosen API’s documentation I ended up creating a pretty cool CLI program.&lt;/p&gt;

&lt;p&gt;We were tasked with building a Ruby gem that provides a Command Line Interface (CLI) to an external data source. I decided to use Ticketmaster’s API to build my CLI program. My program allows a user to enter in the name of any NFL team to return all future games for that team provided tickets are actively being sold.&lt;/p&gt;

&lt;p&gt;Once the team name has been entered by the user the program makes an API call to retrieve the following info for each available game:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Names for both teams in the game&lt;/li&gt;
&lt;li&gt;Date of game&lt;/li&gt;
&lt;li&gt;Time of game&lt;/li&gt;
&lt;li&gt;Game venue&lt;/li&gt;
&lt;li&gt;Game location — City/State&lt;/li&gt;
&lt;li&gt;URL — Direct link to the purchasing page for tickets to the game.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6Fv5B56L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h0loa6sii0df3xfnyl4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6Fv5B56L--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/h0loa6sii0df3xfnyl4w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Initial prompt &amp;amp; results of user input&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ruUrTXs5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vu2bp3jy30ot8fu79w6w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ruUrTXs5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/vu2bp3jy30ot8fu79w6w.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Results for selected game&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Application programming interface or API for short allows my program to access Ticketmaster’s system to search for NFL games based on the query parameters I have set in my API call. The query parameters allowed me to fine-tune the data I wanted to access from their system. Postman was great at allowing me to test these parameters and see exactly what they would return upon making the API call. Postman is a collaboration platform for API development amongst other things. For my needs, I utilized it for exploratory testing to see what values would be returned when the API calls were made.&lt;/p&gt;

&lt;p&gt;After reading the Ticketmaster API documentation I knew how to add onto the root URL that would be used for my API call with both my API key and my desired parameters. In Postman I used this URL to send a request to see the values returned to my program. Postman highlights the query parameters that you’re using and allows you to toggle them on/off to see different results from your API call which is a really useful feature.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--1iNQMtCi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8t76g8qob7vais75jm53.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--1iNQMtCi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8t76g8qob7vais75jm53.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Postman Interface — Sample results of API call&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once I was happy with the results from my API calls the real coding began. I wanted my program to return results based on the user’s input. Through my testing in Postman, I knew that I could enter any team name for the “keyword” query parameter when making my calls and I would get the results for that specific team back. At this point, I knew I could then just ask the user for their favorite team throughout the program and then interpolate that input into the URL as the “keyword” when making the API calls, in turn returning all games for the requested team.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6_hRZLTw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8dsgmt6ao9jijhu4sh6l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6_hRZLTw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/8dsgmt6ao9jijhu4sh6l.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;API class. The class method takes in the user's input as an argument and then interpolates into the URL.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Ticketmaster API returns its data in JavaScript Object Notation (JSON) format, this format being similar to a hash in Ruby. While a hash is formatted using “key”/”value” pairs, JSON is formatted using “name”/”value” pairs. This allowed me to easily iterate over the data set to capture the data I needed for my instance variables for each game object. After each iteration through the data set the above class method creates a new “Games” object until all games have been created for the given team.&lt;/p&gt;

&lt;p&gt;The remainder of the coding needed was to manage the game objects being created and creating a CLI interface for the user to utilize when running the program. Overall this project was a great learning experience that helped me step out of my comfort zone to create something tangible that I can show my family and friends. Going forward I would like to restructure the program to allow users to access ticket information for not just NFL games but many of the other mainstream sports as well.&lt;/p&gt;

&lt;p&gt;This article was migrated over from Medium. The original article can be found &lt;a href="https://medium.com/@jefftswisher/ruby-cli-nfl-ticket-generator-479a155da390"&gt;here&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
  </channel>
</rss>
