<?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: chrisding7</title>
    <description>The latest articles on DEV Community by chrisding7 (@chrisding7).</description>
    <link>https://dev.to/chrisding7</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%2F929159%2F0a2a8a5b-17ad-4a5c-be71-f17c34ba7797.png</url>
      <title>DEV Community: chrisding7</title>
      <link>https://dev.to/chrisding7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/chrisding7"/>
    <language>en</language>
    <item>
      <title>Basic Sorting Algorithms</title>
      <dc:creator>chrisding7</dc:creator>
      <pubDate>Tue, 22 Nov 2022 21:15:30 +0000</pubDate>
      <link>https://dev.to/chrisding7/basic-sorting-algorithms-3e4h</link>
      <guid>https://dev.to/chrisding7/basic-sorting-algorithms-3e4h</guid>
      <description>&lt;p&gt;Whether in real life or in the world of programming, being able to sort through data is tremendously useful. Think of how many times you have wished a mess would clean itself up on its own! &lt;strong&gt;Sorting algorithms&lt;/strong&gt; are commonly written as a solution to these types of problems in programming.&lt;/p&gt;

&lt;p&gt;So, what is a &lt;strong&gt;sorting algorithm&lt;/strong&gt;? A sorting algorithm is an algorithm that is used to rearrange and &lt;em&gt;sort&lt;/em&gt; a given array or list of elements. There are dozens of well-known sorting algorithms, each with their own unique manner of sorting and rearranging elements. Some are chosen over others for their ease of implementation, while others are chosen for their superior time complexity when run against larger collections of data. Today, we will focus on a few of the more basic sorting algorithms and visualize how they differ from one another.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selection Sort
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Selection sort&lt;/strong&gt; is often viewed as the most basic of sorting algorithms, and chances are that if you have sorted an array at a beginner level, it was likely using selection sort (or something similar). Think of holding playing cards in your hand--to sort them in ascending order, most people would look for the lowest numbers first (in this case let's say 2's) and place them towards the left of their hand. Next, they would look for all of the next highest numbers and place them towards the left, and so on.&lt;/p&gt;

&lt;p&gt;In coding terms, imagine an unsorted array &lt;code&gt;[34, 25, 14, 22, 11]&lt;/code&gt;. Starting at the index of the &lt;u&gt;first unsorted value&lt;/u&gt;, selection sort will traverse the entire array from left to right, replacing the &lt;u&gt;first unsorted value&lt;/u&gt; with the local minimum of the traversed elements. Everything to the left of the &lt;u&gt;next unsorted value&lt;/u&gt; will be considered &lt;em&gt;sorted&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;34&lt;/strong&gt;         25          14          22          11 &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11&lt;/strong&gt;         25          14          22          34&lt;br&gt;&lt;br&gt;
11         &lt;strong&gt;25&lt;/strong&gt;          14          22          34&lt;br&gt;&lt;br&gt;
11         &lt;strong&gt;14&lt;/strong&gt;          25          22          34&lt;br&gt;&lt;br&gt;
11         14          &lt;strong&gt;25&lt;/strong&gt;          22          34&lt;br&gt;&lt;br&gt;
 11        14          &lt;strong&gt;22&lt;/strong&gt;          25          34&lt;br&gt;&lt;br&gt;
 11        14          22          &lt;strong&gt;25&lt;/strong&gt;          34&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11          14          22          25          34&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdn.emre.me/sorting/selection_sort.gif"&gt;SelectionSort Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Insertion Sort
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Insertion sort&lt;/strong&gt; is also a relatively basic sorting algorithm that is widely used for smaller datasets. It also involves splitting a collection of elements into a &lt;em&gt;sorted&lt;/em&gt; and &lt;em&gt;unsorted&lt;/em&gt; portion and comparing elements between the two. Let us use our previous unsorted array for this example: &lt;code&gt;[34, 25, 14, 22, 11]&lt;/code&gt; &lt;/p&gt;

