<?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: Cole Rau</title>
    <description>The latest articles on DEV Community by Cole Rau (@colerau).</description>
    <link>https://dev.to/colerau</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%2F316796%2F001090dc-51c4-4fee-85ab-559d8a2386cf.jpg</url>
      <title>DEV Community: Cole Rau</title>
      <link>https://dev.to/colerau</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/colerau"/>
    <language>en</language>
    <item>
      <title>Creating a Short Math Quiz with Bash Scripting</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 07 Mar 2021 04:12:56 +0000</pubDate>
      <link>https://dev.to/colerau/creating-a-short-math-quiz-with-bash-scripting-64p</link>
      <guid>https://dev.to/colerau/creating-a-short-math-quiz-with-bash-scripting-64p</guid>
      <description>&lt;p&gt;&lt;em&gt;This article explains how to create and run a short math quiz using Bash scripting.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1: Create a .sh file
&lt;/h1&gt;

&lt;p&gt;In Terminal, navigate to a directory where you'll want to put your Bash script. In that directory, open your preferred text editor and create a &lt;code&gt;quiz.sh&lt;/code&gt; file. The &lt;code&gt;.sh&lt;/code&gt; lets your text editor know that you're making a Bash &lt;code&gt;sh&lt;/code&gt;ell script file. &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 2: Write the Bash script
&lt;/h1&gt;

&lt;p&gt;The math quiz Bash script in its entirety looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;1    &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
2
3    quiz&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
4      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Starting quiz..."&lt;/span&gt;
5      &lt;span class="nv"&gt;random1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
6      &lt;span class="nv"&gt;random2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
7      &lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$random1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$random2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
8  
9      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"What is &lt;/span&gt;&lt;span class="nv"&gt;$random1&lt;/span&gt;&lt;span class="s2"&gt; + &lt;/span&gt;&lt;span class="nv"&gt;$random2&lt;/span&gt;&lt;span class="s2"&gt;?"&lt;/span&gt;
10     &lt;span class="nb"&gt;read &lt;/span&gt;response
11     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
12     &lt;span class="k"&gt;then
&lt;/span&gt;13       &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You got it."&lt;/span&gt;
14     &lt;span class="k"&gt;else 
&lt;/span&gt;15       &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"That is incorrect."&lt;/span&gt;
16     &lt;span class="k"&gt;fi
&lt;/span&gt;17   &lt;span class="o"&gt;}&lt;/span&gt;
18
19   &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Begin math quiz? (y/n)"&lt;/span&gt;
20   &lt;span class="nb"&gt;read &lt;/span&gt;response
21   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"y"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Y"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
22   &lt;span class="k"&gt;then 
&lt;/span&gt;23     &lt;span class="nv"&gt;SECONDS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0
24     quiz
25     &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"The quiz took you &lt;/span&gt;&lt;span class="nv"&gt;$SECONDS&lt;/span&gt;&lt;span class="s2"&gt; seconds to complete."&lt;/span&gt;
26   &lt;span class="k"&gt;else 
&lt;/span&gt;27     &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Exiting..."&lt;/span&gt;
28   &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://github.com/colerau/bash-math-quiz/blob/master/quiz.sh"&gt;HERE IS THE CODE WITHOUT COPYABLE LINE NUMBERS&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 3: Make the script file executable
&lt;/h1&gt;

&lt;p&gt;In the directory that your script file lives in, enter the command &lt;code&gt;chmod +x yourScriptFile.sh&lt;/code&gt;. In the context of this article, since our script file is called &lt;code&gt;quiz.sh&lt;/code&gt;, we'll enter &lt;code&gt;chmod +x quiz.sh&lt;/code&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4: Run the script file
&lt;/h1&gt;

&lt;p&gt;In the script file's directory, enter the command &lt;code&gt;./yourScriptFile.sh&lt;/code&gt;. In the context of this article, since our script file is called &lt;code&gt;quiz.sh&lt;/code&gt;, we'll enter in &lt;code&gt;./quiz.sh&lt;/code&gt;. Alternatively, we can run the script file with &lt;code&gt;bash quiz.sh&lt;/code&gt;. &lt;/p&gt;




&lt;h1&gt;
  
  
  Bash Script Explanation
&lt;/h1&gt;

&lt;p&gt;In reference to the Bash script below...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;1    &lt;span class="c"&gt;#!/bin/bash&lt;/span&gt;
2
3    quiz&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
4      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Starting quiz..."&lt;/span&gt;
5      &lt;span class="nv"&gt;random1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
6      &lt;span class="nv"&gt;random2&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
7      &lt;span class="nv"&gt;result&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$random1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nv"&gt;$random2&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
8  
9      &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"What is &lt;/span&gt;&lt;span class="nv"&gt;$random1&lt;/span&gt;&lt;span class="s2"&gt; + &lt;/span&gt;&lt;span class="nv"&gt;$random2&lt;/span&gt;&lt;span class="s2"&gt;?"&lt;/span&gt;
10     &lt;span class="nb"&gt;read &lt;/span&gt;response
11     &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
12     &lt;span class="k"&gt;then
&lt;/span&gt;13       &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"You got it."&lt;/span&gt;
14     &lt;span class="k"&gt;else 
&lt;/span&gt;15       &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"That is incorrect."&lt;/span&gt;
16     &lt;span class="k"&gt;fi
&lt;/span&gt;17   &lt;span class="o"&gt;}&lt;/span&gt;
18
19   &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Begin math quiz? (y/n)"&lt;/span&gt;
20   &lt;span class="nb"&gt;read &lt;/span&gt;response
21   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"y"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$response&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;"Y"&lt;/span&gt; &lt;span class="o"&gt;]&lt;/span&gt;
22   &lt;span class="k"&gt;then 
&lt;/span&gt;23     &lt;span class="nv"&gt;SECONDS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;0
24     quiz
25     &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"The quiz took you &lt;/span&gt;&lt;span class="nv"&gt;$SECONDS&lt;/span&gt;&lt;span class="s2"&gt; seconds to complete."&lt;/span&gt;
26   &lt;span class="k"&gt;else 
&lt;/span&gt;27     &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"Exiting..."&lt;/span&gt;
28   &lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...we begin with line 19, where we use &lt;code&gt;echo&lt;/code&gt; to output the string &lt;code&gt;"Begin math quiz? (y/n)"&lt;/code&gt; to the console. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Before we go further, it's essential to note that &lt;code&gt;#!/bin/bash&lt;/code&gt; is written at the top of the file. This is needed so that your computer knows to use the Bash interpreter for your file's code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Moving on, line 20 initializes a variable &lt;code&gt;response&lt;/code&gt; and uses &lt;code&gt;read&lt;/code&gt; to let the user input its value. In other words, whatever the user types into the console will become the value of &lt;code&gt;response&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Line 21 starts a conditional statement. If the value of response (&lt;code&gt;$response&lt;/code&gt;) is equal to "y" or "Y", the code after the &lt;code&gt;then&lt;/code&gt; is executed.&lt;/p&gt;

&lt;p&gt;Three things to note: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The variable &lt;code&gt;response&lt;/code&gt; is prepended with a &lt;code&gt;$&lt;/code&gt; because in Bash, to get the value of a variable, you need to include a &lt;code&gt;$&lt;/code&gt; before the variable name.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Second, we're not testing for equality with &lt;code&gt;-eq&lt;/code&gt; because that is for &lt;em&gt;integer&lt;/em&gt; equality tests; here, because we're testing the equality between two &lt;em&gt;strings&lt;/em&gt;, &lt;code&gt;==&lt;/code&gt; is used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Third, the quotes around the &lt;code&gt;$response&lt;/code&gt; variables on line 21 are there just to prevent bugs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On line 24, if the user types in "y" or "Y", the quiz function is called. The quiz function is defined in lines 3 - 17. &lt;/p&gt;

&lt;p&gt;If the user types in something else, the &lt;code&gt;else&lt;/code&gt; code is executed, which just outputs "Exiting..." to the screen. The &lt;code&gt;fi&lt;/code&gt; is how you end an if...else conditional code block in Bash. &lt;/p&gt;

