<?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: Rachel Williams</title>
    <description>The latest articles on DEV Community by Rachel Williams (@racheladaw).</description>
    <link>https://dev.to/racheladaw</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%2F168801%2F051f0cc9-6cca-486f-8b2a-1148d7a5a121.jpg</url>
      <title>DEV Community: Rachel Williams</title>
      <link>https://dev.to/racheladaw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/racheladaw"/>
    <language>en</language>
    <item>
      <title>Express.js vs. Rails</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Fri, 22 May 2020 22:55:44 +0000</pubDate>
      <link>https://dev.to/racheladaw/express-js-vs-rails-23a5</link>
      <guid>https://dev.to/racheladaw/express-js-vs-rails-23a5</guid>
      <description>&lt;h3&gt;
  
  
  Why I Am Comparing These Frameworks
&lt;/h3&gt;

&lt;p&gt;I am about to start working on a mobile application with another developer. We decided to use React Native for the front end since I already have experience with React. However, we have been trying to figure out what framework to use for our back end. Since I have never used Express before, I wanted to dive into the differences between Rails and Express. Additionally, during my job search I have seen more companies listing Node and Express in job descriptions rather than Rails. It seems like Express would be a good tool to learn and I really enjoy programming in JavaScript, however I also enjoy the simplicity of Rails.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Express?
&lt;/h3&gt;

&lt;p&gt;Express is a web framework for creating the server-side of web applications. It utilizes Node.js and just like Rails, it uses an MVC pattern to control back end application logic. It contains HTTP utility methods, a wide variety of middleware that can be included, and many other features to make building a web application with JavaScript easier. Some people have even compared Express to Sinatra which is more of a lower-level Ruby framework than Rails.&lt;/p&gt;

&lt;h3&gt;
  
  
  Comparing the Frameworks
&lt;/h3&gt;

&lt;p&gt;On first glance, it was pretty obvious to me that Express was much more minimalistic than Rails. Rails is a very large framework and is very opinionated about the way an application should be built. There are model files, controller files, view files, a routes file and much, much more. Express is much more lightweight and there is more flexibility on how you structure your application.&lt;/p&gt;

&lt;h4&gt;
  
  
  Rails Pros and Cons
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;p&gt;Rails comes with everything you need to create a web application out of the box, including built in support for a SQLite database. Since it is optimized for web applications and has a lot of packages included, creating a standard web application is super fast for developers. Ruby syntax is very intuitive and easy to understand for developers. Since the framework is opinionated, if you look at another developer's Rails application it will be relatively simple to understand how it works.&lt;/p&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;p&gt;Because Rails comes with so many packages, it can be quite slow and heavy. Additionally, since it is quite opinionated about the structure for your application, there is little flexibility and it can be frustrating to work outside of that box. Along with that, the highly abstracted code in Rails can be difficult to debug since the developer is so far removed from the underlying functionality of the framework. One more thing to note, is that it sadly appears that Rails is not being used by companies much anymore.&lt;/p&gt;

&lt;h4&gt;
  
  
  Express Pros and Cons
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;p&gt;Express is super lightweight and fast. It comes with minimal packages out of the box and developers can add middleware as needed. It is highly customizable and flexible, unlike Rails which keeps the structure of applications relatively similar. It is one of the most supported Node frameworks, so developers can ensure that improvements are being made frequently.&lt;/p&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;p&gt;Due to the fact that Express is lightweight, it can require more lines of code than Rails to create the same application. Rails comes with generators that allow you to build out models, controllers, serializers and more super fast. Express will require more time to code the application how you want it to be.&lt;/p&gt;

&lt;h3&gt;
  
  
  Decisions
&lt;/h3&gt;

&lt;p&gt;I think after all of my research, I am leaning towards using Express. Since it is lightweight, super fast, and built on top of Node, I think it will be a great tool to learn. We will probably end up using MongoDB / Mongoose along with Express to align with the MERN stack.&lt;/p&gt;

&lt;p&gt;Let me know if you guys have created applications with Express and Rails and what your thoughts are on these frameworks. Thank you for reading!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/116wwYf3ajIvrG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/116wwYf3ajIvrG/giphy.gif" alt="Napoleon Dynamite GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Additional Resources
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://medium.com/front-end-weekly/node-js-express-js-vs-ruby-on-rails-835a8112db1"&gt;Medium Blog&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://expressjs.com/"&gt;Express Docs&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.freecodecamp.org/news/node-js-what-when-where-why-how-ab8424886e2/"&gt;Node JS Info&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Browser Fingerprinting</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 17 May 2020 05:09:06 +0000</pubDate>
      <link>https://dev.to/racheladaw/browser-fingerprinting-5elj</link>
      <guid>https://dev.to/racheladaw/browser-fingerprinting-5elj</guid>
      <description>&lt;h3&gt;
  
  
  What is Fingerprinting?
&lt;/h3&gt;

&lt;p&gt;Recently I had an interview where my interviewer mentioned fingerprinting as a way to recognize a particular user. I hadn't heard much about it and was intrigued so I decided to do some research on the topic.&lt;/p&gt;

&lt;p&gt;Browser fingerprinting is a method used to partially or fully identify a user based on information collected about the user's browser, computer, preferred language and more. Although you might expect that many people would have the same setup on their device as you, it is rare that two user's fingerprint is similar. In fact, according to this &lt;a href="https://pixelprivacy.com/resources/browser-fingerprinting/"&gt;article&lt;/a&gt;, "only 1 in 286,777 other browsers will share the same fingerprint as another user."&lt;/p&gt;

&lt;h3&gt;
  
  
  How Fingerprinting Works
&lt;/h3&gt;

&lt;p&gt;Most tech-savvy people are aware that your web traffic can be tracked with cookies or that your location can be determined with your IP address. However, fingerprinting can still partially or fully identify you without either of these things.&lt;/p&gt;

&lt;p&gt;With fingerprinting, client-side scripting is used to collect a wide variety of data about you including your operating system, OS version, browser, browser version, screen resolution, list of plugins and much, much more.&lt;/p&gt;

&lt;p&gt;Fingerprinting doesn't require cookies. JavaScript knows information about your screen resolution, device size and more to be able to display web applications properly. If you open your developer tools, and type &lt;code&gt;navigator.userAgent&lt;/code&gt; you will see your device's user-agent in the console. This has information about your browser, computer, operating system and more. There are other commands you can use such as &lt;code&gt;navigator.plugins&lt;/code&gt; to view what plugins you have installed. &lt;/p&gt;

&lt;h5&gt;
  
  
  HTML5 Canvas
&lt;/h5&gt;

&lt;p&gt;Another way for companies to get data for fingerprinting is by using HTML5's canvas element. This element is used to draw graphics on a web page via JavaScript. Developers will use this feature to draw graphics on your device. Since different devices render images differently, the rendered canvas graphic can be sent through a hash function to create a unique ID for fingerprinting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is Fingerprinting Used?
&lt;/h3&gt;

&lt;p&gt;These days, companies are doing everything they can to collect data about their users. This data can be used to improve marketing, improve the user experience, it can be sold to advertisers, and more. However, it is also used for security and authentication and in fact was originally developed to prevent security risks for users. Ultimately though, fingerprinting is a privacy issue and I think everyone should be aware of it. &lt;/p&gt;

&lt;h3&gt;
  
  
  Can You Stop It?
&lt;/h3&gt;

&lt;p&gt;There are different ways to prevent fingerprinting if you are worried about your privacy. You can block all JavaScript with an add-on such as &lt;a href="https://addons.mozilla.org/en-US/firefox/addon/noscript/"&gt;NoScript&lt;/a&gt;, however this will change the functionality of many websites unless you allow JavaScript to run on them. You can try to use the most popular browser and version, but this is quite hard to do.&lt;/p&gt;

&lt;p&gt;The most promising option I found is that the newest &lt;a href="https://blog.mozilla.org/firefox/how-to-block-fingerprinting-with-firefox/"&gt;Firefox&lt;/a&gt; browser protects you against fingerprinting. It blocks "third-party requests to companies that are known to participate in fingerprinting." &lt;/p&gt;

&lt;h3&gt;
  
  
  Are you Unique?
&lt;/h3&gt;

&lt;p&gt;If you would like to see if you are unique, check out this &lt;a href="https://amiunique.org/"&gt;website&lt;/a&gt;. The website checks things like your list of plugins, language header, timezone, presence of adblock and more to see if you are unique.&lt;/p&gt;

&lt;h3&gt;
  
  
  Thank You
&lt;/h3&gt;

&lt;p&gt;Thank you for taking the time to read this post. What are your thoughts on internet privacy and fingerprinting?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/26tn66XDQtXadfWrS/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/26tn66XDQtXadfWrS/giphy.gif" alt="Fingerprint Gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Additional Resources
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://blog.mozilla.org/internetcitizen/2018/07/26/this-is-your-digital-fingerprint/"&gt;Mozilla Blog on Fingerprinting&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>security</category>
    </item>
    <item>
      <title>A Few Notes on JavaScript Events</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Mon, 11 May 2020 00:49:15 +0000</pubDate>
      <link>https://dev.to/racheladaw/a-few-notes-on-javascript-events-9ke</link>
      <guid>https://dev.to/racheladaw/a-few-notes-on-javascript-events-9ke</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;This blog is going to go over some helpful information about JavaScript events that you may, or may not already know. This blog was inspired by the &lt;a href="https://podcasts.apple.com/us/podcast/syntax-tasty-web-development-treats/id1253186678?i=1000473649382"&gt;Syntax Web Development Podcast&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Bubbling
&lt;/h3&gt;

&lt;p&gt;Let's say you have this HTML code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;div onclick="alert("Button Clicked")"&amp;gt;
  &amp;lt;p&amp;gt;
    &amp;lt;button&amp;gt;Click Me&amp;lt;/button&amp;gt;
  &amp;lt;/p&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If a user clicked the button on the HTML page, the onclick event handler would be triggered in the div element. How is this possible? Well, events bubble up, meaning that handlers are called starting from the element where the event occurred and up through each of its ancestors. If there was an onclick event listener for the button, and a user clicked the button, the button's handler would get called first and then the div element's handler.&lt;/p&gt;