&lt;p&gt;Insertion sort will initially compare the first two elements of the array and swap them if unsorted. in this case, &lt;strong&gt;25&lt;/strong&gt; has been swapped with &lt;strong&gt;34&lt;/strong&gt; and is considered part of a &lt;em&gt;sorted&lt;/em&gt; sub-portion of our array.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;34&lt;/strong&gt; &lt;strong&gt;25&lt;/strong&gt; 14 22 11&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25&lt;/strong&gt; &lt;strong&gt;34&lt;/strong&gt; 14 22 11&lt;/p&gt;

&lt;p&gt;Next, &lt;strong&gt;34&lt;/strong&gt; is compared with &lt;strong&gt;14&lt;/strong&gt;, the next unsorted element in our array, and they are swapped. Now, &lt;strong&gt;14&lt;/strong&gt; will be compared with our "sorted" sub-portion and will be swapped with &lt;strong&gt;25&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;25 &lt;strong&gt;34&lt;/strong&gt; &lt;strong&gt;14&lt;/strong&gt; 22 11&lt;/p&gt;

&lt;p&gt;25 &lt;strong&gt;14&lt;/strong&gt; &lt;strong&gt;34&lt;/strong&gt; 22 11&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14&lt;/strong&gt; &lt;strong&gt;25&lt;/strong&gt; 34 22 11&lt;/p&gt;

&lt;p&gt;This process repeats until the collection has been sorted:&lt;/p&gt;

&lt;p&gt;14 25 &lt;strong&gt;34&lt;/strong&gt; &lt;strong&gt;22&lt;/strong&gt; 11&lt;/p&gt;

&lt;p&gt;14 25 &lt;strong&gt;22&lt;/strong&gt; &lt;strong&gt;34&lt;/strong&gt; 11&lt;/p&gt;

&lt;p&gt;14 &lt;strong&gt;22&lt;/strong&gt; &lt;strong&gt;25&lt;/strong&gt; 34 11&lt;/p&gt;

&lt;p&gt;14 22 25 &lt;strong&gt;34&lt;/strong&gt; &lt;strong&gt;11&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;14 22 25 &lt;strong&gt;11&lt;/strong&gt; &lt;strong&gt;34&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;14 22 &lt;strong&gt;11&lt;/strong&gt; &lt;strong&gt;25&lt;/strong&gt; 34&lt;/p&gt;

&lt;p&gt;14 &lt;strong&gt;11&lt;/strong&gt; &lt;strong&gt;22&lt;/strong&gt; 25 34&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11&lt;/strong&gt; &lt;strong&gt;14&lt;/strong&gt; 22 25 34&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11 14 22 25 34&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdn.emre.me/sorting/insertion_sort.gif"&gt;InsertionSort Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Merge Sort
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Merge sort&lt;/strong&gt; is a slightly more complex algorithm that utilizes &lt;strong&gt;recursion&lt;/strong&gt; to sort data. If you haven't worked with recursion, learning about the merge sort algorithm is also a great introduction to recursion. Let's use an array with a couple of more elements to demonstrate merge sort: &lt;code&gt;[38, 27, 43, 3, 9 , 82, 10]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Merge sort will initially split the array into equal halves (or in the case of odd-numbered elements, almost-equal halves). Then, it will recursively split each half into smaller halves again and again until each split "half" cannot be split anymore, such that each recursively-split "half" represents a single element of the original array. The halves are then compared with their prior-split counterparts and sorted in small groups. These small groups are then compared with the next prior-split small group and sorted again--this process is repeated until all of the groups have been merged back together in sorted order. It is important to note that merge sort will prioritize &lt;strong&gt;completely sorting&lt;/strong&gt; each "half" or "split-group" before merging halves back together. The diagram below shows the steps in which the merge sort algorithm acts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gIFYcvU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kidw91dygvcsaq7wq1br.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gIFYcvU6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kidw91dygvcsaq7wq1br.png" alt="MergeSort" width="657" height="646"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://cdn.emre.me/sorting/merge_sort.gif"&gt;MergeSort Demo&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why so many algorithms?
&lt;/h2&gt;