&lt;p&gt;To gauge how many seconds it took the user to complete the quiz, we declare the built-in variable &lt;code&gt;SECONDS&lt;/code&gt; on line 23, give it an initial value of 0, run the quiz function, then when the user is finished with the quiz, we use &lt;code&gt;$SECONDS&lt;/code&gt; on line 25 to provide the amount of time elapsed. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Code Inside the Quiz Function
&lt;/h3&gt;

&lt;p&gt;If the user typed "y" or "Y", the quiz function code will be run. Inside the quiz function, on line 5, we assign a random integer between 0 and 327 to the &lt;code&gt;random1&lt;/code&gt; variable using the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;random1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;$((&lt;/span&gt;&lt;span class="nv"&gt;$RANDOM&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="k"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;$(( ))&lt;/code&gt; allows us to perform an arithmetic expression and assign it to a variable. &lt;/p&gt;

&lt;p&gt;On line 6, we get a second random integer. &lt;/p&gt;

&lt;p&gt;On line 7, we save the sum of the two integers to a variable called &lt;code&gt;result&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;From lines 11 - 16, if the user got the question right (if the &lt;code&gt;response&lt;/code&gt; variable is equal to the &lt;code&gt;result&lt;/code&gt; variable), &lt;code&gt;"You got it."&lt;/code&gt; is outputted to the screen. Else, &lt;code&gt;"That is incorrect."&lt;/code&gt; is displayed to the user. &lt;/p&gt;

</description>
      <category>bash</category>
      <category>scripting</category>
    </item>
    <item>
      <title>Creating a has_many/belongs_to relationship in Rails</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 28 Feb 2021 07:46:50 +0000</pubDate>
      <link>https://dev.to/colerau/creating-a-hasmany-belongsto-relationship-in-rails-oe1</link>
      <guid>https://dev.to/colerau/creating-a-hasmany-belongsto-relationship-in-rails-oe1</guid>
      <description>&lt;p&gt;&lt;a href="https://github.com/colerau/people" rel="noopener noreferrer"&gt;Completed Example Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article will cover how to set up a basic Rails relational database. Our example database will have two models (Ruby classes): &lt;code&gt;Person&lt;/code&gt; and &lt;code&gt;Interest&lt;/code&gt;. We will say that a person has many interests, and an interest belongs to a person. A person will have the attributes &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;age&lt;/code&gt;; an interest will have the attributes &lt;code&gt;activity_name&lt;/code&gt; and &lt;code&gt;person_id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Below is a visual representation of our database:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fppcgtd63j1ow88rz0psr.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%2Fuploads%2Farticles%2Fppcgtd63j1ow88rz0psr.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1: Navigate to your Rails project or create a new one
&lt;/h1&gt;

&lt;p&gt;You'll want to be inside of your Rails project for the following steps. If you do not have one:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;assuming you have &lt;em&gt;Ruby&lt;/em&gt;, &lt;em&gt;SQLite3&lt;/em&gt;, &lt;em&gt;Node.js&lt;/em&gt;, &lt;em&gt;Yarn&lt;/em&gt;, and &lt;em&gt;Rails&lt;/em&gt; itself downloaded to your computer,&lt;/li&gt;
&lt;li&gt;In Terminal, navigate to the folder you want your Rails app to reside in.&lt;/li&gt;
&lt;li&gt;Enter the command &lt;code&gt;rails new {lowercase and no spaces app name}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Enter the command &lt;code&gt;cd {lowercase and no spaces app name}&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Open up your text editor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;I'm making a Rails app called &lt;code&gt;People&lt;/code&gt; so I will, in Terminal, navigate to the folder I want my Rails app to be in, then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;run the command &lt;code&gt;rails new people&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;run the command &lt;code&gt;cd people&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;run the command &lt;code&gt;code .&lt;/code&gt; to open up VSCode.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Step 2: Inside your text editor, create the Person model
&lt;/h1&gt;

&lt;p&gt;In Terminal, in your Rails app directory, enter &lt;br&gt;
&lt;code&gt;rails g model NameOfModel column_name:datatype column_name2:datatype2 --no-test-framework&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;To make the &lt;code&gt;Person&lt;/code&gt; model, I'll enter in Terminal&lt;br&gt;
&lt;code&gt;rails g model Person name:string age:integer --no-test-framework&lt;/code&gt;. Now I have a SQLite model called &lt;code&gt;Person&lt;/code&gt; that has the attributes &lt;code&gt;name&lt;/code&gt; (a string), and &lt;code&gt;age&lt;/code&gt; (an integer).&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 3: Create the Interest model
&lt;/h1&gt;

&lt;p&gt;Follow the instructions in Step 2 but for the Interest model. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;To make the &lt;code&gt;Interest&lt;/code&gt; model, I'll enter in Terminal&lt;br&gt;
&lt;code&gt;rails g model Interest activity_name:string person_id:integer --no-test-framework&lt;/code&gt;. Now I have a SQLite model called &lt;code&gt;Interest&lt;/code&gt; that has the attributes &lt;code&gt;activity_name&lt;/code&gt; (a string) and &lt;code&gt;person_id&lt;/code&gt; (an integer). &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;The model that "belongs to" another model will have the attribute &lt;code&gt;lowercase other model's name_id.&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;&lt;em&gt;That is why, in our example case, the &lt;code&gt;Interest&lt;/code&gt; model has an attribute of &lt;code&gt;person_id&lt;/code&gt;.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 4: Add the has_many relationship
&lt;/h1&gt;

&lt;p&gt;In the model that "has_many", add the code such that your file looks like this:&lt;/p&gt;

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

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ModelThatHasMany&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="n"&gt;lowercase&lt;/span&gt; &lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;has&lt;/span&gt; &lt;span class="n"&gt;many&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Since a person has many interests, we will add the &lt;code&gt;has_many&lt;/code&gt; relationship to the &lt;code&gt;Person&lt;/code&gt; model. &lt;/p&gt;

&lt;p&gt;In &lt;code&gt;app/models/person.rb&lt;/code&gt;, I will make the file look like the following: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;has_many&lt;/span&gt; &lt;span class="ss"&gt;:interests&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;I'm telling Rails that an instance of my &lt;code&gt;Person&lt;/code&gt; class will have many interests. &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 5: Add the belongs_to relationship
&lt;/h1&gt;

&lt;p&gt;Identify the model that belongs to another model.&lt;/p&gt;

&lt;p&gt;In the model that belongs to another model, add the following code:&lt;/p&gt;

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

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ModelThatBelongsToAnotherModel&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="p"&gt;:{&lt;/span&gt;&lt;span class="nb"&gt;name&lt;/span&gt; &lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;this&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="n"&gt;belongs&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;
&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;Since an interest belongs to a person, we will add the &lt;code&gt;belongs_to&lt;/code&gt; relationship to the &lt;code&gt;Interest&lt;/code&gt; model. &lt;/p&gt;

&lt;p&gt;In &lt;code&gt;app/models/interest.rb&lt;/code&gt;, I will make the file look like the following: &lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Interest&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:person&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;I'm telling Rails that an instance of my &lt;code&gt;Interest&lt;/code&gt; class will belong to a person instance.&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 6: Run rails db:migrate in Terminal
&lt;/h1&gt;

&lt;p&gt;In Terminal, enter the command &lt;code&gt;rails db:migrate&lt;/code&gt; to create your database with the code you've already written. &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 7: Write some seeds
&lt;/h1&gt;

&lt;p&gt;In &lt;code&gt;db/seeds.rb&lt;/code&gt;, create some instances of your classes (models), then type in &lt;code&gt;rails c&lt;/code&gt; in Terminal to test out the relationships. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;We want to see if a person truly does have many interests and if an interest truly does belong to a person. &lt;/p&gt;

&lt;p&gt;To do that, we'll create some seeds to later test out in the rails console.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;db/seeds.rb&lt;/code&gt;, add the following code: &lt;/p&gt;

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

&lt;span class="c1"&gt;# person1 will have an id of 1&lt;/span&gt;
&lt;span class="n"&gt;person1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Steve"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# person2 will have an id of 2&lt;/span&gt;
&lt;span class="n"&gt;person2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;name: &lt;/span&gt;&lt;span class="s2"&gt;"Alex"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;age: &lt;/span&gt;&lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This code creates two person instances. "Steve" will have an id of 1. "Alex" will have an id of 2. &lt;/p&gt;

&lt;p&gt;Now add some more code to the seeds.rb file:&lt;/p&gt;

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

&lt;span class="c1"&gt;# person1 will have the following interests&lt;/span&gt;
&lt;span class="n"&gt;interest1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Interest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;activity_name: &lt;/span&gt;&lt;span class="s2"&gt;"walking"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;person_id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;interest2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Interest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;activity_name: &lt;/span&gt;&lt;span class="s2"&gt;"playing the viola"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;person_id: &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# person2 will have the following interests&lt;/span&gt;
&lt;span class="n"&gt;interest3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Interest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;activity_name: &lt;/span&gt;&lt;span class="s2"&gt;"reading"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;person_id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;interest4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Interest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;activity_name: &lt;/span&gt;&lt;span class="s2"&gt;"playing the piano"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;person_id: &lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;This code creates four interest instances. Taking into consideration that "Steve" has an id of 1, since interest1 has a person_id of 1, interest1 will belong to "Steve". Since interest2 has a person_id of 1, interest2 will belong to "Steve". "Steve" now has many interests (interest1 and interest2). &lt;/p&gt;

&lt;p&gt;Taking into consideration that "Alex" has an id of 2, since interest3 has a person_id of 2, interest3 will belong to "Alex". Since interest4 has a person_id of 2, interest4 will belong to "Alex". "Alex" now has many interests (interest3 and interest4). &lt;/p&gt;

&lt;h1&gt;
  
  
  Step 8: Test out the relationships in the Rails console
&lt;/h1&gt;

&lt;p&gt;Entering the rails console will allow us to play around with our model instances and how they are related to each other. &lt;/p&gt;

&lt;h2&gt;
  
  
  Example
&lt;/h2&gt;

&lt;p&gt;In Terminal, I'll type in &lt;code&gt;rails c&lt;/code&gt; to enter the Rails console.&lt;/p&gt;

&lt;p&gt;I want to see if my model instances are associated correctly. According to my seeds.rb file, Steve's first interest should be walking. It's relevant to note that Steve has an id of 1. Therefore, if I type into the console &lt;code&gt;Person.first.interests.first.activity_name&lt;/code&gt;, &lt;code&gt;"walking"&lt;/code&gt; should be returned. &lt;/p&gt;

&lt;p&gt;Does the interest of "walking" belong to Steve? To test this out, I'll type into the console &lt;code&gt;Interest.first.person.name&lt;/code&gt;. This should return &lt;code&gt;"Steve"&lt;/code&gt;. (&lt;code&gt;Interest.first&lt;/code&gt; is the walking interest since the walking interest has an id of 1). &lt;/p&gt;




&lt;p&gt;&lt;a href="https://github.com/colerau/people" rel="noopener noreferrer"&gt;Completed Example Code&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>rails</category>
      <category>ruby</category>
      <category>hasmanybelongsto</category>
    </item>
    <item>
      <title>Build process of my first deployed web app</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 21 Feb 2021 04:54:49 +0000</pubDate>
      <link>https://dev.to/colerau/build-process-of-my-first-deployed-web-app-3ij7</link>
      <guid>https://dev.to/colerau/build-process-of-my-first-deployed-web-app-3ij7</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pRZWKzht--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hun9km5la1rrctqcxvz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pRZWKzht--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hun9km5la1rrctqcxvz9.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To solidify my programming skills, I decided to create and deploy a compound interest and amortization calculator. &lt;/p&gt;

&lt;p&gt;The app can be found &lt;a href="https://money-calculators.herokuapp.com/"&gt;here&lt;/a&gt;. &lt;br&gt;
The source code can be found &lt;a href="https://github.com/colerau/money-calculators"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  App Overview
&lt;/h1&gt;

&lt;p&gt;In the Compound Interest Calculator, a user can find the compound interest accrued in an investment by supplying the investment's starting amount, interest rate, number of years, and compounding interval (monthly or yearly). If a user wanted to determine their monthly payment on a loan, they could use the Amortization Calculator. The Amortization Calculator will automatically generate an amortization schedule, which each month shows how much of their monthly payment will go toward principal and interest. &lt;/p&gt;

&lt;h1&gt;
  
  
  useState
&lt;/h1&gt;

&lt;p&gt;The app, which I call Money Calculators, was made with React. I decided to try the &lt;code&gt;useState&lt;/code&gt; react hook to add internal state to my functional components. &lt;code&gt;useState&lt;/code&gt; allowed me to code the project without a single React class component. Basically, &lt;code&gt;useState&lt;/code&gt; creates two variables: a variable that holds a specific slice of the component's current state and a function that will update that state. You can think of a React component's state like a global variable - accessible in any function inside that component.&lt;/p&gt;

&lt;h1&gt;
  
  
  Submit Button
&lt;/h1&gt;

&lt;p&gt;The most challenging part of the project was getting rid of the submit button in my forms. This way, the user can change form input values and immediately see the new result. For instance, if a user has filled out all the form boxes for the compound interest calculator, they will see a number on the screen. If they decide to update a form box, they will get a new compound interest calculation right away.&lt;/p&gt;

&lt;p&gt;In the compound interest component, to get rid of the submit button yet still show a compound interest calculation to the user, I did the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The compound interest is calculated via a formula that requires four variables. The user provides each of these variables. &lt;/li&gt;
&lt;li&gt;Each variable is saved to the compound interest component's internal state&lt;/li&gt;
&lt;li&gt;A getFinalAmount function is created. This function produces a compound interest based on the component's state. This function will only return a truthy value (not zero) if all the component's forms are filled out. &lt;/li&gt;
&lt;li&gt;In the component's return function, a ternary operator is introduced. If calling getFinalAmount returns a truthy value, a piece of HTML is rendered to the screen that shows the result of the compound interest calculation. If calling getFinalAmount returns a falsy value, a React fragment is rendered. The user will not be able to see this React fragment.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Deploying to Heroku
&lt;/h1&gt;

&lt;p&gt;This was my first time publishing an application of mine to the Internet, so I was pretty nervous putting my app on Heroku because I didn't want to mess anything up. In the end, I succeeded by doing the following: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make a Heroku account&lt;/li&gt;
&lt;li&gt;Download the &lt;a href="https://devcenter.heroku.com/articles/heroku-cli"&gt;Heroku CLI&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Push your project to GitHub&lt;/li&gt;
&lt;li&gt;I built a React app so I followed the steps in &lt;a href="https://devcenter.heroku.com/articles/deploying-nodejs"&gt;this&lt;/a&gt; article.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;The above article says as a final step to write &lt;code&gt;git push heroku main&lt;/code&gt; in Mac Terminal. I needed to change &lt;code&gt;main&lt;/code&gt; to &lt;code&gt;master&lt;/code&gt; for my project to send to Heroku. So I wrote &lt;code&gt;git push heroku master&lt;/code&gt; instead.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>heroku</category>
      <category>react</category>
      <category>herokucli</category>
      <category>usestate</category>
    </item>
    <item>
      <title>Map, Reduce, Filter/Select: Ruby vs. JavaScript</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 14 Feb 2021 07:41:13 +0000</pubDate>
      <link>https://dev.to/colerau/map-reduce-filter-select-ruby-vs-javascript-1d9f</link>
      <guid>https://dev.to/colerau/map-reduce-filter-select-ruby-vs-javascript-1d9f</guid>
      <description>&lt;h1&gt;
  
  
  Map
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Ruby
def addTwoZeros(array)
  array.map { |element| element * 100 }
end 



// JavaScript
const addTwoZeros = array =&amp;gt; {
  return array.map((element) =&amp;gt; {
    return element * 100
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above functions/methods take in a list of numbers, add two zeros to the end of each number, and return a new list of transformed numbers. The main idea behind &lt;code&gt;map&lt;/code&gt; is transforming each member in an array and putting it into a new array. This new array will eventually hold a collection of transformed elements in the same order as the original array. &lt;/p&gt;

&lt;p&gt;In the Ruby implementation, each element of the array goes through &lt;code&gt;|element|&lt;/code&gt; and undergoes the code to the right of &lt;code&gt;|element|&lt;/code&gt;. That is, each element goes through the pipes &lt;code&gt;||&lt;/code&gt; and gets 100 multiplied to it.&lt;/p&gt;

&lt;p&gt;In the JavaScript implementation, the &lt;code&gt;map&lt;/code&gt; function gets another function passed to it. Each element of the array gets passed through the &lt;code&gt;(element)&lt;/code&gt; of this secondary function. Then the element gets 100 multiplied to it.&lt;/p&gt;

&lt;h1&gt;
  
  
  Reduce
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Ruby
def getProduct(array)
  array.reduce do |accumulator, element|
    accumulator * element
  end
end 



// JavaScript
const getProduct = array =&amp;gt; {
  return array.reduce((accumulator, element) =&amp;gt; {
    return accumulator * element
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above functions/methods take in an array, multiply all the elements together, then return the result. The main idea behind &lt;code&gt;reduce&lt;/code&gt; is taking a bunch of things and reducing them down to a single value.&lt;/p&gt;

&lt;p&gt;In the Ruby implementation, each element of the array goes through the &lt;code&gt;element&lt;/code&gt; part of &lt;code&gt;|accumulator, element|&lt;/code&gt; and gets multiplied to &lt;code&gt;accumulator&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In the JavaScript implementation, the &lt;code&gt;reduce&lt;/code&gt; function gets another function passed to it. Each element of the array gets passed through the &lt;code&gt;(element)&lt;/code&gt; of this secondary function, then multiplied to &lt;code&gt;accumulator&lt;/code&gt;. &lt;/p&gt;

&lt;h1&gt;
  
  
  Filter/Select
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Ruby
def getOddNums(array)
  array.select { |element| element % 2 != 0 }
end 



// JavaScript
const getOddNums = array =&amp;gt; {
  return array.filter((element) =&amp;gt; {
    return element % 2 !== 0
  })
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above functions/methods take in an array and return only the elements that are odd. The main idea behind &lt;code&gt;filter&lt;/code&gt; / &lt;code&gt;select&lt;/code&gt; is passing each element to a block; if the element makes the block truthy, the element is added to a new array.&lt;/p&gt;

&lt;p&gt;In the Ruby implementation, each element of the array goes through &lt;code&gt;|element|&lt;/code&gt; and undergoes the code to the right of &lt;code&gt;|element|&lt;/code&gt;. If the element makes the code to the right of &lt;code&gt;|element|&lt;/code&gt; truthy, then that element is added to a new array. If the element does not make the code to the right of &lt;code&gt;|element|&lt;/code&gt; truthy, the element is &lt;em&gt;not&lt;/em&gt; added to the new array.&lt;/p&gt;

&lt;p&gt;In the JavaScript implementation, the &lt;code&gt;filter&lt;/code&gt; function gets another function passed to it. Each element of the array gets passed through the &lt;code&gt;(element)&lt;/code&gt; of this secondary function. If the element makes &lt;code&gt;element % 2 !== 0&lt;/code&gt; truthy, then that element is added to a new array. If the element does not make &lt;code&gt;element % 2 !== 0&lt;/code&gt; truthy, the element is &lt;em&gt;not&lt;/em&gt; added to the new array.&lt;/p&gt;

</description>
      <category>map</category>
      <category>reduce</category>
      <category>ruby</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Syntax Differences: JavaScript vs. Java</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 07 Feb 2021 04:51:57 +0000</pubDate>
      <link>https://dev.to/colerau/syntax-differences-javascript-vs-java-2d0j</link>
      <guid>https://dev.to/colerau/syntax-differences-javascript-vs-java-2d0j</guid>
      <description>&lt;p&gt;&lt;em&gt;In this post I'll explain some syntactical differences between JavaScript and Java.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Declaring and assigning a variable
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JAVASCRIPT
let watermelon = "good";


// JAVA
String watermelon = "good";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, you can declare a variable with &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;var&lt;/code&gt;. You do not need to specify the variable's data type, like you do in Java. In the Java example above, we need to say the &lt;code&gt;watermelon&lt;/code&gt; variable will point to the &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;"good"&lt;/code&gt;.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Printing/logging/outputting something
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JAVASCRIPT
console.log("tomatoes are gross");


// JAVA
System.out.println("tomatoes are gross");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There is just a minor difference between printing in JavaScript vs. Java. Printing the contents of an array works a bit differently in Java compared to JavaScript, which I'll cover later in this post. &lt;/p&gt;

&lt;h1&gt;
  
  
  Variable interpolation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JAVASCRIPT
let fruit = "blueberry";
console.log(`My favorite fruit is the ${fruit}`);


// JAVA
String fruit = "blueberry";
System.out.println(
  String.format("My favorite fruit is the %s", fruit)
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To put a variable in a string: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;In JavaScript&lt;/em&gt;, inside the &lt;code&gt;console.log&lt;/code&gt;, write your string with back ticks; insert &lt;code&gt;${variableName}&lt;/code&gt; where you want your variable to go. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;In Java&lt;/em&gt;, inside the &lt;code&gt;System.out.println&lt;/code&gt;, use &lt;code&gt;String.format()&lt;/code&gt; to write your string; insert &lt;code&gt;%s&lt;/code&gt; where you want your variable to go; after the ending quotation mark of the string, place a comma then write your variable name. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Declaring, invoking, and printing the return value of a function/method
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JAVASCRIPT
const isEven = (num) =&amp;gt; {

  // do something with num and return the result
  return (num % 2 === 0) ? true : false;
}

console.log(isEven(4));




// JAVA
public class Main {
  static boolean isEven(int num) {

    // do something with num and return the result
    return (num % 2 == 0) ? true : false;
  }

  public static void main(String[] args) {
    System.out.println(isEven(4));
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In both languages, we're passing an integer of &lt;code&gt;4&lt;/code&gt; to the &lt;code&gt;isEven&lt;/code&gt; function/method. Then, we determine whether the remainder of the integer divided by 2 is 0. If it is, it's an even number so we return &lt;code&gt;true&lt;/code&gt;; else, the integer is not even so we return &lt;code&gt;false&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;In JavaScript, you can declare and invoke a function with the following general syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// declare a function called functionName&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;functionName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parameter1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;parameter2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// return something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// invoke the function called functionName&lt;/span&gt;
&lt;span class="nx"&gt;functionName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;argument1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;argument2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, in Java, every method needs to be declared within a class (in the above example, the class is &lt;code&gt;Main&lt;/code&gt;). Every parameter to a method needs to have a data type. If the method returns something, you need to specify the return value's data type (&lt;code&gt;void&lt;/code&gt; means nothing is returned). Every method must be invoked within the &lt;code&gt;main&lt;/code&gt; method (The Java compiler will look for a &lt;code&gt;main&lt;/code&gt; method before it executes any other code). &lt;/p&gt;

&lt;p&gt;The following is the general syntax for declaring and invoking a method in Java:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Main&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

  &lt;span class="c1"&gt;// declare a function called functionName&lt;/span&gt;
  &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="n"&gt;returnValueDataType&lt;/span&gt; &lt;span class="nf"&gt;functionName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameterOneDataType&lt;/span&gt; 
  &lt;span class="n"&gt;parameterOne&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;parameterTwoDataType&lt;/span&gt; &lt;span class="n"&gt;parameterTwo&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;...)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// return something &lt;/span&gt;

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


  &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// invoke the function called functionName&lt;/span&gt;
    &lt;span class="n"&gt;functionName&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;argument1&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;argument2&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="o"&gt;...);&lt;/span&gt;
  &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Declaring and printing an array
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// JAVASCRIPT: 
let array = ["strawberry", "orange", "lemon"];
console.log(array);


// JAVA:
public class Main {
  public static void main(String[] args) {
    String[] array = {"strawberry", "orange", "lemon"};
    for (int i = 0; i &amp;lt; array.length; i++) {
      System.out.println(array[i]);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In JavaScript, the elements in an array go inside of square [] brackets. In Java, the elements in an array go inside curly {} braces. Also in Java, you need to specify what data type the elements in the array will be. This is why we write &lt;code&gt;String[]&lt;/code&gt; above. The &lt;code&gt;String&lt;/code&gt; tells Java the array elements will be &lt;code&gt;String&lt;/code&gt;s. The &lt;code&gt;[]&lt;/code&gt; tells Java you're creating an array. &lt;/p&gt;

&lt;p&gt;Since Java doesn't natively print out the contents of an array with &lt;code&gt;System.out.println(arrayName)&lt;/code&gt;, you need to loop through the array, printing each element for each iteration. This is why the above example uses a &lt;code&gt;for&lt;/code&gt; loop to print each element in the array.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>java</category>
      <category>syntax</category>
      <category>differences</category>
    </item>
    <item>
      <title>Checking whether there are any two numbers that will sum up to a given number</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 31 Jan 2021 05:00:28 +0000</pubDate>
      <link>https://dev.to/colerau/checking-whether-there-are-any-two-numbers-that-will-sum-up-to-a-given-number-3gi9</link>
      <guid>https://dev.to/colerau/checking-whether-there-are-any-two-numbers-that-will-sum-up-to-a-given-number-3gi9</guid>
      <description>&lt;p&gt;In this article I'll discuss the solution for a common JavaScript interview algorithm:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// In an unsorted array, check whether 
// there are any two numbers that will 
// sum up to a given number. Example:

// sumFinder([6, 4, 3, 2], 9) = true 
// (because 6 + 3 equals 9.)

// sumFinder([6, 4, 3, 2], 2) = false 
// (because nothing in the array plus 
// another number in the array equals 2.) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  JavaScript Implementation
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sumFinder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;obj&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;diff&lt;/span&gt;

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

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;diff&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;obj&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sumFinderResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sumFinder&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// returns true&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;anotherSumFinderResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;sumFinder&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// returns false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Short Explanation of the Solution
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;If the difference between the function's given target number (the number provided by itself in the arguments list) and an element in the array is equal to another element in the array, return true. Else, return false.&lt;/em&gt;&lt;/strong&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Longer Explanation of the Solution
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;For each element in the array, get the difference between the given number (the number provided by itself in the arguments list) and the current element in the array.&lt;/li&gt;
&lt;li&gt;Put the current element into an object as a key. &lt;/li&gt;
&lt;li&gt;If any element in the array (if any key in our object) matches the difference between the given number and the new current element, return true. &lt;/li&gt;
&lt;li&gt;Else, return false.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  An example of the solution using concrete numbers
&lt;/h1&gt;

&lt;p&gt;Let's say &lt;code&gt;sumFinder&lt;/code&gt; was called with two parameters: an array of [6, 4, 3, 2], and a given number of 7:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;sumFinder&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, according to the above code implementation, an empty object is created, and a diff variable is declared. Then, a &lt;code&gt;for&lt;/code&gt; loop is created that loops through each index of the array. The first index points to &lt;code&gt;6&lt;/code&gt;. The line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;diff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;num&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;now translates to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;diff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="c1"&gt;// diff is now assigned 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since a key of 1 doesn't yet exist in our object, we will insert &lt;code&gt;arr[i]&lt;/code&gt;, or &lt;code&gt;6&lt;/code&gt; as a key in our object. &lt;/p&gt;

&lt;p&gt;Now on the second loop iteration, our &lt;code&gt;num&lt;/code&gt; is &lt;code&gt;7&lt;/code&gt; (the &lt;code&gt;num&lt;/code&gt; never changes). The &lt;code&gt;arr[i]&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt;, and the &lt;code&gt;diff&lt;/code&gt; is therefore &lt;code&gt;3&lt;/code&gt;. Since &lt;code&gt;3&lt;/code&gt; is not yet a key in our object, we will add &lt;code&gt;arr[i]&lt;/code&gt;, or &lt;code&gt;4&lt;/code&gt; as a key in our object. &lt;/p&gt;

&lt;p&gt;On the third loop iteration, our &lt;code&gt;num&lt;/code&gt; is &lt;code&gt;7&lt;/code&gt;, the &lt;code&gt;arr[i]&lt;/code&gt; is &lt;code&gt;3&lt;/code&gt;, and the &lt;code&gt;diff&lt;/code&gt; is therefore &lt;code&gt;4&lt;/code&gt;. This time, &lt;code&gt;4&lt;/code&gt; is a key in our object, so we return &lt;code&gt;true&lt;/code&gt;. We return true because we have successfully figured out that there are two numbers in our array (&lt;code&gt;3&lt;/code&gt; and &lt;code&gt;4&lt;/code&gt;) that will sum up to our given number (&lt;code&gt;7&lt;/code&gt;). &lt;/p&gt;

</description>
      <category>array</category>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Building a simple HTML/JavaScript app from start to finish</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 24 Jan 2021 02:52:37 +0000</pubDate>
      <link>https://dev.to/colerau/building-a-simple-html-javascript-app-from-start-to-finish-12cl</link>
      <guid>https://dev.to/colerau/building-a-simple-html-javascript-app-from-start-to-finish-12cl</guid>
      <description>&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%2F0f2kgb1mwrnwe00hgj0j.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%2F0f2kgb1mwrnwe00hgj0j.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/colerau/simple-word-counter" rel="noopener noreferrer"&gt;Completed Code&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: The following article was written from the perspective of a Visual Studio Code/Mac user.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This article details the process of creating a web app that counts the number of words in a user's input.&lt;/strong&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Step 1: Create an empty folder (directory) in your text editor and navigate to that folder
&lt;/h1&gt;

&lt;p&gt;Your text editor is the piece of software you use to write your code (e.g., Visual Studio Code, Atom, Sublime Text). You can create an empty folder in your text editor via your computer's Terminal. Open the Terminal and, assuming you are in a fresh Terminal session, type in&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;mkdir word-counter&lt;/code&gt; will create a new folder called &lt;code&gt;word-counter&lt;/code&gt;. &lt;code&gt;cd word-counter&lt;/code&gt; will navigate you inside that folder. Now, enter in the command that opens up your text editor. For instance, if you have VSCode, type in&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 2: Assuming you have pulled up an empty folder in your text editor, create your &lt;code&gt;index.html&lt;/code&gt; file
&lt;/h1&gt;

&lt;p&gt;Your &lt;code&gt;index.html&lt;/code&gt; file will contain your HTML code. Create a new file and call it &lt;code&gt;index.html&lt;/code&gt;. Copy and paste the standard HTML boilerplate code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;


    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Between the body tags (between &lt;code&gt;&amp;lt;body&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt;) and above the script tag (above &lt;code&gt;&amp;lt;script src="" async defer&amp;gt;&amp;lt;/script&amp;gt;&lt;/code&gt;), type in the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Word Counter&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will create a header with the text "Word Counter".&lt;br&gt;
Now we need to make a form for our user's input. Here is the code for that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Enter text:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user-input-box"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:650px"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:200px"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Get word count"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code has a label, a text input box, and a submit button. From top to bottom: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;form id="form"&amp;gt;&lt;/code&gt; creates an HTML form and gives the entire form an id of "form". We will later use JavaScript to select this form based on its unique id of "form". This HTML tag needs a closing tag of &lt;code&gt;&amp;lt;/form&amp;gt;&lt;/code&gt;, which we will write when we're done writing the innards of the form. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;label&amp;gt;Enter text:&amp;lt;/label&amp;gt;&lt;/code&gt; gives the form a label of "Enter text:". &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;br /&amp;gt;&lt;/code&gt; denotes a new line. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;input type="text" id="user-input-box" style="width:650px" /&amp;gt;&lt;/code&gt; creates a text input box with an id of "user-input-box" and stretches its width out to a length of 650 pixels.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;input type="submit" style="width:200px" value="Get word count" /&amp;gt;&lt;/code&gt; creates a submit button, stretches it out to 200px in length, and replaces the submit button's text with "Get word count". &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;/form&amp;gt;&lt;/code&gt; closes the form that was opened on the first line (&lt;code&gt;&amp;lt;form id="form"&amp;gt;&lt;/code&gt;). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to display the word count to the user via the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;h3&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"word-count-area"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;The word count is 0.&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;(Notice that the word count is initialized to 0. Our future JavaScript code will change that 0 into the proper word count.)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to update our HTML script tag to include our soon to be created JavaScript file. We do this by locating this line (just above the &lt;code&gt;&amp;lt;/body&amp;gt;&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and adding some text inside the parentheses. Now the line should read:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;THE COMPLETED HTML FILE (index.html) SHOULD LOOK LIKE THIS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;charset=&lt;/span&gt;&lt;span class="s"&gt;"utf-8"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"X-UA-Compatible"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"IE=edge"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"description"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"viewport"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"width=device-width, initial-scale=1"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Word Counter&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"form"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;label&amp;gt;&lt;/span&gt;Enter text:&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"user-input-box"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:650px"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"submit"&lt;/span&gt; &lt;span class="na"&gt;style=&lt;/span&gt;&lt;span class="s"&gt;"width:200px"&lt;/span&gt; &lt;span class="na"&gt;value=&lt;/span&gt;&lt;span class="s"&gt;"Get word count"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;br&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;h3&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"word-count-area"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;The word count is 0.&lt;span class="nt"&gt;&amp;lt;/h3&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"index.js"&lt;/span&gt; &lt;span class="na"&gt;async&lt;/span&gt; &lt;span class="na"&gt;defer&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 3: Uh oh, looks like it's JavaScript time
&lt;/h1&gt;

&lt;p&gt;In your text editor, create a new file called &lt;code&gt;index.js&lt;/code&gt;. This file will be where our JavaScript code lives. &lt;/p&gt;

&lt;p&gt;First, we need to select the appropriate HTML elements with JavaScript. We will begin by selecting our HTML form with this line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;More exactly, this JavaScript code selects the HTML code with the id of "form".&lt;br&gt;
Now we need to select the user's input box and the area in which we will update the word count. This is accomplished with the following two lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userInputBox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user-input-box&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wordCountArea&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;word-count-area&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to add an event listener to our form. Whenever the form's submit button is clicked, we want the code within the event listener function to fire. The foundation of the event listener code is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// When the form's submit button is clicked, the code in &lt;/span&gt;
  &lt;span class="c1"&gt;// this area will run. &lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to fill in the area where the code will run with some JavaScript. Inside the event listener function: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We'll first prevent the page from refreshing upon submit with this line: &lt;code&gt;event.preventDefault();&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We'll get the user's input with this line: &lt;code&gt;let userInput = userInputBox.value.trim();&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We'll split the user's input string into an array where each word is a separate element in the array with this line: &lt;code&gt;let array = userInput.split(" ");&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;We'll get the number of elements in the array with this line: &lt;code&gt;let count = array.length;&lt;/code&gt;. This will be our final word count answer. &lt;/li&gt;
&lt;li&gt;We'll update the HTML file with the word count by writing this code:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;wordCountArea&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`The word count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;THE COMPLETED JAVASCRIPT FILE (index.js) SHOULD LOOK LIKE THIS:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;form&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userInputBox&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;user-input-box&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wordCountArea&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;word-count-area&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;submit&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;userInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userInputBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;userInput&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;wordCountArea&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`The word count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;.`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Step 4: Test the app
&lt;/h1&gt;

&lt;p&gt;Open up your app in a new internet browser tab by typing into your text editor's terminal &lt;code&gt;open index.html&lt;/code&gt;. See if your app works by entering in a piece of text, clicking the submit button ("Get word count") and verifying that the word count the app displays is correct. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/colerau/simple-word-counter" rel="noopener noreferrer"&gt;Completed Code&lt;/a&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>html</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Intro to Google Chrome DevTools</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 17 Jan 2021 02:26:16 +0000</pubDate>
      <link>https://dev.to/colerau/intro-to-google-chrome-devtools-3k9j</link>
      <guid>https://dev.to/colerau/intro-to-google-chrome-devtools-3k9j</guid>
      <description>&lt;p&gt;&lt;em&gt;This article covers the Console, Sources, and Elements tab, and provides instructions for inserting breakpoints to reliably halt your code's execution at a certain line.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Google Chrome DevTools can help with debugging by letting you:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;insert breakpoints into your code, which let you determine the value of variables at a certain point in your code's execution flow&lt;/li&gt;
&lt;li&gt;alter your app's DOM, or the DOM of any Google Chrome web page&lt;/li&gt;
&lt;li&gt;let you execute JavaScript code and see the results of a console.log&lt;/li&gt;
&lt;li&gt;display the innards of a resolved promise from a fetch request&lt;/li&gt;
&lt;li&gt;warn you of problems with your code&lt;/li&gt;
&lt;li&gt;make you the cool hacker kid in middle school&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Opening Chrome DevTools
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to a Chrome web page&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;On Mac&lt;/em&gt;, press Command + Option (alt) + J. &lt;em&gt;On Windows&lt;/em&gt;, press Control + Shift + J. &lt;/li&gt;
&lt;li&gt;
&lt;em&gt;(optional)&lt;/em&gt; You can double click anywhere on a page and select Inspect.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  The Console Tab
&lt;/h1&gt;

&lt;p&gt;This is where you can execute JavaScript code for experimentation purposes or to select and alter a DOM element. Additionally, the result of running console.log will live here.&lt;/p&gt;

&lt;p&gt;If too many error messages and warnings clutter up this tab, click the circle with a diagonal line button on the top-left corner to clear the console.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Sources Tab
&lt;/h1&gt;

&lt;p&gt;Here you can view the contents of your local files and insert breakpoints at specific lines.&lt;/p&gt;

&lt;h1&gt;
  
  
  The Elements Tab
&lt;/h1&gt;

&lt;p&gt;In this tab the HTML skeleton of the web page is revealed. Click the button on the very top-left corner (the square with an eclipsing mouse cursor) to select an HTML element on your web page. Once selected, the element will be highlighted in the HTML skeleton. To manipulate the DOM, alter some HTML in the skeleton and see the resulting change in the DOM upon pressing Enter.&lt;/p&gt;

&lt;h1&gt;
  
  
  Inserting A Breakpoint Into Your Code
&lt;/h1&gt;

&lt;p&gt;Breakpoints work just like JavaScript's debugger or Ruby's binding.pry or byebug. However, if you find that your favorite debugging tool is not working, you can count on breakpoints to help you out.&lt;/p&gt;

&lt;p&gt;Let's say you navigated to localhost:3000/ on Google Chrome and have opened up the Sources tab in the Chrome DevTools. To insert a breakpoint:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;1 - Navigate to the desired file&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the left window of the Sources tab, you will see a list of folders. The localhost:3000/ folder should already be opened and listing its contents. Your current project's files will likely be listed under "Users/{your name}/Projects/{your project's name} in the "localhost:3000/" folder. Follow the tree of folders and files until you get to the file into which you wish to insert a breakpoint.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2 - Insert a breakpoint&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In the middle window of the Sources tab, you will see the code from the file selected in the left window of the Sources tab. The code will look familiar as it is the same code you wrote in your local text editor. Click on the line number of the code at which you want to stop the execution flow. For instance, if you want to stop execution at line 36, click on the number 36; a blue rectangle with a pointed right edge should highlight the number you clicked on.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;3 - Do something to activate the line of code you just selected&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This could be as simple as refreshing the page, or pressing refresh and then clicking on an element in your DOM. In any case, you must first refresh the page.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;4 - The execution flow has stopped&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should see a faint yellow box appear on the page that says "Paused in debugger". You can click on the blue "play" icon to resume execution flow.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;5 - See the value of your variables&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;With the execution stopped, you can navigate to the Console tab of the DevTools and type in a variable name to get its current value.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=9jC1EA4jDc0"&gt;This video demonstrates how to add a breakpoint using Chrome DevTools&lt;/a&gt;&lt;/p&gt;

</description>
      <category>devtools</category>
      <category>beginners</category>
      <category>chrome</category>
      <category>googlechrome</category>
    </item>
    <item>
      <title>How to find the mode (most repeating number) of an array in JavaScript</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sun, 10 Jan 2021 02:16:24 +0000</pubDate>
      <link>https://dev.to/colerau/how-to-find-the-mode-most-repeating-number-of-an-array-in-javascript-78p</link>
      <guid>https://dev.to/colerau/how-to-find-the-mode-most-repeating-number-of-an-array-in-javascript-78p</guid>
      <description>&lt;p&gt;Disclaimer: This post assumes the array input will be composed of only positive numbers and there will only be one mode per input (i.e., there will only be one most repeating number). &lt;/p&gt;

&lt;h1&gt;
  
  
  The findMode Function
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;findMode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// This function starts by creating an object where the keys are each unique number of the array and the values are the amount of times that number appears in the array.&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// increment existing key's value&lt;/span&gt;
      &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// make a new key and set its value to 1&lt;/span&gt;
      &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// assign a value guaranteed to be smaller than any number in the array&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;biggestValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;biggestValuesKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

  &lt;span class="c1"&gt;// finding the biggest value and its corresponding key&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;biggestValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;biggestValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;
      &lt;span class="nx"&gt;biggestValuesKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;biggestValuesKey&lt;/span&gt;

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

&lt;/div&gt;



&lt;h1&gt;
  
  
  Function Breakdown
&lt;/h1&gt;

&lt;p&gt;At a high-level, the function:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;takes in an array &lt;/li&gt;
&lt;li&gt;makes an object where the keys are each unique number of the array and the values are the amount of times that number appears in the array &lt;/li&gt;
&lt;li&gt;finds the key that points to the biggest value&lt;/li&gt;
&lt;li&gt;returns that key. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Analyzing the findMode function from top to bottom, first we create an empty object and assign it to the "object" variable. Then, to fill up our object, we create a &lt;code&gt;for&lt;/code&gt; loop that goes through each member of the array. We want our object to end up looking something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each key of the object is a unique number in the array and the key's value is the amount of times that number appears in the array. So in the above example object, the mode would be 2, because it appears the most in the array (7 times). &lt;/p&gt;

&lt;p&gt;To get our object to look like that, we introduce an &lt;code&gt;if...else&lt;/code&gt; block. For each element in the array input, if the element is already a key in the object, we increment that key's value by one. If the element is not already in the object, we make that element a new key and set its value to one. &lt;/p&gt;

&lt;p&gt;This is all taken care of in the following code from the findMode function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// increment existing key's value&lt;/span&gt;
      &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// make a new key and set its value to 1&lt;/span&gt;
      &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we declare two new variables, &lt;code&gt;biggestValue&lt;/code&gt; and &lt;code&gt;biggestValuesKey&lt;/code&gt;, and assign them both to -1. It doesn't matter which negative number you assign to these variables, it just has to be the guaranteed smallest number in the array. &lt;/p&gt;

&lt;p&gt;Now we need to find the biggest value of our object and return that value's key (the mode). To do that, we receive a new array of our object's keys with Object.keys, passing in our object to the &lt;code&gt;keys&lt;/code&gt; method. Then, we iterate through that new array with a &lt;code&gt;.forEach&lt;/code&gt; enumerator. Next, we get a value from our object with &lt;code&gt;object[key]&lt;/code&gt; and assign it to the new &lt;code&gt;value&lt;/code&gt; variable. If that value is greater than the &lt;code&gt;biggestValue&lt;/code&gt; variable, our new &lt;code&gt;biggestValue&lt;/code&gt; is set to &lt;code&gt;value&lt;/code&gt; and our new &lt;code&gt;biggestValuesKey&lt;/code&gt; is set to &lt;code&gt;key&lt;/code&gt;. The code goes through those steps for each key in the array returned from &lt;code&gt;Object.keys(object)&lt;/code&gt;. Finally, we return &lt;code&gt;biggestValuesKey&lt;/code&gt;, which is the mode. &lt;/p&gt;

&lt;p&gt;Thank you for reading. Please let me know if you have any questions, if there is a better way to find the mode or if I made a mistake somewhere in this post. &lt;/p&gt;

</description>
      <category>mode</category>
      <category>javascript</category>
      <category>array</category>
      <category>repeating</category>
    </item>
    <item>
      <title>How Redux's mapDispatchToProps Works</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Sat, 31 Oct 2020 10:29:49 +0000</pubDate>
      <link>https://dev.to/colerau/how-redux-s-mapdispatchtoprops-works-3aal</link>
      <guid>https://dev.to/colerau/how-redux-s-mapdispatchtoprops-works-3aal</guid>
      <description>&lt;h1&gt;
  
  
  Overview
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;&lt;code&gt;mapDispatchToProps&lt;/code&gt; gives your props indirect access to Redux's &lt;code&gt;dispatch&lt;/code&gt; function. &lt;code&gt;dispatch&lt;/code&gt; passes an action (a JavaScript object) to your reducer so your application’s state can update.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The props that get access to &lt;code&gt;dispatch&lt;/code&gt; belong to the file that is written in the Redux &lt;code&gt;connect&lt;/code&gt; function's second set of parentheses. For example, in &lt;code&gt;App.js&lt;/code&gt;, if you write the line&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;export default connect(null, mapDispatchToProps)(App);&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;only the props for &lt;code&gt;App.js&lt;/code&gt; will receive indirect access to the dispatch function. &lt;/p&gt;

&lt;h1&gt;
  
  
  How to Use &lt;code&gt;mapDispatchToProps&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;(The following assumes you are building a React app with a Redux store already configured).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's say we are making a React app that will allow us to write reviews for something. To add a new review object to our Redux store, we will do the following in the class component &lt;code&gt;ReviewsContainer.js&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Make sure &lt;code&gt;react-redux&lt;/code&gt; is listed in your &lt;code&gt;package.json&lt;/code&gt; file. Also, in the terminal inside your project's directory, make sure you have run &lt;code&gt;npm install&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At the top of the file, add the line &lt;code&gt;import { connect } from 'react-redux';&lt;/code&gt;. This will grant your file access to Redux's &lt;code&gt;connect&lt;/code&gt; function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;At the bottom of the file, add the line &lt;code&gt;export default connect(null, mapDispatchToProps)(ReviewsContainer);&lt;/code&gt;. This lets our file use &lt;code&gt;mapDispatchToProps&lt;/code&gt;. The &lt;code&gt;null&lt;/code&gt; can be replaced with &lt;code&gt;mapStateToProps&lt;/code&gt;. The &lt;code&gt;ReviewsContainer&lt;/code&gt; part is the name of the file we're working in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Write your &lt;code&gt;mapDispatchToProps&lt;/code&gt; function. This function goes outside of the class. &lt;strong&gt;The function returns an object with keys that we'll use later via props.&lt;/strong&gt; If our goal is to add a review to our Redux store, we could write:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;addReview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; 
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ADD_REVIEW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The syntax is convoluted but can be understood like this: &lt;/p&gt;

&lt;p&gt;The function takes in an argument of &lt;code&gt;dispatch&lt;/code&gt; and returns an object with a key of &lt;code&gt;addReview&lt;/code&gt;. (&lt;code&gt;dispatch&lt;/code&gt; is provided by Redux's &lt;code&gt;connect&lt;/code&gt; function). &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;addReview&lt;/code&gt; key points to a value which is an anonymous function. The anonymous function returns an expression that uses the &lt;code&gt;dispatch&lt;/code&gt; function to send an action to the reducer. The action is this part: &lt;code&gt;{ type: "ADD_REVIEW", payload: reviewObject }&lt;/code&gt;. Your reducer will expect an &lt;code&gt;action.type&lt;/code&gt; of &lt;code&gt;'ADD_REVIEWS'&lt;/code&gt; and an &lt;code&gt;action.payload&lt;/code&gt; of &lt;code&gt;reviewObject&lt;/code&gt;. The word "payload" just means the data, the meat and potatoes, that is given to the reducer.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;To actually add a review, we could call &lt;code&gt;this.props.addReview(reviewObject)&lt;/code&gt; somewhere in our &lt;code&gt;ReviewsContainer&lt;/code&gt; class. This will invoke the following anonymous function:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;reviewObject =&amp;gt; (&lt;br&gt;
    dispatch({ type: "ADD_REVIEW", payload: reviewObject })&lt;br&gt;
)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;(This function is the value of the &lt;code&gt;addReview&lt;/code&gt; key of our &lt;code&gt;mapDispatchToProps&lt;/code&gt; function).&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;We can give that function a review object, which can be retrieved by using action.payload in the reducer.&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;(optional)&lt;/em&gt; If you want to add another key-value pair in your &lt;code&gt;mapDispatchToProps&lt;/code&gt; function, you could write the following, which will create a deleteReview action you can send to your reducer:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;addReview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ADD_REVIEW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;

  &lt;span class="na"&gt;deleteReview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DELETE_REVIEW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;mapDispatchToProps&lt;/code&gt; is a function that one writes to dispatch actions to the reducer. &lt;code&gt;mapDispatchToProps&lt;/code&gt; itself returns an object with any number of key-value pairs. Each key points to an anonymous function declaration that will dispatch an action object to the reducer. To invoke the anonymous function, you can call &lt;code&gt;this.props.&amp;lt;keyOfAnonymousFunction&amp;gt;()&lt;/code&gt; somewhere in your class.  &lt;/p&gt;

&lt;p&gt;In the end, &lt;code&gt;ReviewsContainer.js&lt;/code&gt; looks like this:&lt;br&gt;
&lt;/p&gt;

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

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;ReviewsContainer&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;good&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;

        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;reviewObject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deleteReview&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dispatch&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;addReview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ADD_REVIEW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;reviewObject&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;),&lt;/span&gt;

  &lt;span class="na"&gt;deleteReview&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;dispatch&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;DELETE_REVIEW&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;

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

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mapDispatchToProps&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="nx"&gt;ReviewsContainer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>mapdispatchtoprops</category>
      <category>redux</category>
      <category>react</category>
    </item>
    <item>
      <title>Photo Rover - Flatiron School Final Project</title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Thu, 29 Oct 2020 00:41:48 +0000</pubDate>
      <link>https://dev.to/colerau/photo-rover-flatiron-school-final-project-4d8h</link>
      <guid>https://dev.to/colerau/photo-rover-flatiron-school-final-project-4d8h</guid>
      <description>&lt;h1&gt;
  
  
  App Overview
&lt;/h1&gt;

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

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/colerau?tab=projects"&gt;Source Code&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Photo Rover is a web app that uses NASA's &lt;a href="https://api.nasa.gov/"&gt;Mars Rover Photos API&lt;/a&gt; to receive a daily-updating collection of photos from Mars. The photos are taken by NASA's Curiosity rover. A lot of photos feature Martian geology. The rover is also fond of selfies.&lt;/p&gt;

&lt;p&gt;In Photo Rover, a user can view pictures of Mars by selecting a specific Earth date. If they like a photo, they can save it to their library. A link to their saved photo is persisted in a SQLite database.&lt;/p&gt;

&lt;p&gt;The app uses a React/Redux frontend, Redux Thunk to send asynchronous API calls, and a Ruby on Rails JSON API backend to handle user accounts and their respective actions.&lt;/p&gt;

&lt;h1&gt;
  
  
  App Execution Flow (Initial Photo Retrieval)
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;In the React frontend, we start in &lt;code&gt;index.js&lt;/code&gt;, where the Redux store is configured, and &lt;code&gt;App.js&lt;/code&gt; is rendered.&lt;/li&gt;
&lt;li&gt;In &lt;code&gt;App.js&lt;/code&gt;, all the routes are set up with &lt;a href="https://reactrouter.com/"&gt;React Router&lt;/a&gt;. When &lt;code&gt;App.js&lt;/code&gt; mounts, an action is dispatched to receive photos of Mars from yesterday. The action sends a &lt;code&gt;GET&lt;/code&gt; request to NASA's database. The action uses &lt;code&gt;fetch&lt;/code&gt; and waits for a resolved promise to send to the reducer. &lt;/li&gt;
&lt;li&gt;The reducer takes the list of photo objects from the action and stores them in state. Each photo object has its own link to retrieve itself from the Internet. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;App.js&lt;/code&gt; then receives the updated state that contains the photo objects. It renders &lt;code&gt;PhotosList.js&lt;/code&gt; and sends it props containing the photo objects. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PhotosList.js&lt;/code&gt; receives the props and iterates through them, passing each photo object to the &lt;code&gt;Photo.js&lt;/code&gt; component. &lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Photo.js&lt;/code&gt; takes the photo object it received from props and passes the image URL from the object to an HTML &lt;code&gt;img&lt;/code&gt; element. This causes the photo to render on the web DOM. &lt;/li&gt;
&lt;li&gt;The photo is rendered with a button that, upon being clicked by a logged-in user, will cause that photo to be associated with that user. The user can retrieve that photo by going to their Saved Photos library and clicking "Refresh Saved Photos". &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>react</category>
      <category>redux</category>
      <category>javascript</category>
      <category>ruby</category>
    </item>
    <item>
      <title>My Flatiron School JavaScript Project </title>
      <dc:creator>Cole Rau</dc:creator>
      <pubDate>Wed, 16 Sep 2020 22:24:14 +0000</pubDate>
      <link>https://dev.to/colerau/my-flatiron-school-javascript-project-3nkd</link>
      <guid>https://dev.to/colerau/my-flatiron-school-javascript-project-3nkd</guid>
      <description>&lt;h1&gt;
  
  
  &lt;a href="https://github.com/colerau/the-second-hardest-game"&gt;source code&lt;/a&gt; • &lt;a href="https://www.youtube.com/watch?v=WrzEvacPbyU"&gt;demo&lt;/a&gt;
&lt;/h1&gt;

&lt;h1&gt;
  
  
  Project Description
&lt;/h1&gt;

&lt;p&gt;I made an in-browser game using JavaScript, HTML, and CSS (which comprise the frontend); and a Ruby on Rails JSON API (the backend). The frontend uses the backend to alter elements on the DOM. My frontend code utilizes Object-Orientation, and the backend code is organized using REST and the MVC pattern.&lt;/p&gt;

&lt;p&gt;The goal of the game is to move the player (using the keyboard as a controller) to the goal. Along the way, the user must avoid the red squares, which briskly bounce up and down on the screen. The player's position will reset should a collision occur with a red square. &lt;/p&gt;

&lt;p&gt;The game has collision detection between the player object and the enemy objects. Collision detection is also incorporated when the player and enemies hit the game screen edges, and when the player touches the goal.  &lt;/p&gt;

&lt;h1&gt;
  
  
  How the game works
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;An HTML &lt;code&gt;canvas&lt;/code&gt; is made. Its 2d context is selected by JavaScript. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Game&lt;/code&gt; class is created and a new instance is instantiated.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;start&lt;/code&gt; method is called on the &lt;code&gt;Game&lt;/code&gt; instance.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;gameLoop&lt;/code&gt; function is created and called. &lt;code&gt;gameLoop&lt;/code&gt; uses &lt;code&gt;requestAnimationFrame&lt;/code&gt; recursively to clear, update, and draw on the game screen every frame.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Player&lt;/code&gt; class object moves by changing the pixels of its position. To move diagonally, the x and y position are both updated. &lt;/li&gt;
&lt;li&gt;The &lt;code&gt;InputHandler&lt;/code&gt; class listens for a &lt;code&gt;keydown&lt;/code&gt; event, and calls the appropriate player movement function based on the key that was pressed. &lt;code&gt;InputHandler&lt;/code&gt; also listens for a &lt;code&gt;keyup&lt;/code&gt; event, and will call the stop function on the player object when a key is released.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;Enemy&lt;/code&gt; class changes its y-position every frame to move up and down. When it hits the edges of the game screen, its position is multiplied by -1 to cause it to change direction. &lt;/li&gt;
&lt;li&gt;When the player bumps into an enemy, the player's position is set to &lt;code&gt;x = 0&lt;/code&gt; and &lt;code&gt;y = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;When the player makes contact with the goal, an alert to the user is displayed telling them they won the game. The number of levels completed is incremented by 1, and the player's position is reset.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  How signing up and logging in works
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;JavaScript Event listeners wait for a &lt;code&gt;click&lt;/code&gt; of an account form's submit button. &lt;/li&gt;
&lt;li&gt;The username provided by the user is sent to the Ruby on Rails backend via a &lt;code&gt;fetch&lt;/code&gt; request.&lt;/li&gt;
&lt;li&gt;If the user wants to log in, Rails attempts to match the username string with a username in its SQLite database. If the user wants to sign up, Rails will create a user object based on the username it receives. However, if the username is blank or if it already exists, Rails will not create a new user object and the end-user will not be able to sign up. &lt;/li&gt;
&lt;li&gt;In the frontend, the DOM elements for the username and number of levels completed are updated based on the JSON that Rails gives back. The comment feature of the application also uses this asynchronous AJAX technique to render comments and to send a post request to the backend if a logged-in user wants to compose a new comment.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>html</category>
      <category>css</category>
      <category>rails</category>
    </item>
  </channel>
</rss>