&lt;p&gt;Think of it like a bubble starting from the bottom of a cup and floating up to the top, triggering handlers on the way up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/daavV7tQReSlpWQnI4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/daavV7tQReSlpWQnI4/giphy.gif" alt="Event Bubbling GIF"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Please ignore my horrible art skills&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Capturing
&lt;/h3&gt;

&lt;p&gt;Event capturing is the opposite of event bubbling. Using our example from above, if the button was clicked, the event would be handled by the outermost div, then the inner div, and then the button. This is also called "trickling," since the event trickles down the chain.&lt;/p&gt;

&lt;p&gt;Both bubbling and capturing are phases of event propagation. Basically the main difference is the order in which the element's receive the event. For this reason, most people prefer event bubbling since the element they want to target is usually the one that the user will be interacting with. Event bubbling is the default propagation mode.&lt;/p&gt;

&lt;p&gt;However, you can specify if you want the event to trigger during the capturing phase. One reason you might want to do this is that some events don't bubble up, like the play event. To use event capturing, you will set the &lt;code&gt;useCapture&lt;/code&gt; argument to true. This is the third argument to &lt;code&gt;addEventListener()&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  event.preventDefault()
&lt;/h3&gt;

&lt;p&gt;Most programmers that have used a JavaScript framework know about this one. This method allows you to prevent the default action of an element. For example, when submitting forms the default action is to reload the page. However, with single-page applications usually you don't want this behavior and so you can use &lt;code&gt;event.preventDefault()&lt;/code&gt; to prevent it.&lt;/p&gt;

&lt;p&gt;Another example for when you might want to use this is if you want to prevent specific keystrokes from showing up in a text field. See the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault"&gt;MDN docs&lt;/a&gt; for an example of this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Target vs. currentTarget
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;target&lt;/code&gt; and &lt;code&gt;currentTarget&lt;/code&gt; are properties of events. &lt;code&gt;target&lt;/code&gt; is the element which triggered the event. For a click event, this would be the element that was clicked on. &lt;code&gt;currentTarget&lt;/code&gt; is the current target for the event as the event traverses the DOM via capturing or bubbling. The current target is the element that the event listener is attached to.&lt;/p&gt;

&lt;p&gt;This comes in handy for event delegation, where you can attach one event listener for a bunch of child elements. For example if you had a few buttons nested within the same &lt;code&gt;div&lt;/code&gt; element, you can register an event listener for the &lt;code&gt;div&lt;/code&gt; and use &lt;code&gt;event.target&lt;/code&gt; to see which button was clicked. &lt;code&gt;event.currentTarget&lt;/code&gt; will be your &lt;code&gt;div&lt;/code&gt; element with all of the nested buttons inside.&lt;/p&gt;

&lt;p&gt;Event delegation can save a lot of time because instead of adding event listeners to a bunch of elements, you only have to add it to a common ancestor.&lt;/p&gt;

&lt;h3&gt;
  
  
  event.isTrusted()
&lt;/h3&gt;

&lt;p&gt;This method will check if the event was created by a user or is a fake event. It will return true if the event was created by a user action and it will return false if it was created by a script or dispatched.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding an Event Listener Once
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;addEventListener()&lt;/code&gt; method has a parameter called &lt;code&gt;once&lt;/code&gt; that accepts a boolean. If this parameter is set to true, then the event listener will only be invoked, at most, once. The listener will be automatically removed once invoked.&lt;/p&gt;

&lt;p&gt;One use case for this parameter is for a &lt;code&gt;load&lt;/code&gt; event. Since a page only loads once, you may want to automatically remove this listener after the page is loaded.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;In this blog, I wrote about a few JavaScript event tidbits that you may or may not know about. What other methods or tricks regarding events do you know about?&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o7qDJKK4LCK7qRb56/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o7qDJKK4LCK7qRb56/giphy.gif" alt="Happy Mother's Day"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  Additional Resources
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/4616694/what-is-event-bubbling-and-capturing"&gt;Stack Overflow Post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/52409223/reasoning-for-usecapture-in-addeventlistener"&gt;Stack Overflow Post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/@bretdoucette/part-4-what-is-event-delegation-in-javascript-f5c8c0de2983"&gt;Blog Post&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener"&gt;MDN Docs&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>My Take on Comparing Angular and React</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 03 May 2020 21:10:27 +0000</pubDate>
      <link>https://dev.to/racheladaw/my-take-on-comparing-angular-and-react-4gem</link>
      <guid>https://dev.to/racheladaw/my-take-on-comparing-angular-and-react-4gem</guid>
      <description>&lt;h3&gt;
  
  
  Background
&lt;/h3&gt;

&lt;p&gt;I recently decided to build a project with Angular to learn the framework. Before this, the main JavaScript framework that I had used was React. After seeing some of the similarities and differences between the two, I decided it would be good to dive deeper into why I might want to use one over the other.&lt;/p&gt;

&lt;h3&gt;
  
  
  My First Impressions
&lt;/h3&gt;

&lt;p&gt;The first thing I noticed about Angular was how similar it was to React. It has a main &lt;code&gt;index.html&lt;/code&gt; file, which serves as the template, just like React. It also has a &lt;code&gt;main.ts&lt;/code&gt; file which serves as the jumping off point for your Angular application. This is similar to the &lt;code&gt;index.js&lt;/code&gt; file in a React application. Both use npm for packages and thus have a &lt;code&gt;node_modules&lt;/code&gt; folder and a &lt;code&gt;package.json&lt;/code&gt; file. Both frameworks make use of components, which are reusable chunks of JavaScript (or in the case of Angular, Typescript) and html that are the building blocks of your application. Additionally, you can create mobile apps with both frameworks.&lt;/p&gt;

&lt;p&gt;I did notice some major differences though. For one, Angular uses TypeScript which is a superset of JavaScript. If you would like to learn more about TypeScript, check out my &lt;a href="https://dev.to/racheladaw/intro-to-typescript-4d8k"&gt;blog&lt;/a&gt;. Also, each Angular component has its own HTML file, whereas React uses JSX in the component itself. &lt;a href="https://reactjs.org/docs/introducing-jsx.html"&gt;JSX&lt;/a&gt; is a, "syntax extension" to JavaScript and allows you to create React elements that look very similar to XML.&lt;/p&gt;

&lt;p&gt;Another big difference is the use of Angular directives to help create logic in the HTML files. One example is &lt;code&gt;ngIf&lt;/code&gt; which allows you to create an if statement inside of your HTML. The following is from the &lt;a href="https://angular.io/api/common/NgIf"&gt;docs&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div *ngIf="condition"&amp;gt;Content to render when condition is true.&amp;lt;/div&amp;gt;

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