&lt;p&gt;The main reason sorting algorithms are implemented are to save time in the long-run, but what happens when you have to choose between these different sorting algorithms to save time?  Using our playing card example, it is easy to imagine looking through 5 or so cards and sorting them using an algorithm like selection sort--but how about when the hand gets to 20, 100, even 1000 cards? It's much easier to imagine dividing that hand up between 2, 4, even 8 or more people and utilizing something like merge sort! &lt;/p&gt;

&lt;p&gt;Some sorting algorithms perform more efficiently than others on average, and that's just the way it is. However, some algorithms are easier to implement when saving time is not an issue, such as implementing selection sort and insertion sort when working with very small sets of data. While saving time is important, saving the hassle of writing difficult code is also crucial to many programmers!&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Introduction to Linked Lists</title>
      <dc:creator>chrisding7</dc:creator>
      <pubDate>Tue, 25 Oct 2022 08:52:49 +0000</pubDate>
      <link>https://dev.to/chrisding7/introduction-to-linked-lists-23e6</link>
      <guid>https://dev.to/chrisding7/introduction-to-linked-lists-23e6</guid>
      <description>&lt;p&gt;&lt;strong&gt;Linked lists&lt;/strong&gt; are a relatively common type of data structure that you may run into throughout your programming journey. They are especially common in technical job interviews as well as acting as building blocks for some more complex data structures. As a result, they are very important for budding programmers to familiarize themselves with.&lt;/p&gt;

&lt;p&gt;Linked lists can be compared to arrays, as they have similar structures and functions. However, each will have its own distinct advantages and disadvantages that will dictate how and when you utilize them. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E4edltGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x3tm09glvz0cnlersab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E4edltGp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2x3tm09glvz0cnlersab.png" alt="1-D Array" width="880" height="164"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As shown above, you can visualize an array as one large "box" that is partitioned into several smaller sections that each contain a data point, such as an integer. Linked lists are very similar--instead of one big box, think of a linked list as several small boxes that are connected to each other. Each box will contain data  and "point" to the next box in the list, hence the name: linked lists:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--timBZCcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41lbifscuvcj9pak5lvv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--timBZCcs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/41lbifscuvcj9pak5lvv.png" alt="Linked List" width="621" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each "box" in a linked list is oftentimes referred to as a &lt;strong&gt;node&lt;/strong&gt;. Additionally, the first and last node in a linked list are usually called the &lt;strong&gt;head&lt;/strong&gt; and &lt;strong&gt;tail&lt;/strong&gt; nodes respectively. &lt;/p&gt;