&lt;p&gt;With React, I have typically written functions in my components to conditionally render elements with if/else blocks like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;renderPrice = () =&amp;gt; {
    if (this.props.concert.attributes.price_range !== "-") {
      return &amp;lt;p&amp;gt;${this.props.concert.attributes.price_range}&amp;lt;/p&amp;gt;
    } else {
      return &amp;lt;p&amp;gt;Price Unknown&amp;lt;/p&amp;gt;
    }
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;These functions are then called within React's &lt;code&gt;render()&lt;/code&gt; function using JSX.&lt;/p&gt;

&lt;h3&gt;
  
  
  A Little Bit of History
&lt;/h3&gt;

&lt;p&gt;Angular was developed and released in 2016 by Google. Angular was based off of AngularJS, which was created by Misko Hevery and Adam Abrons in 2009. However AngularJS was completely re-written and transformed into Angular to include things, such as TypeScript and modularity.&lt;/p&gt;

&lt;p&gt;React was developed by Jordan Walke, a Software Engineer at Facebook. It was originally called FaxJS, was inspired by XHP, and used internally. It was first deployed in 2011 and open-sourced in 2013.&lt;/p&gt;

&lt;h3&gt;
  
  
  Angular
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;p&gt;Angular comes with everything a developer needs to create applications. As mentioned earlier, it utilizes TypeScript which compiles to plain JavaScript and the documentation is all centered around TypeScript. The use of TypeScript is considered a &lt;strong&gt;pro&lt;/strong&gt; by most developers because it allows errors to be caught quickly. Angular comes with routing, templating, and more without needing to install additional packages.&lt;/p&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;p&gt;One &lt;strong&gt;con&lt;/strong&gt; to Angular is that although it comes with everything you need to build an application, it can be hard to add additional packages and get them to seamlessly work with Angular components and TypeScript. Also, some say that the learning curve is steep for Angular mainly due to the syntax.&lt;/p&gt;

&lt;p&gt;Some companies that use Angular are Google (Gmail), Microsoft, Xbox, and Udacity.&lt;/p&gt;

&lt;h3&gt;
  
  
  React
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Pros
&lt;/h5&gt;

&lt;p&gt;React's exciting features include the Virtual DOM and state. See my blog about the virtual DOM and how it works &lt;a href="https://dev.to/racheladaw/react-diffing-and-the-dom-2j2d"&gt;here&lt;/a&gt;. Basically, the virtual DOM allows React to batch updates to the DOM and only make changes to the necessary elements. This feature makes React super fast! State allows each component to contain dynamic data in its own object. This is a great way to package up variables that will change, instead of spreading them out throughout your code. Additionally, including extra packages with React is usually pretty straightforward and easy to incorporate.&lt;/p&gt;

&lt;h5&gt;
  
  
  Cons
&lt;/h5&gt;

&lt;p&gt;Here are some potential &lt;strong&gt;cons&lt;/strong&gt; of React: You can use TypeScript with React, however the documentation is written in JavaScript so it might not be as straightforward as Angular out of the box. React has routing as well (&lt;code&gt;react-router&lt;/code&gt;), but requires installing a different package that isn't included with &lt;code&gt;create-react-app&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Companies that currently use React are Facebook, Instagram, Airbnb, Twitter, Netlix and more.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping it Up
&lt;/h3&gt;

&lt;p&gt;Deciding whether to get started with Angular or React ultimately depends on your use case. Angular comes with everything you need to build a single-page web application out of the box, while React is sometimes seen as more customizable. Also, some say that Angular has a little bit of a steeper learning curve than React.&lt;/p&gt;

&lt;p&gt;Let me know which framework you prefer and why. Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3ohs7MNbaPYwc5mKOc/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3ohs7MNbaPYwc5mKOc/giphy.gif" alt="Bye"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  References
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/AngularJS"&gt;AngularJS Wiki&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/React_(web_framework)"&gt;React Wiki&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=pVbcAafEzDc"&gt;Youtube Video&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>angular</category>
      <category>react</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Intro to JavaScript Promises</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Mon, 27 Apr 2020 00:50:57 +0000</pubDate>
      <link>https://dev.to/racheladaw/intro-to-javascript-promises-16kp</link>
      <guid>https://dev.to/racheladaw/intro-to-javascript-promises-16kp</guid>
      <description>&lt;h3&gt;
  
  
  Async Actions
&lt;/h3&gt;

&lt;p&gt;If you have written JavaScript before, you are most likely aware of asynchronous programming. Since JavaScript can only perform one command at a time (it is a single-threaded language), operations that are long, such as retrieving data from a server, can block the main thread. This is bad because to the user, your program may appear unresponsive.&lt;/p&gt;

&lt;p&gt;An example of this would be making a fetch request to an API and waiting for the response. While waiting for the response from the API, we want our program to be able to execute other processes. Asynchronous programming allows JavaScript to continue execution of other processes while waiting for another process to resolve. Here is a good &lt;a href="https://codesthq.com/asynchronous-and-single-threaded-javascript-meet-the-event-loop/"&gt;blog&lt;/a&gt; that goes more in depth on asynchronous JavaScript and the event loop.&lt;/p&gt;

&lt;h3&gt;
  
  
  Promises
&lt;/h3&gt;

&lt;p&gt;A &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;promise&lt;/a&gt; is a JavaScript object that, "represents the eventual completion (or failure) of an asynchronous operation, and its resulting value." Promises were first introduced to JavaScript in ES6. With this new feature, functions can return a promise and continue executing other processes while waiting for it to resolve or settle. Once the promise is resolved, the program can continue with the use of that data it was waiting on.&lt;/p&gt;

&lt;p&gt;Before promises were introduced, callbacks and events were used for asynchronous programming, but this presented some challenges. Some people have referred to this as callback hell, as the code can be extremely hard to understand. Here is an example from this Stranger Things themed &lt;a href="https://dev.to/amberjones/how-to-escape-callback-hell-with-javascipt-promises-42d0"&gt;blog post&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fightTheDemogorgon(function(result) {
  rollForDamage(result, function(seasonsLeft) {
    closeTheGate(seasonsLeft, function(finalResult) {
      console.log('Hawkins is safe for ' + finalResult + ' more seasons.');
    }, failureCallback);
  }, failureCallback);
}, failureCallback);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://i.giphy.com/media/NTur7XlVDUdqM/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/NTur7XlVDUdqM/giphy.gif" alt="This is fine"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  A Metaphor
&lt;/h4&gt;

&lt;p&gt;I like to think of promises like a ticket you would get from a food truck after you order. The ticket has a number, so it can be associated with your order and you can perform other actions while you are waiting for your food. Meanwhile, the food truck is hard at work getting the order they "promised" ready for you.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/d31vot4Yoy9r8dwY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d31vot4Yoy9r8dwY/giphy.gif" alt="Food truck GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  States
&lt;/h5&gt;

&lt;p&gt;Promises have three possible states: fulfilled, rejected, and pending.&lt;/p&gt;

&lt;p&gt;The initial state of the promise is pending. This is when you first get the ticket from the food truck and are waiting for your order. Maybe during this time you are doing another action you need to complete, like calling your mom. We all know we need to do that more.&lt;/p&gt;

&lt;p&gt;A promise is fulfilled if the operation was completed successfully. This would be when the food truck has successfully given you your food. A promise is rejected if it failed. If rejected, there will be an error associated with the promise. This would be the case if the food truck ran out of an ingredient and couldn't make your order. Maybe the error would be a message from the employee telling you what went wrong.&lt;/p&gt;

&lt;h4&gt;
  
  
  Then / Catch Methods
&lt;/h4&gt;

&lt;p&gt;Two methods that are frequently used with promises are &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt;. These methods will execute when a promise is "settled," meaning it is no longer pending. Both &lt;code&gt;.then()&lt;/code&gt; and &lt;code&gt;.catch()&lt;/code&gt; return promises themselves and can be chained onto the original async function.&lt;/p&gt;

&lt;h5&gt;
  
  
  Then
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;.then()&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then"&gt;method&lt;/a&gt; takes up to two arguments. The arguments should be callback functions to execute when the previous async action settled. The first callback will execute in the case of the promise being &lt;strong&gt;fulfilled&lt;/strong&gt; and the second will execute in the case of the promise being &lt;strong&gt;rejected&lt;/strong&gt;. These callback functions are named &lt;code&gt;onFulfilled()&lt;/code&gt; and &lt;code&gt;onRejected()&lt;/code&gt; in the MDN documentation, but most programmers use arrow functions for their callbacks like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return fetch(`${baseUrl}/api/v1/concert/${concertId}/users`, configurationObject)
      .then(r =&amp;gt; r.json())
      .then(users =&amp;gt; {
        if (users.error) {
          alert(users.error)
        } else {
          dispatch(setUsersOfConcert(users.data))
        }
      })
      .catch(error =&amp;gt; console.log(error))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;onFulfilled()&lt;/code&gt; function takes one argument, the fulfillment value. In this case, I called the value &lt;code&gt;r&lt;/code&gt; for response. The &lt;code&gt;onRejected()&lt;/code&gt; callback takes one argument, the reason for the rejection. In this case, I didn't use the second argument for &lt;code&gt;.then()&lt;/code&gt;, but this was handled with &lt;code&gt;.catch()&lt;/code&gt;, which you will read about below. The promise returned by &lt;code&gt;.then()&lt;/code&gt; will resolve to the return value of the passed in callback, or to its originally settled value if the passed in argument was not a callback function. For example, if &lt;code&gt;.then(console.log("hi"))&lt;/code&gt; was chained onto an async function, the promise will resolve to undefined.&lt;/p&gt;

&lt;p&gt;As you can see, I chained the &lt;code&gt;.then()&lt;/code&gt; method on with one argument, to execute in the case of the promise being fulfilled. In this case, I take the response body from the API and parse it to json. The &lt;code&gt;.json()&lt;/code&gt; method returns a promise as well, hence the additional &lt;code&gt;.then()&lt;/code&gt; chaining.&lt;/p&gt;

&lt;h5&gt;
  
  
  Catch
&lt;/h5&gt;

&lt;p&gt;The &lt;code&gt;.catch()&lt;/code&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch"&gt;method&lt;/a&gt; is basically the same as &lt;code&gt;.then()&lt;/code&gt;, however it deals with the case of the promise being rejected.  From the docs, this method actually calls &lt;code&gt;.then()&lt;/code&gt; under the hood, with the first argument as &lt;code&gt;undefined&lt;/code&gt;. As mentioned earlier, &lt;code&gt;.catch()&lt;/code&gt; returns a promise as well. From the docs, this promise is resolved unless the &lt;code&gt;onRejected()&lt;/code&gt; callback function returns an error, or returns a promise that is rejected. The promise returned by &lt;code&gt;.catch()&lt;/code&gt; will resolve to the return value of the passed in callback.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;MDN&lt;/a&gt; has a pretty good diagram to explain the flow of chaining:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BDtxbm8A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/15911/promises.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BDtxbm8A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://mdn.mozillademos.org/files/15911/promises.png" alt="MDN Promises"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In most cases that I have seen, developers use &lt;code&gt;.catch()&lt;/code&gt; to log errors. Above, in my code snippet, you can see that in the case of an error, I just log it to my console.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;There is much more I could write about when it comes to promises. There are more methods associated with them and more use cases. You can even construct your own promise objects using the &lt;code&gt;new&lt;/code&gt; keyword. Check out the youtube video I linked in the resources section to see this in action.&lt;/p&gt;

&lt;p&gt;I wanted to keep this blog fairly short, but I might go more in depth on the subject in the future. Let me know how you have used promises in your code. Hope you all are safe and happy!&lt;/p&gt;

&lt;h5&gt;
  
  
  Resources
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise"&gt;MDN&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-promise-27fc71e77261"&gt;Medium Blog by Eric Elliott&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=DHvZLI7Db8E"&gt;A Good Youtube Video on Promises&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Intro to Binary Search</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 19 Apr 2020 01:21:49 +0000</pubDate>
      <link>https://dev.to/racheladaw/intro-to-binary-search-385o</link>
      <guid>https://dev.to/racheladaw/intro-to-binary-search-385o</guid>
      <description>&lt;h3&gt;
  
  
  Quick Overview
&lt;/h3&gt;

&lt;p&gt;Binary search is an important searching algorithm to learn for technical interviews and for use in searching problems you might encounter in your projects. For large arrays this algorithm is very quick. The only catch is that it can only be done with sorted arrays.&lt;/p&gt;

&lt;h5&gt;
  
  
  The Phone Book Analogy
&lt;/h5&gt;

&lt;p&gt;A lot of people like to think of searching through a phone book when they think about binary search. This analogy is a little bit antiquated considering most people just search the contacts in their phones these days, however I think it's a good way to understand the concept.&lt;/p&gt;

&lt;p&gt;If you were to look up a last name in the phone book, let's say the name Smith, how would you go about doing this? Most people would first flip to where they thought the name might be, which might be a little past halfway. Then they would check the names on the page they flipped to. Let's say you flipped to a page with last names starting with P. You would know that since P comes before S, you need to now check the back-half of the phone book. Therefore, you can eliminate all of the names in the phone book from the beginning until just past the page you are on, since you know Smith isn't on that page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0HlAgJTVaAPHEGdy/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0HlAgJTVaAPHEGdy/giphy.gif" alt="Flipping Through Book"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You would repeat this process, searching a spot roughly halfway through the remainder of the phone book and comparing the names to your target name, Smith, until you found the page with the name you are searching for.&lt;/p&gt;

&lt;p&gt;This is very similar to how binary search works and explains why it's so much faster than searching each element one by one. Since the data is sorted, we can take a better guess as to where our target value is.&lt;/p&gt;

&lt;h3&gt;
  
  
  Working on the Pseudocode
&lt;/h3&gt;

&lt;p&gt;With this knowledge of the algorithm, we can begin to work on some pseudocode for how our algorithm should work. Let's say we are looking for the target value &lt;code&gt;5&lt;/code&gt; in the array: &lt;code&gt;[0, 1, 2, 3, 5, 7, 8]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We know that our function should take two parameters, a sorted array and a target value to find in the array. We know we will be looking at the element in the middle of the array each time and comparing that to our target. If we don't find a match, we know we will need to look at a new part of the array, either the portion after the middle or before the middle.&lt;/p&gt;

&lt;p&gt;One good way to find the middle of the array is by using the average. To find the average, we know we will need pointers to the left and right sides of the portion of the array that we are currently "investigating." We will need to add the pointers together and divide them by two. Since this is the case, we will store the index at the farthest left side of the portion of the array we are looking at, as well as the index of the farthest right position.&lt;/p&gt;

&lt;p&gt;Next we will create a loop so that we can continue looking at different portions of the array until we find the match. With each loop, we will calculate the index at the middle of the portion of the array we are looking at and compare the value at that index to our target value. If the middle value matches our target, we will return the index of the middle value. If the middle value is less than our target, we will set our left pointer to one above our current middle to look at the last half of the current scope of the array. If the middle value is greater than our target, we will set the right pointer to one below the middle index to look at the first half of the current scope of the array. We will then execute the loop again.&lt;/p&gt;

&lt;p&gt;If we can't find a match after searching the entire array, then we will want to return -1, indicating no index found for the target value.&lt;/p&gt;

&lt;p&gt;Here is some pseudocode for what we have so far:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function binarySearch(sortedArray, targetValue) {
  //set leftSide to beginning of array at first
  let leftSide = 0
  //set rightSide to end of array at first so the entire array is in scope
  let rightSide = endOfArray

  while (targetNotFound) {
    // average the left and right pointer to find middle. Will need to round this number to get an integer
    let middle = average(left, right)

    if (targetValue === valueAtMiddleIndex) {
      return middle
    } else if (valueAtMiddleIndex &amp;lt; targetValue) {
      leftSide = middle + 1
    } else if (valueAtMiddleIndex &amp;gt; targetValue) {
      rightSide = middle - 1
    }
  }
  // if target value can't be found in array
  return -1
}

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



&lt;p&gt;Let's walk through the code with our test case.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We start with &lt;code&gt;[0, 1, 2, 3, 5, 7, 8]&lt;/code&gt; and are searching for &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;leftSide&lt;/code&gt; will be initialized at &lt;code&gt;0&lt;/code&gt;. &lt;code&gt;rightSide&lt;/code&gt; will be initalized at &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;1st loop:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; initialized at &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;3&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;3&lt;/code&gt; === &lt;code&gt;5&lt;/code&gt;? No, it is smaller than the target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;leftSide&lt;/code&gt; now = 3 + 1 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;2nd loop:

&lt;ul&gt;
&lt;li&gt;We are now looking at this portion of the array: &lt;code&gt;[5, 7, 8]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; now = (4 + 6) / 2 = &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;5&lt;/code&gt; is &lt;code&gt;7&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;7&lt;/code&gt; === &lt;code&gt;5&lt;/code&gt;? No, it is bigger than the target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rightSide&lt;/code&gt; now = 5 -1 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;3rd loop:

&lt;ul&gt;
&lt;li&gt;Now we are only looking at this portion: &lt;code&gt;[5]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; now = (4 + 4) / 2 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;4&lt;/code&gt; is &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;5&lt;/code&gt; === &lt;code&gt;5&lt;/code&gt;. Yes!&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;middle&lt;/code&gt; which = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works!&lt;/p&gt;

&lt;h5&gt;
  
  
  A Problem
&lt;/h5&gt;

&lt;p&gt;Do you see a problem with the pseudocode?&lt;/p&gt;

&lt;p&gt;If you thought that the loop could execute forever in certain cases, you would be right. With our current code, we only stop the loop if we find the target value, however if we never find it the loop will continue forever.&lt;/p&gt;

&lt;p&gt;A good way to short circuit this loop would be to make sure the left pointer never goes past the right. That is, if the array is down to one more value to check and that value is not equal to our target, we exit the loop. Here is our updated pseudocode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function binarySearch(sortedArray, targetValue) {
  //set leftSide to beginning of array at first
  let leftSide = 0
  //set rightSide to end of array at first so the entire array is in scope
  let rightSide = endOfArray

  // exit loop if left pointer goes past rightPointer. I removed the targetNotFound condition since the return statement within the loop already handles this.
  while (leftSide &amp;lt;= rightSide) {
    // average the left and right pointer to find middle. Will need to round this number to get an integer
    let middle = average(left, right)

    if (targetValue === valueAtMiddleIndex) {
      return middle
    } else if (valueAtMiddleIndex &amp;lt; targetValue) {
      leftSide = middle + 1
    } else if (valueAtMiddleIndex &amp;gt; targetValue) {
      rightSide = middle - 1
    }
  }
  // if target value can't be found in array
  return -1
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's walk through the pseudocode using the same array as before with a new target value of &lt;code&gt;4&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We start with &lt;code&gt;[0, 1, 2, 3, 5, 7, 8]&lt;/code&gt; and are searching for &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;leftSide&lt;/code&gt; will be initialized at &lt;code&gt;0&lt;/code&gt;. &lt;code&gt;rightSide&lt;/code&gt; will be initalized at &lt;code&gt;6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;1st loop because leftSide(&lt;code&gt;0&lt;/code&gt;) &lt;code&gt;&amp;lt;=&lt;/code&gt; rightSide(&lt;code&gt;6&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; initialized at &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;3&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;3&lt;/code&gt; === &lt;code&gt;4&lt;/code&gt;? No, it is smaller than the target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;leftSide&lt;/code&gt; now = 3 + 1 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;2nd loop because leftSide(&lt;code&gt;4&lt;/code&gt;) &lt;code&gt;&amp;lt;=&lt;/code&gt; rightSide(&lt;code&gt;6&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;We are now looking at this portion of the array: &lt;code&gt;[5, 7, 8]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; now = (4 + 6) / 2 = &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;5&lt;/code&gt; is &lt;code&gt;7&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;7&lt;/code&gt; === &lt;code&gt;4&lt;/code&gt;? No, it is bigger than the target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rightSide&lt;/code&gt; now = 5 - 1 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;3rd loop because leftSide(&lt;code&gt;4&lt;/code&gt;) &lt;code&gt;&amp;lt;=&lt;/code&gt; rightSide(&lt;code&gt;4&lt;/code&gt;):

&lt;ul&gt;
&lt;li&gt;Now we are only looking at this portion: &lt;code&gt;[5]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;middle&lt;/code&gt; now = (4 + 4) / 2 = &lt;code&gt;4&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The element at index &lt;code&gt;4&lt;/code&gt; is &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Does &lt;code&gt;5&lt;/code&gt; === &lt;code&gt;4&lt;/code&gt;. No, it is bigger than the target.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rightSide&lt;/code&gt; now = 4 - 1 = &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Exit while loop because leftSide(&lt;code&gt;4&lt;/code&gt;) is NOT &lt;code&gt;&amp;lt;=&lt;/code&gt; rightSide(&lt;code&gt;3&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;-1&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It works!&lt;/p&gt;

&lt;p&gt;This pseudocode is already pretty close to the real thing, but I challenge you to get a working JavaScript function yourself before continuing on. Here's a gif so you don't sneak a peek at my code below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ZDKZ8hiISoEy4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ZDKZ8hiISoEy4/giphy.gif" alt="Praying Mantis GIF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  My Implementation of Binary Search
&lt;/h3&gt;

&lt;p&gt;Here is my implementation of this algorithm using JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function binarySearch(sortedArr, value){
  let left = 0;
  let right = sortedArr.length - 1;

  // I chose to initialize these variables outside the loop
  let middle;
  // currentElem will be the element that is at the middle index
  let currentElem;

  while (right &amp;gt;= left) {
      // Math.floor() will round the decimal down to the nearest integer
      middle = Math.floor((left + right) / 2)

      currentElem = sortedArr[middle];

      if (currentElem === value) {
          return middle;
      } else if (currentElem &amp;lt; value) {
          left = middle + 1;
      } else if (currentElem &amp;gt; value) {
          right = middle - 1;
      }
  }
  return -1;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Big O of Binary Search
&lt;/h3&gt;

&lt;p&gt;The worst case performance of Big O is O(log n) which is very fast. For perspective, most of JavaScript's built in searching methods, such as &lt;code&gt;Array.prototype.includes()&lt;/code&gt;, have a &lt;a href="https://miro.medium.com/max/2928/1*5ZLci3SuR0zM_QlZOADv8Q.jpeg"&gt;time complexity&lt;/a&gt; of O(n) because they use linear search.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rFt9Cn5---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2928/1%2A5ZLci3SuR0zM_QlZOADv8Q.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rFt9Cn5---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/2928/1%2A5ZLci3SuR0zM_QlZOADv8Q.jpeg" alt="Big O Chart"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Binary search is significantly faster than linear search for arrays that aren't considered small. If the array is small, it might not perform faster than linear search. The only downside with binary search that I see is that the data must be sorted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Cheers
&lt;/h3&gt;

&lt;p&gt;Thank you for reading. I hope I could teach you something new today and I hope everyone is having a fun and safe weekend!&lt;/p&gt;

&lt;h5&gt;
  
  
  Resources
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/"&gt;JavaScript Algorithms and Data Structures Masterclass by Colt Steele&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://miro.medium.com/max/2928/1*5ZLci3SuR0zM_QlZOADv8Q.jpeg"&gt;Time Complexity Chart&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Uploading Profile Pictures in a React and Rails API App Part II</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 12 Apr 2020 22:36:20 +0000</pubDate>
      <link>https://dev.to/racheladaw/uploading-profile-pictures-in-a-react-and-rails-api-app-part-ii-bm7</link>
      <guid>https://dev.to/racheladaw/uploading-profile-pictures-in-a-react-and-rails-api-app-part-ii-bm7</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;This is the second part in my series on uploading images in a React application. If you haven't read the first part, &lt;a href="https://dev.to/racheladaw/uploading-profile-pictures-in-a-react-and-rails-api-application-part-i-3nan"&gt;here it is&lt;/a&gt;. This blog will pick up where the last one left off.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting Up Rails to Receive the Image
&lt;/h3&gt;

&lt;p&gt;Obviously, the first thing I did was set up my route to send my POST request to. I decided have this route go to my users controller, since a picture is associated with a user. I went with &lt;code&gt;'/users/:id/upload_photo'&lt;/code&gt; where id is the user's id.&lt;/p&gt;

&lt;p&gt;Next up was creating the controller action. This blog largely follows the work of this &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. It's a really great resource if you would like to set this functionality up yourself. Here is the code for my uploadPhoto action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def uploadPhoto
  //grabbing user from the db using the id from the query string parameters
  //i used strong params
  @user = User.find(profile_picture_params[:id])

  @user.profile_picture.attach(profile_picture_params[:profile_picture])

  if @user.profile_picture.attached?
    profile_picture_serializer = ProfilePictureSerializer.new(profile_picture: @user.profile_picture, user: @user)
    render json: profile_picture_serializer.serialize_new_profile_picture()
  else
    render json: {errors: "No profile picture attached"}, status: 400
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So above, I am grabbing the current user based on the id from the query string parameter. I used a strong params function, hence the profile_picture_params method.&lt;/p&gt;

&lt;h5&gt;
  
  
  ActiveStorage
&lt;/h5&gt;

&lt;p&gt;In order to attach the image file to my user, I used ActiveStorage. From the &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html" rel="noopener noreferrer"&gt;Rails guide&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;"Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations."&lt;/p&gt;

&lt;p&gt;ActiveStorage is already integrated into Rails, however to use it you need to create some migrations to make two tables in your database, &lt;code&gt;active_storage_blobs&lt;/code&gt; and &lt;code&gt;active_storage_attachments&lt;/code&gt;. You might recognize that word &lt;code&gt;blob&lt;/code&gt; from part one of this blog series. To set up ActiveStorage I recommend following the blog I linked earlier or the ActiveStorage Rails guide linked above. You basically just run an install command and then run migrate to make the new tables.&lt;/p&gt;

&lt;h4&gt;
  
  
  Back to the Code
&lt;/h4&gt;

&lt;p&gt;The profile_picture attribute on my user comes from this ActiveStorage macro, &lt;code&gt;has_one_attached :profile_picture&lt;/code&gt;. From the &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html#has-one-attached" rel="noopener noreferrer"&gt;Rails guides&lt;/a&gt;, this macro, "sets up a one-to-one mapping between records and files. Each record can have one file attached to it." Essentially, each of my users will have one image file attached to their record. Next up, I used the &lt;code&gt;attach&lt;/code&gt; method from &lt;a href="https://edgeguides.rubyonrails.org/active_storage_overview.html" rel="noopener noreferrer"&gt;ActiveStorage&lt;/a&gt; to attach the image to the user's record.&lt;/p&gt;

&lt;p&gt;ActiveStorage has a method &lt;code&gt;attached?&lt;/code&gt; that checks if the user has a &lt;code&gt;profile_picture&lt;/code&gt; attached to the record. If this is the case, the profile picture data and will be serialized and rendered as JSON. Here is the code for my Profile Picture Serializer:&lt;br&gt;
&lt;/p&gt;

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

  def initialize(profile_picture: nil, user:)
    @profile_picture = profile_picture
    @user = user
  end

  def serialize_new_profile_picture()
    serialized_new_profile_picture = serialize_profile_picture(@profile_picture, @user)
    serialized_new_profile_picture.to_json()
  end

  private

  def serialize_profile_picture(profile_picture, user)
    {
      profile_picture: {
        user_id: user.id,
        image_url: user.get_profile_picture_url(),
        created_at: profile_picture.created_at
      }
    }
  end

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

&lt;/div&gt;



&lt;p&gt;Once again, this largely follows the setup from this &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. I wanted the JSON for the profile picture to contain the user id, image url, and date the record was created. The &lt;code&gt;get_profile_picture_url()&lt;/code&gt; method comes from my user model. This method uses Rails' &lt;code&gt;url_for()&lt;/code&gt; method to get the url for the profile_picture attached to the record. &lt;code&gt;url_for()&lt;/code&gt; is a helper method and you will need to add &lt;code&gt;include Rails.application.routes.url_helpers&lt;/code&gt; to the beginning of your model code in order to use it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuring Cloudinary
&lt;/h3&gt;

&lt;p&gt;To set up Cloudinary the first thing you will need to do is &lt;a href="https://cloudinary.com/" rel="noopener noreferrer"&gt;create a free account&lt;/a&gt;. This part is pretty straightforward so I won't go over it, but once that is complete you will need your key and secret which you can find on your dashboard when you are logged into your account.&lt;/p&gt;

&lt;p&gt;Next up add the &lt;code&gt;cloudinary&lt;/code&gt; &lt;a href="https://github.com/cloudinary/cloudinary_gem" rel="noopener noreferrer"&gt;gem&lt;/a&gt; to your Gemfile and run &lt;code&gt;bundle install&lt;/code&gt;. Then add this to your &lt;code&gt;storage.yml&lt;/code&gt; file in your &lt;code&gt;config&lt;/code&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cloudinary:
  service: Cloudinary
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next step is to set &lt;code&gt;config.active_storage.service = :cloudinary&lt;/code&gt; in your &lt;code&gt;config/environments/development.rb&lt;/code&gt; and &lt;code&gt;production.rb&lt;/code&gt; files. This tells active_storage to use Cloudinary to store the files. Then download the YML file from your Cloudinary account. You will find the link for this in the top right of your account details on your dashboard. Put this file in your &lt;code&gt;config&lt;/code&gt; folder and be sure to add this file to your &lt;code&gt;.gitignore&lt;/code&gt; because you don't want to push these credentials to Github.&lt;/p&gt;

&lt;p&gt;As the &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13" rel="noopener noreferrer"&gt;blog&lt;/a&gt; mentions, "With this in place, ActiveStorage will automatically upload and retrieve images from the cloud."&lt;/p&gt;

&lt;h5&gt;
  
  
  A Minor Bug
&lt;/h5&gt;

&lt;p&gt;As the blog I followed mentioned, I did receive this error the first time I tried to successfully post an image to my Rails API: &lt;code&gt;ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true)&lt;/code&gt;. I followed what the blog said and added &lt;code&gt;Rails.application.routes.default_url_options = { host: "http://localhost:3001" }&lt;/code&gt; to my &lt;code&gt;config/environments/development.rb&lt;/code&gt; folder. I have 3001 as my port instead of 3000 which is the default Rails port.&lt;/p&gt;

&lt;h3&gt;
  
  
  Success
&lt;/h3&gt;

&lt;p&gt;After adding the default url options, the JSON was fetched successfully! When I checked my Cloudinary account, I could see in my assets that my image was uploaded successfully. Also yes, this is a picture of my dog:&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%2F6ij1c4mf7zuwigv8zs3o.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%2F6ij1c4mf7zuwigv8zs3o.png" alt="Profile Picture"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I won't go over the details of how I rendered the image in my React app in this blog, but here is the final product. It needs some styling, but I was excited to have it show up!&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%2Fpauq67kdgd7p3t34r9ia.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%2Fpauq67kdgd7p3t34r9ia.png" alt="React Profile Page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another cool thing to note, is that if I change the profile picture, the old one will be automatically deleted from Cloudinary and replaced with the new one. I'm assuming this comes from the fact that I used the &lt;code&gt;has_one_attached&lt;/code&gt; macro, so only one file can be associated with the record at a time.&lt;/p&gt;

&lt;p&gt;Thank you for reading and let me know if you have any feedback or questions. &lt;a href="https://github.com/racheladaw/concert-buddy-app-backend" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is the repo for my Rails API if you would like to look at any of the code further.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
      <category>ruby</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Uploading Profile Pictures in a React and Rails API Application Part I</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 05 Apr 2020 22:12:26 +0000</pubDate>
      <link>https://dev.to/racheladaw/uploading-profile-pictures-in-a-react-and-rails-api-application-part-i-3nan</link>
      <guid>https://dev.to/racheladaw/uploading-profile-pictures-in-a-react-and-rails-api-application-part-i-3nan</guid>
      <description>&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;This week I wanted to add a feature to my &lt;a href="https://github.com/racheladaw/concert-buddy-frontend"&gt;Concert Buddy&lt;/a&gt; application where users could add an image to their profile. It turns out that this is not as easy as I originally thought. I had to decide whether to store the images in my database or on a cloud service.&lt;/p&gt;

&lt;h3&gt;
  
  
  Storing in the Database
&lt;/h3&gt;

&lt;p&gt;After some research, I found that there is a way to store images in a database and that is by using BLOBS. &lt;a href="https://en.wikipedia.org/wiki/Binary_large_object"&gt;BLOBS&lt;/a&gt; are Binary Large Objects meaning the image is stored as, "a collection of binary data stored as a single entity." However, it seems like the general consensus among developers is to not store image data in your database because it takes up a large amount of storage. From &lt;a href="https://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay"&gt;this&lt;/a&gt; Stack Overflow post:&lt;/p&gt;

&lt;p&gt;"database storage is usually more expensive than file system storage" and "things like web servers, etc, need no special coding or processing to access images in the file system"&lt;/p&gt;

&lt;p&gt;Also, I used PostgreSQL for the database in my application which uses &lt;code&gt;bytea&lt;/code&gt; instead of the &lt;code&gt;BLOB&lt;/code&gt; type. Uploading images to a cloud service seemed like a better plan for my profile picture images. This way, my database wouldn't get too large and I could also learn how to work with a cloud service.&lt;/p&gt;

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

&lt;p&gt;I ended up finding this amazing &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13"&gt;blog&lt;/a&gt; that walks through how to send images from a JavaScript front end to a Rails API backend. It was exactly what I needed! I will walk through the basic implementation here and talk about the hiccups I had, but the blog already has great information on how to do it yourself.&lt;/p&gt;

&lt;h5&gt;
  
  
  The First Step
&lt;/h5&gt;

&lt;p&gt;The first step to starting this feature was to add an input for a user to select a file from their computer. I added a form to my Profile component with one input for the image, as well as a submit button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;form className={props.formStatus === "Active" ? "" : "hidden"} onSubmit={submitPhoto}&amp;gt;
  &amp;lt;input type="file"
    id="profile-photo-input" name="profile_picture"
    accept="image/png, image/jpeg"
  /&amp;gt;
  &amp;lt;input
    className="submit-input"
    type="submit"
    value="Upload"
  /&amp;gt;
&amp;lt;/form&amp;gt;


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



&lt;p&gt;By using the file type input, a user can select a file from their computer to upload. File type inputs allow an accept attribute to specify which types of files are allowed. You can also choose to use the multiple attribute which allows users to upload multiple files at once. In my case, I only want users to upload one image so I left the multiple attribute out and specified &lt;code&gt;image/png&lt;/code&gt; and &lt;code&gt;image/jpeg&lt;/code&gt; with the accept attribute to allow those file types.&lt;/p&gt;

&lt;h5&gt;
  
  
  Sending the Photo
&lt;/h5&gt;

&lt;p&gt;The next step in the process was sending the photo via a fetch request to my Rails API.&lt;/p&gt;

&lt;p&gt;From the code snippet above, you can see that my form has an onSubmit event attribute. This will call the submitPhoto function in my component when the form is submitted. Here is my code for that function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const submitPhoto = (event) =&amp;gt; {
    event.preventDefault();
    let photoInput = document.getElementById('profile-photo-input');
    if (photoInput.files[0]) {
      const formData = new FormData();
      const upload_file = photoInput.files[0]
      formData.append('profile_picture', upload_file);
      props.submitProfilePhoto(formData, props.userId)
    }
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The main thing to focus on here is setting up the FormData object. I had never used this before, however it was used in the main &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13"&gt;blog&lt;/a&gt; that I followed. This object allows you to set key/value pairs for all of the data you are submitting in your form and your subsequent fetch request. In this case, I chose to name my key 'profile_picture' and the value is the uploaded image. I grabbed the image by grabbing the file input from the DOM and then used the file input's &lt;code&gt;files&lt;/code&gt; method to get the &lt;code&gt;FileList&lt;/code&gt;. If the image was successfully selected, there should be a file in the first index of &lt;code&gt;FileList&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next up was to make the action creator function to perform the fetch request. I called this function &lt;code&gt;submitProfilePhoto&lt;/code&gt; when I declared it in mapDispatchToProps. The actual function is called &lt;code&gt;uploadProfilePicture&lt;/code&gt; and that is what was imported into the file with my component. Here is the code for that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mapDispatchToProps = dispatch =&amp;gt; {
  return {
    toggleForm: () =&amp;gt; dispatch(setFormStateToActive()),
    submitProfilePhoto: (formData, userId) =&amp;gt; dispatch(uploadProfilePicture(formData, userId))
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above you can see I passed the formData and userId to the &lt;code&gt;dispatch&lt;/code&gt; method wrapping the &lt;code&gt;uploadProfilePicture&lt;/code&gt; function. In case you aren't familiar with dispatch, it is a store method in the Redux library that allows you to send actions to your store and trigger state changes. Using it along with Thunk middleware is very useful for &lt;a href="https://redux.js.org/advanced/async-actions"&gt;asynchronous requests&lt;/a&gt; because the action creators can then return functions that can have side effects, such as asynchronous requests. Then after your request is complete, you can trigger a state change. As a side note, this code could definitely use some refactoring in the future.&lt;/p&gt;

&lt;p&gt;Here is my Thunk action creator function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const uploadProfilePicture = (formData, userId) =&amp;gt; {
  return dispatch =&amp;gt; {
    const configurationObject = {
      credentials: "include",
      method: "POST",
      body: formData
    }

    return fetch(`${baseUrl}/api/v1/users/${userId}/upload_photo`, configurationObject)
      .then(r =&amp;gt; r.json())
      .then(photo =&amp;gt; {
        if (photo.error) {
          alert(photo.error)
        } else {
          // this is where I will dispatch an action creator function to update my store
          console.log("success", photo)
        }
      })
      .catch(error =&amp;gt; console.log(error))
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Above you can see I set up the configurationObject, setting the body to the formData object I created earlier. This is the code I had success with after some trial and error.&lt;/p&gt;

&lt;h5&gt;
  
  
  A Bug
&lt;/h5&gt;

&lt;p&gt;At first I had specified a &lt;code&gt;Content-Type&lt;/code&gt; in my configurationObject and was getting a bad request response (status 400) when sending my FormData to Rails.&lt;/p&gt;

&lt;p&gt;After some searching I found &lt;a href="https://github.com/github/fetch/issues/505"&gt;this&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;"Setting the Content-Type header manually means it's missing the boundary parameter. Remove that header and allow fetch to generate the full content type. It will look something like this:&lt;/p&gt;

&lt;p&gt;Content-Type: multipart/form-data;boundary=----WebKitFormBoundaryyrV7KO0BoCBuDbTL&lt;/p&gt;

&lt;p&gt;Fetch knows which content type header to create based on the FormData object passed in as the request body content."&lt;/p&gt;

&lt;p&gt;Also, if I would have looked more closely in the blog I was following, I would have noticed that it talks about this in &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13"&gt;there&lt;/a&gt; as well.&lt;/p&gt;

&lt;p&gt;"There is no "Content-Type" key in the headers — the content type is multipart/form-data, which is implied by the FormData object itself."&lt;/p&gt;

&lt;p&gt;I removed the content-type from my request and it worked! Also, I should mention that I had already set up the route and controller action in my Rails API along with a byebug to test that the data hit the endpoint. I will go over this further in my next blog.&lt;/p&gt;

&lt;h3&gt;
  
  
  For the Next Blog
&lt;/h3&gt;

&lt;p&gt;As this blog is already getting pretty long, I decided that I'm going to split it into multiple parts.&lt;/p&gt;

&lt;p&gt;Next week I will talk about the Rails side of things and how I am hosting my images on a cloud service, &lt;a href="https://github.com/cloudinary/cloudinary_gem"&gt;Cloudinary&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thank you for reading!&lt;/p&gt;

&lt;h3&gt;
  
  
  Additional Resources
&lt;/h3&gt;

&lt;h6&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400"&gt;400 Status Code&lt;/a&gt;
&lt;/h6&gt;

&lt;h6&gt;
  
  
  &lt;a href="https://medium.com/better-programming/how-to-upload-images-to-a-rails-api-and-get-them-back-again-b7b3e1106a13"&gt;Main Blog I Followed&lt;/a&gt;
&lt;/h6&gt;

&lt;h6&gt;
  
  
  &lt;a href="https://medium.com/@mendywoly/uploading-a-file-with-react-frontend-and-rails-api-backend-eb61939eedbc"&gt;Another Blog I Referenced&lt;/a&gt;
&lt;/h6&gt;

&lt;h6&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file"&gt;File Input&lt;/a&gt;
&lt;/h6&gt;

</description>
      <category>javascript</category>
      <category>rails</category>
      <category>ruby</category>
      <category>react</category>
    </item>
    <item>
      <title>Intro to TypeScript</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Sun, 29 Mar 2020 19:44:49 +0000</pubDate>
      <link>https://dev.to/racheladaw/intro-to-typescript-4d8k</link>
      <guid>https://dev.to/racheladaw/intro-to-typescript-4d8k</guid>
      <description>&lt;h1&gt;
  
  
  Introduction to TypeScript
&lt;/h1&gt;

&lt;h3&gt;
  
  
  The Basics
&lt;/h3&gt;

&lt;p&gt;TypeScript is a superset of JavaScript. This open-source language builds upon JavaScript and adds new features which are advantageous to developers. It was developed by Microsoft and first released to the public in 2012. TypeScript can't be executed by browsers or other JavaScript environments. Instead, TypeScript code must be compiled to regular JavaScript first, which some might say is a disadvantage. However, this compilation phase adds some advantages such as error checking.&lt;/p&gt;

&lt;h3&gt;
  
  
  Compiling
&lt;/h3&gt;

&lt;p&gt;TypeScript technically is &lt;a href="https://stackoverflow.com/questions/39246498/compiler-vs-interpreter-vs-transpiler"&gt;transpiled&lt;/a&gt; to JavaScript. Transpiling code means to change it from one language to another at the same level of abstraction. Where as compiling code is changing it from a higher-level language to a lower-level language. For example, the language Go is compiled to machine code. Transpiling TypeScript to JavaScript enables developers to use new features that compile to JS workarounds. For example, take a look at this TypeScript code and its corresponding transpiled JS code:&lt;/p&gt;

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

&lt;p&gt;The code in this screenshot was taken from the TypeScript website's &lt;a href="https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html"&gt;quick tutorial&lt;/a&gt;. You can see that the compiled JS code is much more lengthy and not as clear. An additional feature of TypeScript is that if any errors are present in the code, the errors will be displayed for the developer during compilation. For example, if a function is called with no parameters and is expecting a string as a parameter, an error will be thrown saying, &lt;code&gt;error TS2554: Expected 1 arguments, but got 0.&lt;/code&gt;. TypeScript will also show the exact line of code and spot within the line where the error occurred:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;greeter.ts:7:27 - error TS2554: Expected 1 arguments, but got 0.

7 document.body.innerHTML = greeter();
                            ~~~~~~~~~

  greeter.ts:1:18
    1 function greeter(person: string) {
                       ~~~~~~~~~~~~~~
    An argument for 'person' was not provided.


Found 1 error.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Typing
&lt;/h3&gt;

&lt;p&gt;TypeScript allows developers to use static typing. It isn't required, but it allows developers to catch errors on compilation. For example, if you try to pass an array in as an argument to a function that is expecting a string, this error will be thrown: &lt;code&gt;error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'.&lt;/code&gt; Here is an example of how typing is used in TypeScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let color: string = "blue";
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here are the types you can use:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;boolean&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;number&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;number[]&lt;/code&gt; (This is for arrays. Replace &lt;code&gt;number&lt;/code&gt; with data type in the array. Arrays can also be written like this &lt;code&gt;Array&amp;lt;number&amp;gt; = [1, 2, 3]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Tuple: Allows you to declare an array with a fixed number of elements with specific types. Example: &lt;code&gt;let a: [string, number];&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;enum&lt;/code&gt; Allows you to give a name to a set of constants. Example: &lt;code&gt;enum Direction {
Up: "UP",
Down: "DOWN"
}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;any&lt;/code&gt; (When a data type is unknown, label them with any and values will pass through checks during compilation)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;void&lt;/code&gt; (This describes the absence of type. You can only assign null  or undefined to this type)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;never&lt;/code&gt; (Used for values that never occur, for example functions that always throw an error)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;object&lt;/code&gt; (Represents anything that is not the primitive data type)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Types are optional with TypeScript, and they can be inferred during compilation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installing TypeScript
&lt;/h3&gt;

&lt;p&gt;If you would like to install TypeScript and try it out yourself, here is the command using npm: &lt;code&gt;npm install -g typescript&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;When you write your TypeScript code in a .ts file, run &lt;code&gt;tsc {your-file-name_here}.ts&lt;/code&gt; in your command line to compile it to JavaScript. When it's done compiling, you will see a new .js file in your directory with the compiled JavaScript code.&lt;/p&gt;

&lt;p&gt;Let me know if you have used TypeScript before and what you think about it. Thanks for reading and I hope you're all staying safe out there!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/d7HWDCV6t72iRm9vPh/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/d7HWDCV6t72iRm9vPh/giphy.gif" alt="Wash Your Hands"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h5&gt;
  
  
  References
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.typescriptlang.org/docs/home.html"&gt;TypeScript Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.youtube.com/watch?v=BwuLxPH8IDs"&gt;Academind YouTube Video&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/TypeScript"&gt;Wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/39246498/compiler-vs-interpreter-vs-transpiler"&gt;Transpile vs Compile&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Overview of Breadth-First Search and it's Uses</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Mon, 23 Mar 2020 00:49:28 +0000</pubDate>
      <link>https://dev.to/racheladaw/overview-of-breadth-first-search-and-it-s-uses-4fmg</link>
      <guid>https://dev.to/racheladaw/overview-of-breadth-first-search-and-it-s-uses-4fmg</guid>
      <description>&lt;h1&gt;
  
  
  Breadth-First Search
&lt;/h1&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;On my path to improving my data structures and algorithms knowledge, I decided to blog about breadth-first search. This algorithm comes up a lot in interviews and it will be important to master.&lt;/p&gt;

&lt;p&gt;Breath-first search is an algorithm for searching trees and graphs. Trees and graphs are two different types of data structures. From this &lt;a href="https://techdifferences.com/difference-between-tree-and-graph.html"&gt;article&lt;/a&gt;,&lt;/p&gt;

&lt;p&gt;"Tree and graph come under the category of non-linear data structure where tree offers a very useful way of representing a relationship between the nodes in a hierarchical structure and graph follows a network model. Tree and graph are differentiated by the fact that a tree structure must be connected and can never have loops while in the graph there are no such restrictions."&lt;/p&gt;

&lt;p&gt;The article also has this image that I think clarifies the difference between the two:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XuWUA5QJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://techdifferences.com/wp-content/uploads/2018/03/Untitled-1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XuWUA5QJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://techdifferences.com/wp-content/uploads/2018/03/Untitled-1.jpg" alt="tree vs graph"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see from the diagram above, a tree progresses out from the root node at the top and doesn't loop, where as the graph doesn't necessarily have a set structure and looks more like a web.&lt;/p&gt;

&lt;h3&gt;
  
  
  Breadth-First Search
&lt;/h3&gt;

&lt;p&gt;Breadth-first search is used to search a tree or graph. The algorithm starts at the tree root or a selected node on the graph. All neighbors of the node at the current depth will be explored before moving onto the next level. For example using the diagram above, if we start at tree root A on level zero, next its children will be explored(B and C). Since all nodes have been explored on level one of the tree, the next level will be explored(D, E, F). Finally, the last level will be explored (G, H).&lt;/p&gt;

&lt;h5&gt;
  
  
  Implementation
&lt;/h5&gt;

&lt;p&gt;Breadth-first search uses a queue to store visited nodes. A queue is a data structure where elements are removed first-in first-out(FIFO). For example, say we have this queue &lt;code&gt;[]&lt;/code&gt;, and we add D, E, and F &lt;code&gt;[D, E, F]&lt;/code&gt; from the second level. Adding to the queue is known as enqueuing. D will be removed from the queue first since it was added first. This is also known as dequeuing.&lt;/p&gt;

&lt;p&gt;Here is some &lt;a href="https://en.wikipedia.org/wiki/Breadth-first_search"&gt;pseudocode&lt;/a&gt; from Wikipedia:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;NOTE: The words vertex and node are used interchangeably in my step by step walkthrough below.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1 procedure BFS(G, start_v) is
2      let Q be a queue
3      label start_v as discovered
4      Q.enqueue(start_v)
5      while Q is not empty do
6          v := Q.dequeue()
7          if v is the goal then
8              return v
9          for all edges from v to w in G.adjacentEdges(v) do
10             if w is not labeled as discovered then
11                 label w as discovered
12                 w.parent := v
13                 Q.enqueue(w)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The function takes in a graph(G) and a starting vertex or root node(start_v).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First an empty queue is initialized &lt;code&gt;queue = []&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The starting node is labeled as discovered since it has already been visited&lt;/li&gt;
&lt;li&gt;The starting node is added to the queue &lt;code&gt;queue = [A]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Next we enter a while loop that exits once the queue is empty&lt;/li&gt;
&lt;li&gt;In this loop, we dequeue and assign that node to a variable v &lt;code&gt;v = A&lt;/code&gt;. Now our queue is empty &lt;code&gt;queue = []&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Next we check if v (node A) is our goal and if it is, return the node.&lt;/li&gt;
&lt;li&gt;If it isn't the goal we enter another loop that occurs once for each adjacent edge of the node. An edge is a connection between two nodes. Think of it as the line between one node and the next. In this case, our adjacent edges connect to nodes &lt;code&gt;G.adjacentEdges(v) = [B, C]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Within the loop, each adjacent node is assigned to a variable w. &lt;code&gt;w = B&lt;/code&gt;. We check if the node has already been discovered and if it hasn't, we label it as discovered, we set its parent as node v &lt;code&gt;w.parent = A&lt;/code&gt;, and we enqueue w &lt;code&gt;queue = [B]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;This will be done with all adjacent vertices. In this case, C is the only adjacent node left.&lt;/li&gt;
&lt;li&gt;After the loop exits, we will be back at line 6 where we dequeue the next vertex (B) and continue with the cycle until the goal node is found.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note from the &lt;a href="https://en.wikipedia.org/wiki/Breadth-first_search"&gt;article&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;"Nodes can be labelled as discovered by storing them in a set, or by an attribute on each node, depending on the implementation."&lt;/p&gt;

&lt;h3&gt;
  
  
  Time and Space Complexity
&lt;/h3&gt;

&lt;h5&gt;
  
  
  Time Complexity
&lt;/h5&gt;

&lt;p&gt;The time complexity of breadth-first search in a graph is O(|V| + |E|), where V is the total number of nodes and E is the total number of edges of the graph since each node should not be visited more than once. For a tree, the time complexity is O(|V|) where V is the number of nodes.&lt;/p&gt;

&lt;p&gt;From this &lt;a href="https://www.quora.com/What-is-the-worst-case-of-time-complexity-of-BFS-when-searching-for-an-element-without-storing-visited-states"&gt;quora&lt;/a&gt; answer:&lt;/p&gt;

&lt;p&gt;"The edge |E| term should not be forgotten because in some cases (like highly connected graphs) it can be much larger than the number of nodes. On a tree we can just ignore it."&lt;/p&gt;

&lt;h5&gt;
  
  
  Space Complexity
&lt;/h5&gt;

&lt;p&gt;The space complexity of breadth-first search is O(|V|) since the worst case is holding all of the vertices in the queue at the same time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Cases
&lt;/h3&gt;

&lt;p&gt;Breadth-first search is useful if you want to find the shortest path between one vertex and another in an unweighted graph. An unweighted graph is a graph where the edges have weights attached to them.&lt;/p&gt;

&lt;p&gt;From this &lt;a href="https://medium.com/tebs-lab/types-of-graphs-7f3891303ea8"&gt;blog&lt;/a&gt;:&lt;/p&gt;

&lt;p&gt;"A weight is a numerical value attached to each individual edge. In a weighted graph relationships between nodes have a magnitude and this magnitude is important to the relationship we’re studying. In an unweighted graph the existence of a relationship is the subject of our interest."&lt;/p&gt;

&lt;p&gt;BFS can also be used to check if there is a path between two vertices, find the length of the path, or find all of the nodes reachable from a given node.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Summary
&lt;/h3&gt;

&lt;p&gt;Breadth-first search is an algorithm that every programmer learns at some point in their journey. Let me know about your experiences using it. Thank you for reading and happy searching!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xT9IgFLfWUZigjoem4/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xT9IgFLfWUZigjoem4/giphy.gif" alt="searching computer"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Intro to Hash Tables</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Mon, 16 Mar 2020 04:06:23 +0000</pubDate>
      <link>https://dev.to/racheladaw/intro-to-hash-tables-57n5</link>
      <guid>https://dev.to/racheladaw/intro-to-hash-tables-57n5</guid>
      <description>&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;A hash table is a data structure that contains key, value pairs. Hash tables use something called a hash function to store and retrieve the data. The hashing function will allow you to map the key to a hash code in order to retrieve the index for the data you are looking for. This is similar to the Dewey Decimal System in a library where books are looked up based on a decimal number. With this system, books are categorized by genre and assigned a number within a specific range.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stored Data
&lt;/h3&gt;

&lt;p&gt;Hash tables have a predetermined length, where each of the cells stores a key value pair. These cells are usually referred to as "buckets". Often the data is stored in an array with each bucket being an index in the array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hashing Functions
&lt;/h3&gt;

&lt;p&gt;As I mentioned at the beginning of this blog, hash tables use a hashing function. This function tells us where to find an element in the array by inputting a key. Basically, the function will map a string to a number. Ideally, our hashing function will output unique index numbers for each key. Otherwise, the time complexity of retrieving an element will be closer to O(n) where n is the number of elements in the data set. When a hashing function returns the same outputted index for two or more different keys it is called a collision.&lt;/p&gt;

&lt;h3&gt;
  
  
  Collisions
&lt;/h3&gt;

&lt;p&gt;Collisions happen when two keys get mapped to the same number or index. There are a few different ways to handle this scenario. One way is by storing the key value pairs that share the same index in a linked list. This is called &lt;strong&gt;separate chaining&lt;/strong&gt;. Using this method, you will have to iterate through the pairs to find the item you are looking for. This could lead to inefficiency if there are a lot of collisions and bring the time complexity closer to O(n).&lt;/p&gt;

&lt;p&gt;Here is an example with a collision. In this example the key is the name of the dog and the value is the dog's breed. We are hashing the dog's name to get the index we should store its data at. Cruiser and Ranger's names both hash to the index of 7, so they are stored in a linked list at index 7.&lt;/p&gt;

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

&lt;p&gt;Another option to handle collisions is called &lt;strong&gt;open addressing&lt;/strong&gt;, also known as closed hashing. With this method, the key is used to find the location in the array to look at first. If that spot is already full, a scheme will be used to probe the array for another location. This is done until an empty bucket is found to store the data. One scheme you can use is called linear probing where you look at the next spot in the array if the previous one is full. &lt;a href="https://stackoverflow.com/questions/2556142/chained-hash-tables-vs-open-addressed-hash-tables"&gt;Here&lt;/a&gt; is a great Stack Overflow article on open addressing versus chaining.&lt;/p&gt;

&lt;p&gt;Usually, open addressing is faster than separate chaining. However, if all of the buckets are close to being full, open addressing will be slow because it will take a lot of time to look through the buckets and find an open location.    &lt;/p&gt;

&lt;h3&gt;
  
  
  Run Time
&lt;/h3&gt;

&lt;p&gt;Hash tables perform fast retrieval, insertion, and deletion of data, however they are not very fast when the data needs to be searched / iterated over. For example, if you need to iterate over the data to find a maximum value, a hash table would not be the most efficient data structure. &lt;/p&gt;

&lt;p&gt;Retrieval, insertion, and deletion from a hash table should have a time complexity of O(1), unless the hashing function isn't optimized and there are collisions. For example, if there are a lot of collisions and separate chaining is used, the lists will need to be iterated through in order to find the key value pair that you are looking for. &lt;/p&gt;

&lt;h3&gt;
  
  
  Closing Time
&lt;/h3&gt;

&lt;p&gt;When is a time you have used hash tables in your applications? It would be great to hear some good use cases for this data structure. &lt;/p&gt;

&lt;h5&gt;
  
  
  References
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=F95z5Wxd9ks"&gt;Hash Table Video&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.csl.mtu.edu/cs2321/www/newLectures/17_Hash_Tables_Collisions.html"&gt;Hash Table Collision Handling&lt;/a&gt;&lt;/p&gt;

</description>
      <category>computerscience</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Finding Max in a Stack</title>
      <dc:creator>Rachel Williams</dc:creator>
      <pubDate>Mon, 09 Mar 2020 04:56:36 +0000</pubDate>
      <link>https://dev.to/racheladaw/finding-max-in-a-stack-iej</link>
      <guid>https://dev.to/racheladaw/finding-max-in-a-stack-iej</guid>
      <description>&lt;h2&gt;
  
  
  Stack Problem
&lt;/h2&gt;

&lt;p&gt;I recently had a phone interview with a company and was asked a question about stacks that I didn't know how to properly solve. This prompted me to deepen my knowledge of stacks.&lt;/p&gt;

&lt;p&gt;I was asked to think about how I would write a max function to find the maximum value in a stack.&lt;/p&gt;

&lt;h2&gt;
  
  
  Stack Refresher
&lt;/h2&gt;

&lt;p&gt;A stack is a data structure where newer items are added on top and items are also removed from the top. This is called Last In First Out (LIFO). One way to represent a stack is an array. When thinking about the problem on the phone call, I pictured an array where items would be added onto the end and also taken off of the end.&lt;/p&gt;

&lt;p&gt;Here is an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[2, 3, 3, 1, 1, 100, 200, 100, 400, 30]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;30 would be the top of the stack and would also be the first number removed from the stack.&lt;/p&gt;

&lt;h1&gt;
  
  
  Back to the Problem
&lt;/h1&gt;

&lt;p&gt;During the phone call, I could only come up with a few solutions, which I knew were not what the interviewer was looking for. My first obvious thought was to loop through the array, storing the current maximum number and return that number at the end. This was clearly not what the interviewer was looking for and asked me how I would solve this with a different data structure.&lt;/p&gt;

&lt;p&gt;Next I tried to think of a way I could use an object, and linked lists came to mind. My idea was to store the current maximum value in a linked list with a pointer to the previous maximum number, in case the current maximum number was deleted. I didn't fully think through this idea, as I hadn't reviewed how to implement a linked list in a while and I had a hunch that this wasn't what the interviewer was looking for.&lt;/p&gt;

&lt;p&gt;We moved on from this question and I was determined to figure out the answer once I got off the phone.&lt;/p&gt;

&lt;h2&gt;
  
  
  After Some Googling
&lt;/h2&gt;

&lt;p&gt;After some googling, I came across an &lt;a href="https://www.growingwiththeweb.com/2017/05/max-aware-stack.html"&gt;implementation&lt;/a&gt; that seemed like what my interviewer was actually looking for.&lt;/p&gt;

&lt;p&gt;With this implementation, you would have an additional stack to store your max values. You would read the top value from this extra stack to see what the current maximum value was.&lt;/p&gt;

&lt;p&gt;For example, given this initial stack:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;I would have a max value stack that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[3]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Three is the only number in the stack so far, thus it is the maximum. Now if I push 5 onto the stack, my current max should be 5. So we will add that onto our max value stack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//currentStack
[3, 5]
//maxNumStack
[3, 5]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now say we add a number that is less than or equal to our current max onto the stack. Now, we would just add the current max again to our maxNumStack to align the lengths of the stacks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//currentStack
[3, 5, 4]
//maxNumStack
[3, 5, 5]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This way, if 4 is popped off of the stack, we can pop one element off of our maxNumStack as well and still know what the currentMax is (in this case 5).&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing the Stack With a Max Method
&lt;/h2&gt;

&lt;p&gt;This is how I might implement this with JavaScript. I roughly followed this &lt;a href="https://medium.com/better-programming/implementing-a-stack-in-javascript-73d1aa0483c1"&gt;blog&lt;/a&gt; to implement the basic features of a stack, such as the push and pop methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Stack {
  constructor() {
    this.data = [];
    this.size = 0;
    this.maxValues = [];
  }

  push(element) {
    // if the stack is empty or the element we're pushing is greater than currentMax, add the new element to maxValues
    if (this.size === 0 || element &amp;gt;= this.findMax()) {
      this.maxValues.push(element)
    }
    // otherwise, push the currentMax to maxValues again, to align lengths of arrays and keep currentMax
    else {
      this.maxValues.push(this.findMax())
    }
    // increase the size variable by 1 to keep track of length, and add element to stack
    this.size += 1
    this.data.push(element);
    return this.data
  }

  pop() {
    // if the stack isn't empty, decrease the size count and remove element from the end of the stack and maxValue array to keep lengths aligned
    if (this.size &amp;gt; 0) {
      this.size -= 1;
      this.maxValues.pop()
      return this.data.pop()
    }
  }

  // this returns the top element in the stack (last element in the array)
  peek() {
    return this.data[this.size - 1]
  }

  // this returns the maxValue currently in the stack, by grabbing the last element of the maxValue stack
  findMax() {
    return this.maxValues[this.size - 1]
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here is some code I wrote to test that it worked along with the outputs in my terminal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let stack = new Stack()

stack.push(5)
stack.push(5)
stack.push(7)
stack.push(3)
stack.push(4)

console.log(stack)
// Stack {data: [ 5, 5, 7, 3, 4 ], size: 5, maxValues: [ 5, 5, 7, 7, 7 ]}
console.log("max", stack.findMax())
// max 7

stack.pop()
console.log(stack)
// Stack { data: [ 5, 5, 7, 3 ], size: 4, maxValues: [ 5, 5, 7, 7 ] }
console.log("max", stack.findMax())
// max 7

stack.pop()
console.log(stack)
// Stack { data: [ 5, 5, 7 ], size: 3, maxValues: [ 5, 5, 7 ] }
console.log("max", stack.findMax())
// max 7

stack.pop()
console.log(stack)
// Stack { data: [ 5, 5 ], size: 2, maxValues: [ 5, 5 ] }
console.log("max", stack.findMax())
// max 5
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this implementation, theoretically all you would be doing to find the max is a quick lookup operation to find the last element in the maxValues array. Looking up an element in an array by index has a time complexity of O(1). Also, the &lt;code&gt;push()&lt;/code&gt; and &lt;code&gt;pop()&lt;/code&gt; operations to add and remove values from the maxValues stack have a time complexity of O(1). This is significantly more efficient than looping through the array to find the current maximum every time, which would be O(n), where n is the length of the array.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;As a side note, there are other ways to implement the push and pop methods, however that wasn't the focus of this blog so I chose to use the built in array methods.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrapping It Up
&lt;/h2&gt;

&lt;p&gt;When is a time you have used a stack in your code? Let me know what you think of this solution and if you think it can be improved. Thank you for reading!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/hRsayJrDAx8WY/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/hRsayJrDAx8WY/giphy.gif" alt="pancake stacks"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>computerscience</category>
      <category>node</category>
    </item>
  </channel>
</rss>