&lt;p&gt;Now that we know what a linked list is, how do we implement it with code? With object-oriented programming in mind, we will want to eventually have a class for both our Node and our Linked List, but for simplicity's sake, we will be building a simple linked list with only Node objects. Starting with our Node class, we want to define a Node to have two properties: &lt;em&gt;data&lt;/em&gt; and &lt;em&gt;next&lt;/em&gt;. Data will hold the actual data within the node, and next will hold the pointer to the next node in the list (which will be initialized as &lt;em&gt;null&lt;/em&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
    constructor(data)
    {
        this.data = data;
        this.next = null
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we wanted to recreate the linked list pictured above, we would create our first Node and assign it to a new variable, &lt;em&gt;head&lt;/em&gt;, with its initial value of &lt;em&gt;1&lt;/em&gt;. We would then have to create the rest of the nodes and assign their pointers to each other, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const head = new Node(1);
const nodeB = new Node(2);
const nodeC= new Node(3);
const nodeD = new Node(4);
const nodeE= new Node(5);

head.next = nodeB;
nodeB.next = nodeC;
nodeC.next = nodeD;
nodeD.next = nodeE;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that &lt;code&gt;nodeE&lt;/code&gt; never has its &lt;code&gt;.next&lt;/code&gt; assigned to a Node and remains &lt;code&gt;null&lt;/code&gt;. We have created a &lt;strong&gt;singly linked list&lt;/strong&gt;, which means our linked list is one-directional and only points forward. This makes it easy for us to write code that will recognize when the end of the linked list has been reached. For example, if we wanted to write a function that counted the number of nodes in a linked list (&lt;em&gt;think 'length of an array' as a comparison&lt;/em&gt;), we could implement it as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function countNodes(head) {
    let counter = 1;
    let currentNode = head;
    while (currentNode.next != null) {
        currentNode = currentNode.next;
        counter += 1;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have just implemented a function that steps through a linked list from its head node and increments a counter variable each time a node successfully points to another node that is not &lt;code&gt;null&lt;/code&gt;! The code snippets above are oversimplified, and creating a LinkedList class would clean up our code and allow us to write more complex functions for other purposes, such as inserting and removing nodes from a linked list. However, the basics of linked lists include the structure of a node and how the pointers affect the interactions between nodes.&lt;/p&gt;

&lt;p&gt;The next question you may have is, "when would I use a linked list?" A small disadvantage of linked lists is that indexing or searching through a linked list takes linear time, meaning that it has to traverse the entire structure (and therefore every element) while indexing or searching. However, linked lists are able to insert and delete nodes with more efficient time complexity than arrays. They are also used as a foundation for other complex data structures, such as &lt;strong&gt;trees&lt;/strong&gt;. Try and see if you can recognize the linked list structure in the tree pictured below! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KF-voLRd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ifhgzz98h4p0n1qmwtx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KF-voLRd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ifhgzz98h4p0n1qmwtx.png" alt="Binary Tree" width="861" height="521"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Stacks and Queues in JavaScript</title>
      <dc:creator>chrisding7</dc:creator>
      <pubDate>Tue, 11 Oct 2022 10:27:44 +0000</pubDate>
      <link>https://dev.to/chrisding7/stacks-and-queues-in-javascript-4fdm</link>
      <guid>https://dev.to/chrisding7/stacks-and-queues-in-javascript-4fdm</guid>
      <description>&lt;p&gt;Once you have familiarized yourself with arrays in JavaScript, it's time to learn about some more types of data structures! &lt;strong&gt;Stacks&lt;/strong&gt; and &lt;strong&gt;queues&lt;/strong&gt; are quite similar in functionality to arrays, but have specific instances in which they can achieve better performance. Stacks and queues are relatively intuitive, and have plenty of real-world cases in which developers will find themselves implementing these useful data structures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stacks
&lt;/h2&gt;

&lt;p&gt;Instead of visualizing a stack as a "horizontal" structure like an array, try and imagine a stack as a "vertical" structure, like a &lt;em&gt;stack&lt;/em&gt; of plates piling on top of one another. You can think of a stack as a data structure in which you only insert and delete elements from one side of the structure. This is a common principle in programming called &lt;em&gt;last in first out&lt;/em&gt; (LIFO). In other words, the elements inserted the latest at the top of the stack are also the first to be accessed and deleted. Using our plate analogy, the plates placed on top of the stack will be the easiest to grab and remove. &lt;/p&gt;

&lt;p&gt;When working with stacks, the two most important operations used are &lt;strong&gt;push&lt;/strong&gt; and &lt;strong&gt;pop&lt;/strong&gt;--methods that we should recognize from working with arrays. Push is a destructive method that will insert a new element to the end (top) of our stack. Pop is also a destructive method, and will remove the most recent element from the stack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wpp_va4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9cqdnkcmy5kidwpwjt7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wpp_va4E--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9cqdnkcmy5kidwpwjt7.png" alt="LIFO Stack" width="260" height="194"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While hard to notice at first, real-world examples of stacks are extremely common in web development. When searching the web and using a browser, your browsing history is constantly being logged using a stack. If you wish to return to your most recently visited page, a simple click of the "back" button will take you there. Additionally, we have all made use of the "undo" functionality when typing a document or using a text editor. Similar to browsing history, actions are tracked so that they can easily be accessed and "undone" using stacks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;The simplest way to implement a stack in JavaScript is to utilize the push and pop array methods that the language already provides us with. Therefore, we can create an array and use the push and pop methods to create our operations and give us a working stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const stack = [];
const firstElement = 1;
const secondElement = 2;

const push = (element) =&amp;gt; stack.push(element);
const pop = () =&amp;gt; stack.pop();

push(firstElement);
push(secondElement);

console.log(stack) // [1, 2]
pop();
console.log(stack) // [1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you may be thinking that this just looks like an ordinary array that solely uses the push and pop methods--and you would be correct. In such a case that we can utilize an array as a stack, we should! This is because the &lt;strong&gt;time complexity&lt;/strong&gt; of the push and pop methods will always be &lt;em&gt;O(1)&lt;/em&gt;. In simple terms, no matter what the size of the stack is, the time spent executing these operations will remain the same. (More on time complexity can be read &lt;a href="https://www.geeksforgeeks.org/understanding-time-complexity-simple-examples/"&gt;here&lt;/a&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  Queues
&lt;/h2&gt;

&lt;p&gt;Now that we understand stacks, queues should also be relatively intuitive to understand. Instead of a stack of plates, a queue can simply be thought of as a line of people waiting for something. They could be waiting in line for movie tickets, a restaurant reservation, or even the restroom, but will follow the same rule of thumb--first come, first served. If stacks were associated with &lt;em&gt;last in first out&lt;/em&gt; (LIFO), queues are associated with &lt;em&gt;first in first out&lt;/em&gt; (FIFO).  &lt;/p&gt;

&lt;p&gt;When working with queues, the two most important operations used are &lt;strong&gt;enqueue&lt;/strong&gt; and &lt;strong&gt;dequeue&lt;/strong&gt;. Enqueue will be responsible for adding a new element to the end of our queue (back of the line), and dequeue will be responsible for removing the oldest element from the front of our queue (first person in line).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5Ft-iJ8b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkr9g9o18zwnc7j9i4l3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5Ft-iJ8b--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bkr9g9o18zwnc7j9i4l3.png" alt="FIFO Queue" width="390" height="129"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;Like implementing a stack, the simplest way to implement a queue in JavaScript is to utilize existing methods provided to us. Since the enqueue operation works the same as the push operation, we will use the push array method to implement enqueue. For dequeue, JavaScript yet again provides us with an existing array method to remove the first element of an array: &lt;strong&gt;shift&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const queue = [];
const firstElement = 1;
const secondElement = 2;

const enqueue = (element) =&amp;gt; queue.push(element);
const dequeue = () =&amp;gt; queue.shift();

enqueue(firstElement);
enqueue(secondElement);

console.log(queue) // [1, 2]
dequeue();
console.log(queue) // [2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should be noted that this is a simple method of creating a queue, but may not necessarily be the most efficient. One interesting thing to note about the time complexity of our stack and queue is that both our push and pop operations from our stack have a time complexity of &lt;strong&gt;O(1)&lt;/strong&gt;, while our dequeue operation will have a time complexity of &lt;strong&gt;O(n)&lt;/strong&gt;. This is because the shift array method is used in the implementation of dequeue and shares the same time complexity. Nevertheless, both stacks and queues can be extremely useful in their respective use-case scenarios. They can each help us organize data in sequential order while retaining efficiency in our code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>JavaScript Arrays for Beginners</title>
      <dc:creator>chrisding7</dc:creator>
      <pubDate>Tue, 20 Sep 2022 05:28:20 +0000</pubDate>
      <link>https://dev.to/chrisding7/javascript-arrays-for-beginners-4cnd</link>
      <guid>https://dev.to/chrisding7/javascript-arrays-for-beginners-4cnd</guid>
      <description>&lt;p&gt;Picture yourself meeting a new friend or acquaintance for the first time. One of the first things that you probably learn about them is their name, right? In JavaScript, you have probably learned about &lt;strong&gt;variables&lt;/strong&gt;, which are pivotal for your JavaScript foundation when first learning how to program. Variables are extremely important because they help us to store pieces of information, such as a name, so that we can access this information later and don't have to metaphorically ask for someone's name over and over again!&lt;/p&gt;

&lt;p&gt;However, remembering a single name is not particularly difficult...but what happens when you have to start remembering tens, hundreds, or even thousands of names? &lt;strong&gt;Arrays&lt;/strong&gt; help us with this problem, as they are one of the most common types of data structures found not only in JavaScript, but in almost every existing programming language. If you are unfamiliar with data structures, they are essentially "data containers" that help us organize and access data when we are programming. Today, we will be learning about and working with arrays, a relatively simple and straightforward type of data structure widely used by programmers everywhere.&lt;/p&gt;

&lt;p&gt;You can think about arrays as a "list", where the order of items matter: &lt;br&gt;
&lt;code&gt;[1, 2, 3]&lt;/code&gt; is not the same as &lt;code&gt;[1, 3, 2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Although the two arrays above contain the same elements, these arrays are unique and distinct. &lt;/p&gt;

&lt;p&gt;Another special note about JavaScript arrays are that the data types within an array can be mixed together: &lt;code&gt;[1, "1", null]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we see that we have a number (integer), a String, and a null value all contained within a single array. This property of JavaScript arrays is not universally shared across all programming languages, so it is important to know how an array handles properties in your language of choice.&lt;/p&gt;

&lt;p&gt;So how do we create a JavaScript array? As shown above, you can create an array using &lt;strong&gt;array literal&lt;/strong&gt; syntax and fill a pair of square brackets with elements of your choosing separated by commas. Logically, you can also assign an array like this to a variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myArray = ["This", "is", "array", "literal", "syntax"];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Checking the length of an array is also as easy as using the built-in &lt;code&gt;length&lt;/code&gt; property of JavaScript arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myArray.length;
// =&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we can create arrays, what can we do with them? We will often find ourselves wanting to access the elements within an array, using &lt;strong&gt;bracket notation&lt;/strong&gt;. Bracket notation utilizes the fact that each element of an array has a designated &lt;strong&gt;index&lt;/strong&gt;, or "place in line" within the array. It is crucial to note that the index of the first element in an array &lt;strong&gt;starts at 0!&lt;/strong&gt; Therefore, the last element in an array will always have an index of &lt;code&gt;Array.length - 1&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myArray[0];
// =&amp;gt; "This"
myArray[myArray.length - 1];
// =&amp;gt; "syntax"
myArray[4];
// =&amp;gt; "syntax"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What if we want to update existing values within an array? We can do that with bracket notation too!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myArray[0] = "Now THIS";
myArray;
// =&amp;gt; ["Now THIS", "is", "array", "literal", "syntax"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you may have noticed, we can freely reassign the individual elements within an array using the &lt;code&gt;=&lt;/code&gt; operator despite the fact that &lt;code&gt;myArray&lt;/code&gt; was declared using &lt;code&gt;const&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Now that you have the basics of arrays down, we can move onto the more complex intricacies of arrays! In my next blog post, I will be going over adding and removing array elements, iterating and looping through arrays, as well as how built-in &lt;strong&gt;array methods&lt;/strong&gt; can help solve many common tasks we face when using arrays.&lt;/p&gt;

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