<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Trevor Vardeman</title>
    <description>The latest articles on Forem by Trevor Vardeman (@trevortx).</description>
    <link>https://forem.com/trevortx</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%2F801417%2F5962a0b0-4998-4ac7-9743-09a569304c9d.png</url>
      <title>Forem: Trevor Vardeman</title>
      <link>https://forem.com/trevortx</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/trevortx"/>
    <language>en</language>
    <item>
      <title>React-Archer: Drawing Arrows Between DOM Elements</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Mon, 13 Mar 2023 09:42:59 +0000</pubDate>
      <link>https://forem.com/trevortx/react-archer-drawing-arrows-between-dom-elements-co</link>
      <guid>https://forem.com/trevortx/react-archer-drawing-arrows-between-dom-elements-co</guid>
      <description>&lt;p&gt;For my latest project, I built a trip-tracking app called &lt;a href="https://trip-tracker.onrender.com/" rel="noopener noreferrer"&gt;Trip Tracker&lt;/a&gt;. While coming up with the idea of the app, I used Miro to draw out concepts of what the UI would look like. &lt;/p&gt;

&lt;p&gt;Since I'm allowing users to create trips, the trips themselves would naturally have many cities within them. I wanted a way to graphically show the user where their trip was starting, all the cities they're visiting in order, and where their trips ends. I drew this up with arrows in Miro to show the flow of the trip. &lt;/p&gt;

&lt;p&gt;But how would I code this? Or would I have to insert images to show these arrows? It turns out, there was a nifty little package out there waiting to be installed called &lt;a href="https://github.com/pierpo/react-archer" rel="noopener noreferrer"&gt;react-archer&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;react-archer lets you easily create arrows between DOM elements. Perfect! Let's dive in to how I was able to make this work.&lt;/p&gt;

&lt;p&gt;First, I installed the package.&lt;br&gt;
&lt;code&gt;npm install react-archer --save&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, I needed to import ArcherContainer and ArcherElement:&lt;br&gt;
&lt;code&gt;import { ArcherContainer, ArcherElement } from "react-archer"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;ArcherContainer is what it sounds like -- a container for all of the ArcherElements. ArcherElements are the DOM elements that the arrows will start from and point to. All of your actual DOM elements will be inside of an ArcherElement.&lt;/p&gt;

&lt;p&gt;Let's add an ArcherContainer to our app:&lt;/p&gt;

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

    return (
      &amp;lt;ArcherContainer&amp;gt;
      &amp;lt;/ArcherContainer&amp;gt;
    )


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

&lt;/div&gt;

&lt;p&gt;Let's add two test elements as well. First, you'll need to open and close each ArcherElement, and then you need to give each ArcherElement some content. For the sake of simplicity, let's just use a &lt;code&gt;&amp;lt;p&amp;gt;&lt;/code&gt; tag for each. Here's how it should look now:&lt;/p&gt;

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

    return (
      &amp;lt;ArcherContainer&amp;gt;
        &amp;lt;ArcherElement&amp;gt;
          &amp;lt;p&amp;gt;Test 1&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
        &amp;lt;ArcherElement&amp;gt;
          &amp;lt;p&amp;gt;Test 2&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
      &amp;lt;/ArcherContainer&amp;gt;
    )


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

&lt;/div&gt;

&lt;p&gt;Now it's time to add attributes that will create the lines and arrows for us.&lt;/p&gt;

&lt;p&gt;First, each ArcherElement needs an ID. IDs are used as the destination for an arrow.&lt;/p&gt;

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

    return (
      &amp;lt;ArcherContainer&amp;gt;
        &amp;lt;ArcherElement id="test1"&amp;gt;
          &amp;lt;p&amp;gt;Test 1&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
        &amp;lt;ArcherElement id="test2"&amp;gt;
          &amp;lt;p&amp;gt;Test 2&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
      &amp;lt;/ArcherContainer&amp;gt;
    )


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

&lt;/div&gt;

&lt;p&gt;Next, let's add a "relations" attribute to the first ArcherElement. This is what will create our line and arrow. Here's what the code looks like:&lt;/p&gt;

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

  return (
    &amp;lt;ArcherContainer&amp;gt;
      &amp;lt;ArcherElement
        id="test1"
        relations={[
          {
            targetId: "test2",
            targetAnchor: "top",
            sourceAnchor: "bottom",
            style: { strokeColor: "black", strokeWidth: 1 }
          }
        ]}
      &amp;gt;
        &amp;lt;p&amp;gt;Test 1&amp;lt;/p&amp;gt;
      &amp;lt;/ArcherElement&amp;gt;
      &amp;lt;ArcherElement id="test2"&amp;gt;
        &amp;lt;p&amp;gt;Test 2&amp;lt;/p&amp;gt;
      &amp;lt;/ArcherElement&amp;gt;
    &amp;lt;/ArcherContainer&amp;gt;
  )


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

&lt;/div&gt;

&lt;p&gt;Let's break down some of those attributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;targetId&lt;/strong&gt;: This is the ID of the ArcherElement that the arrow will point to&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;targetAnchor&lt;/strong&gt;: This tells the arrow where on the target ArcherElement to point to&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;sourceAnchor&lt;/strong&gt;: This is the side of the source ArcherElement the line will begin from&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;strokeColor&lt;/strong&gt;: Controls the color of the arrow&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;strokeWidth&lt;/strong&gt;: Controls how wide the arrow is&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Those attributes are highly customizable, and there are many more that are not utilized in this example.&lt;/p&gt;

&lt;p&gt;After centering the code with some CSS, it looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdfs3a6gueo2u2et96pm.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%2Fwdfs3a6gueo2u2et96pm.png" alt="The Test 1 element has an arrow pointing to Test 2 element"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here's the full code from my example:&lt;/p&gt;

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

import Stack from 'react-bootstrap/Stack'
import { ArcherContainer, ArcherElement } from "react-archer"

function ArcherTest() {
  return (
    &amp;lt;Stack className="centered"&amp;gt;
      &amp;lt;ArcherContainer&amp;gt;
        &amp;lt;ArcherElement
          id="test1"
          relations={[
            {
              targetId: "test2",
              targetAnchor: "top",
              sourceAnchor: "bottom",
              style: { strokeColor: "black", strokeWidth: 1 }
            }
          ]}
        &amp;gt;
          &amp;lt;p&amp;gt;Test 1&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
        &amp;lt;ArcherElement id="test2"&amp;gt;
          &amp;lt;p&amp;gt;Test 2&amp;lt;/p&amp;gt;
        &amp;lt;/ArcherElement&amp;gt;
      &amp;lt;/ArcherContainer&amp;gt;
    &amp;lt;/Stack&amp;gt;
  )
}

export default ArcherTest


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

&lt;/div&gt;

&lt;p&gt;For my project, a user can create many cities, and I didn't limit them. They could have just 2, but they could also have 10. Not knowing how many ArcherElements to create, I had to create each ArcherElement by mapping an array of objects and setting each of their IDs/targetIDs dynamically. That result ended up looking like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jrwt7pi6a5dlho94v7u.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%2F4jrwt7pi6a5dlho94v7u.png" alt="Displaying 5 cities with react-archer arrows betweent hem"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please go check out the react-archer documentation on the &lt;a href="https://github.com/pierpo/react-archer" rel="noopener noreferrer"&gt;GitHub page&lt;/a&gt; to see all of the possibilities and get some inspiration on how to use this spectacular package! &lt;/p&gt;

&lt;p&gt;react-archer made my project look so much better and it was extremely easy to use, I'm so happy I found it. &lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Rails - A Hidden Gem: Seed Dump</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Thu, 20 Oct 2022 05:01:37 +0000</pubDate>
      <link>https://forem.com/trevortx/rails-a-hidden-gem-seed-dump-3hpj</link>
      <guid>https://forem.com/trevortx/rails-a-hidden-gem-seed-dump-3hpj</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Phases 3 and 4 at Flatiron School's software engineering program are based on Ruby and Rails, respectively. During the past couple of phases, we've learned a lot about gems and how beneficial they can be. &lt;/p&gt;

&lt;p&gt;A Ruby gem is basically a small program you can insert into the application you're creating in order to make your life easier. For example, installing the BCrypt gem allows you to use incredible password encryption nearly effortlessly. If you were to build this yourself, it would be its own, large program. Since it's a gem, we don't need to reinvent the wheel, we can install the gem and use the program effectively with a few lines of code.&lt;/p&gt;

&lt;p&gt;One of the gems that was covered in class is &lt;a href="https://github.com/faker-ruby/faker"&gt;Faker&lt;/a&gt;. Faker is fantastic because it allows you to create and show off fake records with real-looking data. It's helpful for both practical testing, but also for taking screenshots and/or video for your amazing readme or demo.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seed Dump
&lt;/h2&gt;

&lt;p&gt;I want to highlight another gem I found while working on my phase 4 project, which I had previously never heard of. This gem is called Seed Dump.&lt;/p&gt;

&lt;p&gt;What makes Seed Dump different from Faker is that, instead of using the gem to create fictional data, which has its place, Seed Dump allows you to boost your seed file with some of the data you've already been testing on.&lt;/p&gt;

&lt;p&gt;In my most recent project, I have full CRUD on one object, CRD on two objects, and CR on another. Since there are a lot of interactions that take place between these objects, testing is actually easiest while &lt;em&gt;using&lt;/em&gt; my own app. It's easiest to test while actually creating a new user, a new post, viewing a community, deleting a comment, etc.&lt;/p&gt;

&lt;p&gt;During the testing of this functionality, you're adding data to your database. Rather than resetting your database, what if you utilized the information you entered? What if this is how you could build your seed file, rather than creating it from scratch? Or what if you do some testing and just want to add a couple of live examples to your seed file? Enter &lt;a href="https://github.com/rroblak/seed_dump"&gt;Seed Dump&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Installing Seed Dump is as easy as installing other gems you're used to. Simply add it to your Gemfile by adding:&lt;br&gt;
&lt;code&gt;gem 'seed_dump'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and then running &lt;code&gt;bundle install&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You may also install it on the CLI by typing:&lt;br&gt;
&lt;code&gt;gem install seed_dump&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Utilization
&lt;/h2&gt;

&lt;p&gt;There are a few things you can do with Seed Dump that I'll discuss here. Most notably, you can dump your &lt;em&gt;entire&lt;/em&gt; current database into the seed file, you can &lt;em&gt;add&lt;/em&gt; the current database entries to your existing seed file, you can &lt;em&gt;dump&lt;/em&gt; the existing database to a separate file, you can &lt;em&gt;exclude&lt;/em&gt; certain tables, and you can &lt;em&gt;limit&lt;/em&gt; the results.&lt;/p&gt;

&lt;p&gt;Let's look at a just a few examples of how easy Seed Dump is to use.&lt;/p&gt;

&lt;h2&gt;
  
  
  Dumping the Current DB to Seed File
&lt;/h2&gt;

&lt;p&gt;To use the following commands, ensure your server is currently shut down.&lt;/p&gt;

&lt;p&gt;If you simply want to dump the entire existing database directly into your db/seeds.rb file, you can run the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rake db:seed:dump&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; If your seed file already has data in it, it will be overwritten. Only use this option if your db/seeds.rb file is completely empty.&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the Current DB to Seed File
&lt;/h2&gt;

&lt;p&gt;This is my favorite use case for Seed Dump. Generally speaking, when I start building, I'll have to create a few custom entries in db/seeds.rb just to get started. However, after setting up a few models, I'm ready to do testing on a larger scale, and the new database entries come fast. This is where adding more entries to your seed file becomes helpful.&lt;/p&gt;

&lt;p&gt;If you've already created the entries, and you know you'll need to reset the database for x or y reason(s), why not incorporate the current test database you've created directly to your seed file? I found this incredibly useful. In order to do so, you simply run:&lt;br&gt;
&lt;code&gt;rake db:seed:dump APPEND=true&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Seed Dump will create objects alphabetically, so if you have objects that use foreign keys, you may need to rearrange the order in which objects are being created.&lt;/p&gt;

&lt;h2&gt;
  
  
  Saving Current DB to Separate File
&lt;/h2&gt;

&lt;p&gt;If you'd prefer to save the database you've been testing to a separate seed file instead of db/seeds.rb, there's a short two step process for you to follow. &lt;/p&gt;

&lt;p&gt;First, you need to create a new file that you want to save the existing database to. For example, if I wanted to save my database to create db/second-db.rb, I just need to create the second-db.rb file within the db folder. After that, I just need to run the following command to dumb the entire existing database into that file:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rake db:seed:dump FILE=db/second-db.rb&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Voila, the current database has been saved into a separate file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;When I was working on this project, I came across this gem naturally. I had the feeling that some other coder out there in the world had the same idea as I had -- to easily create a seed file based on the existing database. Sure enough, after a quick Google search, I found Seed Dump. I immediately installed and utilized it myself. &lt;/p&gt;

&lt;p&gt;There are multiple ways to create a good seed file, including both Faker and Seed Dump. For me, Seed Dump was too easy &lt;em&gt;not&lt;/em&gt; to use, and worked so well for my latest project. Now that I'm about to be working on my capstone project, I can guarantee you that Seed Dump will be installed early on to help me with my testing. I hope it helps you too.&lt;/p&gt;

&lt;p&gt;Happy coding.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>gem</category>
      <category>webdev</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Intro to Regular Expressions</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Mon, 08 Aug 2022 06:24:00 +0000</pubDate>
      <link>https://forem.com/trevortx/intro-to-regular-expressions-1i68</link>
      <guid>https://forem.com/trevortx/intro-to-regular-expressions-1i68</guid>
      <description>&lt;h2&gt;
  
  
  What are Regular Expressions?
&lt;/h2&gt;

&lt;p&gt;Regular Expressions, or RegEx for short, are expressions written to find specific patterns. They can be used for things such as validation (think email address validation) or for searching for specific patterns in text.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why RegEx?
&lt;/h2&gt;

&lt;p&gt;When I was going through phase 3, learning Ruby at Flatiron School, we had a very small section based on RegEx. At first, it was really hard to wrap my mind around. The syntax, of course, is not near as clean as reading something like Ruby. It was something I felt like I could spend a week on. However, there are a lot of use cases for RegEx, and it can be a powerful tool when wielded wisely.&lt;/p&gt;

&lt;p&gt;Interestingly, shortly after I read about RegEx for the first time at Flatiron, it actually came in handy at work. The first example I saw of it being utilized was when watching one of our engineers search for something specific in a large block of code. He used RegEx to find exactly what he was looking for, and he found it quickly.&lt;/p&gt;

&lt;p&gt;The second example was actually something I was able to use personally. I work in Sales Operations, and we recently acquired a piece of software that we can use to help de-dupe our Salesforce instance. In order to do so, I had to set up the rules that would be used in order to match two Leads as being duplicates.&lt;/p&gt;

&lt;p&gt;The problem I was running into was that we wouldn't always collect clean data on our Leads. For example, if someone was to go through our Lead-capturing flow on our website, we ask them for a lot of data in a step-by-step manner, but we don't ask for their name or company name up front. We gather the important data first, such as data that qualifies them to do business with us and their email address.&lt;/p&gt;

&lt;p&gt;The result is that a lot of Leads come in with a first name, last name, and/or company name of "unknown." When trying to deduplicate these Leads, we'd like to be able to fuzzy match first names, last names, and company names, but we can't effectively do that if a good chunk of our database has "unknown" in those fields.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Enter RegEx.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In our deduplication tool, we have the option of using RegEx as a field in our matching rules which are used to match two Leads as duplicates. We don't have the option to simply exclude specific strings in the first name, last name, or company name fields, but we &lt;em&gt;do&lt;/em&gt; have the ability to use a regular expression. With RegEx, I can very easily weed out any first names, last names, or company names that include "unknown" or "test." The code I used to do this is as follows:&lt;br&gt;
&lt;code&gt;/\b(?!\bunknown|Unknown|test|Test\b)\w+\b/&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What this does is actually removes any Leads from being matched as duplicates that contain the words "unknown," "Unknown," "test," and "Test." You can see this in effect by using &lt;a href="https://rubular.com/"&gt;Rubular&lt;/a&gt;: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--qyNG8ChO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0wu3r3hszihgawkwy0d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--qyNG8ChO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p0wu3r3hszihgawkwy0d.png" alt='Picture of Rubular excluding "unknown" and "test"' width="800" height="783"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What you see in this paragraph is that the only words the are &lt;em&gt;excluded&lt;/em&gt; are "unknown," "Unknown," "test," and "Test," everything else is selected. Note that there are instances of "test" and "unknown" that are attached to other words that are &lt;em&gt;not&lt;/em&gt; excluded, which is by design. We only want to remove exact matches.&lt;/p&gt;

&lt;p&gt;Let's break down this code.&lt;/p&gt;

&lt;p&gt;RegEx always starts and ends with slashes, which is why you see those at the beginning and end:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m7A4sz0S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndn6f0420oujdez1j4kl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m7A4sz0S--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ndn6f0420oujdez1j4kl.png" alt="Inner and outer slashes emphasized" width="800" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, the "\b" begins the word boundary. It says that everything that follows is a word of its own, until the final "\b" that ends the boundary. Note that there's a set of inner boundaries inside of this one.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6WGcjioP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n57d4mbjbmscp7zr5csa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6WGcjioP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n57d4mbjbmscp7zr5csa.png" alt="Outer word boundaries emphasized" width="800" height="91"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Next, you have the opening and closing parentheses. These are saying that anything contained within are supposed to be evaluated together.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d2ALmRkh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxrb67ptp322ah0l6m3x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d2ALmRkh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rxrb67ptp322ah0l6m3x.png" alt="Emphasis on parentheses" width="800" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After that, you have the "?!" which is a look-forward statement saying that what follows is &lt;strong&gt;not&lt;/strong&gt; true. It makes the rest of the statement that follows flip from truthy to falsey.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--66SQvqvj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yab98fps9we69f5p7ggu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--66SQvqvj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yab98fps9we69f5p7ggu.png" alt="Emphasis on question mark and exclamation point" width="800" height="99"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next, you have the aforementioned inner word boundary. This is saying that the word that follows is the word (or words, in this case) that we're searching for. It's closed out after all the words in our &lt;strong&gt;or&lt;/strong&gt; statement.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XRzY1L-y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxqslw8yo4ky184plc0e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XRzY1L-y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dxqslw8yo4ky184plc0e.png" alt="Emphasis on inner word boundaries" width="800" height="102"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;After that are the words that we're actually searching. The first is "unknown," but you may notice a familiar operator after that which is the vertical pipe. In RegEx, it means the same thing as it does in a lot of coding languages, it's an "or" statement. So the following line is saying that we're searching for "unknown," "Unknown," "test," &lt;strong&gt;or&lt;/strong&gt; "Test."&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--y4ndoe-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w74fpvgs542l1bqlzoex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--y4ndoe-c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w74fpvgs542l1bqlzoex.png" alt="The words we're searching are emphasized" width="800" height="66"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;The final piece is the "\w+" near the end of the expression. This matches one or more word or numerical characters, such as a-z, A-Z, 0-9, and _.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Wf1ZL4wO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1tw19f05mw8jrocp38h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Wf1ZL4wO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1tw19f05mw8jrocp38h.png" alt="Emphasis on the backslash w plus" width="800" height="101"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;There's an excellent RegEx quick reference guide that can help you navigate RegEx on the &lt;a href="https://rubular.com/"&gt;Rubular&lt;/a&gt; site, here's what it looks like:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--r03UqUPx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udou55g0n3cr4ssqd9vd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--r03UqUPx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/udou55g0n3cr4ssqd9vd.png" alt="Screenshot of RegEx quick reference guide on Rubular" width="800" height="223"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This rather confusing-looking piece of code has a lot of power behind it. There are two things I learned that require a high level of knowledge and precision during phase 3 at Flatiron School. The first is RegEx which we covered a bit of today, and the second is scraping using Nokogiri. More on that second topic another day. I hope you enjoyed this small tutorial about RegEx!&lt;/p&gt;

</description>
      <category>regex</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>[React] Passing State to a Sister Component</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Thu, 09 Jun 2022 06:14:03 +0000</pubDate>
      <link>https://forem.com/trevortx/react-passing-state-to-a-sister-component-570d</link>
      <guid>https://forem.com/trevortx/react-passing-state-to-a-sister-component-570d</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;There are times in React when you'll need to use state between two sister components. You cannot directly pass state between those two sister components, so how can you use state between them? The answer is you need to host the state on the shared parent component. I'll illustrate this while using a controlled form!&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started
&lt;/h2&gt;

&lt;p&gt;First thing's first, you'll need to set up a React app. This is easiest by creating a new directory in your terminal, then by using these commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-react-app my-app
cd my-app
npm start
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Open the code in your code editor of choice. You can go ahead and open the App component in the "src" folder, and remove all the code inside the &lt;code&gt;return()&lt;/code&gt; except for the top level div, so it looks like this:&lt;br&gt;
&lt;/p&gt;

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

function App() {
return (
    &amp;lt;div className="App"&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Adding Components
&lt;/h2&gt;

&lt;p&gt;Let's go ahead and create a couple of sister components. Create two new files called "Form.js" and "Greeting.js." If you're using Visual Studio Code, in each of them, use the keyboard shortcut "rfce" which creates some boilerplate code in the format of a React functional export component.&lt;/p&gt;

&lt;p&gt;Those two components should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Form({ updateName }) {
  return (
    &amp;lt;div&amp;gt;test&amp;lt;/div&amp;gt;
  )
}

export default Form
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Greeting({ name }) {
  return (
    &amp;lt;div&amp;gt;test&amp;lt;/div&amp;gt;
  )
}

export default Greeting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Small note that I removed the &lt;code&gt;import React from 'react'&lt;/code&gt; line from these components as it's not necessary on components other than App.)&lt;/p&gt;

&lt;p&gt;Now, let's go back to our App component and import the two sister components and add them to our page. After doing so, your App component will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css'
import Form from './Form'
import Greeting from './Greeting'

function App() {
return (
    &amp;lt;div className="App"&amp;gt;
      &amp;lt;Form /&amp;gt;
      &amp;lt;Greeting /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have both components appearing on the DOM, let's focus on one at a time. &lt;/p&gt;

&lt;h2&gt;
  
  
  Form.js
&lt;/h2&gt;

&lt;p&gt;Let's create a controlled form in which we ask a user to type in their name so that we can use their name in our Greeting component in order to say hi to them!&lt;/p&gt;

&lt;p&gt;Within the div, we can create a simple form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;form&amp;gt;
        &amp;lt;label&amp;gt;Enter your name: &amp;lt;/label&amp;gt;
        &amp;lt;input type="text"&amp;gt;&amp;lt;/input&amp;gt;
      &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So now, on the DOM, we can see a simple form in which a user can type. But how do we store the value of whatever the user is typing as it changes? As I mentioned previously, we want this to be a controlled form. &lt;/p&gt;

&lt;p&gt;In order for the form to be controlled, the value of the form needs to be equal to the value we're storing in state. Basically, as the user types, state will update along with what they're typing, and the value of the input itself is the state variable, &lt;em&gt;not technically what the user has typed&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Since we know we want to pass this state the Form's sister component, Greeting, we need to store state in their collective parent component, App.&lt;/p&gt;

&lt;h2&gt;
  
  
  App.js
&lt;/h2&gt;

&lt;p&gt;Back in our App component, let's import state by adding this line at the top:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;import { useState } from 'react'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, inside the functional component, let's create our state variable:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const [name, setName] = useState("")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we need to set up a function in order to set the state. We're going to pass this function, along with the variable we declared, down to our Form component as props. Every time the user types something in the form, we'll call the function (onChange). The function will simply set state to be equal to the change event's &lt;code&gt;target.value&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  function updateName(input) {
    setName(input.target.value)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, pass the function as a prop to the Form component along with the variable itself:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Form name={name} updateName={updateName} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;While we're here, let's go ahead and pass down our &lt;code&gt;name&lt;/code&gt; variable to our Greeting component since we're know we're going to make use of it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;Greeting name={name} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Back in the Form component, make sure to accept the props:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function Form({ name, updateName }) {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, add the onChange event and set the value to be equal to our variable. These are the final steps to ensure this is a controlled form:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input type="text" value={name} onChange={updateName}&amp;gt;&amp;lt;/input&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The final Form component should look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Form({ name, updateName }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;form&amp;gt;
        &amp;lt;label&amp;gt;Enter your name: &amp;lt;/label&amp;gt;
        &amp;lt;input type="text" value={name} onChange={updateName}&amp;gt;&amp;lt;/input&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Form
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We're now done with both our Form and App components!&lt;/p&gt;

&lt;h2&gt;
  
  
  Greeting.js
&lt;/h2&gt;

&lt;p&gt;Now, we have our Form component all set up, and with every keystroke, the &lt;code&gt;name&lt;/code&gt; state field is being saved in state on our App component. Let's put that on the DOM so we can greet our user! We already passed down the &lt;code&gt;name&lt;/code&gt; field as a prop to our Greeting component, so we need to make sure to accept it as a prop:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;function Greeting({ name }) {&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, let's just add a paragraph with a short greeting with our user's name! The component should now look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Greeting({ name }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;p&amp;gt;{`Hello, ${name}!`}&amp;lt;/p&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

export default Greeting
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great, we've got the greeting on the DOM! You may notice, however, that before the user types anything, the greeting reads, "Hello, !" This is obviously less than ideal, so let's add a ternary operator to clean this up:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;{name ? &amp;lt;p&amp;gt;{&lt;/code&gt;Hello, ${name}!&lt;code&gt;}&amp;lt;/p&amp;gt; : &amp;lt;p&amp;gt;Hello!&amp;lt;/p&amp;gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, when the &lt;code&gt;name&lt;/code&gt; field is truthy, or, has a value, it will show the code you see between the question mark and the colon, which will include the user's name. When &lt;code&gt;name&lt;/code&gt; is falsey, or has no value, we'll just show the user, "Hello!"&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Nice and tidy. Here's what the final product should look like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XqU2C7uw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcv5bouc0b65kc503bz7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XqU2C7uw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dcv5bouc0b65kc503bz7.gif" alt="Working App!" width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So in this post, we actually learned how to send state to a sister component, how to create a controlled form, and how to use the ternary operator. I hope you can use some of these in your future projects! Thanks for reading. &lt;/p&gt;

&lt;p&gt;-Trevor&lt;/p&gt;

</description>
      <category>react</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Creating a Simple Counter Using HTML/JavaScript</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Fri, 15 Apr 2022 05:42:24 +0000</pubDate>
      <link>https://forem.com/trevortx/creating-a-simple-counter-57hf</link>
      <guid>https://forem.com/trevortx/creating-a-simple-counter-57hf</guid>
      <description>&lt;h3&gt;
  
  
  The Intro
&lt;/h3&gt;

&lt;p&gt;I recently completed my first project at Flatiron School, which I'm very proud of! Feel free to &lt;a href="https://dev.to/trevortx/first-project-worldwide-weather-4dno"&gt;read more about there here&lt;/a&gt;. During the review of my project, I was given a live coding task. The task was simple: to add a counter on the DOM that would add 1 to a number when a button was clicked.&lt;/p&gt;

&lt;p&gt;Sounds easy, right? Especially after having completed a fairly large project. Well, unfortunately, it gave me a litle trouble. I knew what pieces of the solution were fairly quickly, but putting them together on-the-spot proved challenging.&lt;/p&gt;

&lt;p&gt;After feeling just a little down with how I performed on this exercise, I realized how important it was to have been given such a task. While the task itself was simple, there was more to the challenge than meets the eye.&lt;/p&gt;

&lt;p&gt;It was the first time I had to really explain my thought process out loud to someone that I didn't know, and it was also the first time I had to perform a coding task in front of someone I had never met before.&lt;/p&gt;

&lt;p&gt;After graduating from Flatiron, I'm certainly going to have to code in front of people while explaining my thought process - it's going to happen in every programming interview. This was really my first foray into what that would potentially feel like, even though this task was far simpler than any job interview. &lt;/p&gt;

&lt;p&gt;It was great practice, and practice makes perfect.&lt;/p&gt;

&lt;p&gt;So, today, let's talk about the task I was given and how to solve it. Hopefully there's someone out there who will stumble across this post in a time of need!&lt;/p&gt;

&lt;h3&gt;
  
  
  The Task
&lt;/h3&gt;

&lt;p&gt;Here were the deliverables:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add a button to the web page&lt;/li&gt;
&lt;li&gt;Add a counter on the page&lt;/li&gt;
&lt;li&gt;When the button is clicked, add 1 to the counter&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I was allowed to hard-code the number 0 to begin the exercise.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Pieces
&lt;/h3&gt;

&lt;p&gt;So, how can we break this down in terms of steps we need to take to solve the problem? Here is how I broke it down:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to create the button in HTML&lt;/li&gt;
&lt;li&gt;We also need to create a place for the counter in HTML&lt;/li&gt;
&lt;li&gt;We'll add an event listener to connect the JavaScript we're going to write to the HTML&lt;/li&gt;
&lt;li&gt;We will need a JS click event to trigger the counter&lt;/li&gt;
&lt;li&gt;When clicked, we'll update the counter&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The HTML
&lt;/h3&gt;

&lt;p&gt;In our index.html file, we'll start by doing the following, which is far more detail regarding the first 2 items above.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Within our HTML's body, create a div to house everything&lt;/li&gt;
&lt;li&gt;Create a button&lt;/li&gt;
&lt;li&gt;Write some text to describe our number&lt;/li&gt;
&lt;li&gt;Create a span with an ID of "counter" so that we can store it as a variable in our index.js file&lt;/li&gt;
&lt;li&gt;Include our starting number of 0&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; I'm not covering it here for simplicity, but in your HTML file, don't forget your boilerplate HTML and to point to your JavaScript file!&lt;/p&gt;

&lt;p&gt;Here's what the HTML steps above look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    &amp;lt;div&amp;gt;
      &amp;lt;button type="button"&amp;gt;Click Me!&amp;lt;/button&amp;gt;
      Number of Clicks: &amp;lt;span id="counter"&amp;gt;0&amp;lt;/span&amp;gt;
    &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The JavaScript
&lt;/h3&gt;

&lt;p&gt;Next we need to add the JavaScript portion. This consists of steps 2 - 6 in the "The Pieces" section above. In more detail, this is what we'll do:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create a variable for the HTML button&lt;/li&gt;
&lt;li&gt;Create a variable for the HTML counter&lt;/li&gt;
&lt;li&gt;Add an event listener to be able to track when the button is clicked, and then to use a callback function to...&lt;/li&gt;
&lt;li&gt;Add 1 to the existing number&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, we need to store a reference to the button from our HTML in JavaScript. I used &lt;code&gt;querySelector&lt;/code&gt; to find the button by its "type" attribute. Then I added an event listener to my button variable:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const button = document.querySelector("button")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, we need to store the counter as a variable so that we can &lt;em&gt;increment&lt;/em&gt; it when it's clicked. Instead of using &lt;code&gt;querySelector&lt;/code&gt;, let's store this variable by using &lt;code&gt;getElementById&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const counter = document.getElementById("counter")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Next, I added an event listener to that button: &lt;/p&gt;

&lt;p&gt;&lt;code&gt;button.addEventListener("click", () =&amp;gt; clickHandler())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the button is &lt;em&gt;clicked&lt;/em&gt;, I'm calling a callback function titled &lt;code&gt;clickHandler&lt;/code&gt; which will house the code that tells the program what action to take when the button is clicked. This is the part where I got tripped up in the live-coding exercise. I find that very often, I accidentally overcomplicate things. &lt;/p&gt;

&lt;p&gt;The first thing I tried to do was take the number within the counter, using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML"&gt;&lt;code&gt;innerHTML&lt;/code&gt;&lt;/a&gt; property, and add 1 to it, which looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function clickHandler() {
  counter.innerHTML + 1
  console.log(counter)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The problem here is that the number that I'm trying to increment, 0, &lt;em&gt;isn't actually an integer (number) at all&lt;/em&gt;. I found this out by using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof"&gt;&lt;code&gt;typeof&lt;/code&gt;&lt;/a&gt; operator:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log(typeof counter.innerHTML)&lt;/code&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kClOPMVz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r0apkkjxaaxpzo2ja0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kClOPMVz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8r0apkkjxaaxpzo2ja0b.png" alt='Image of console logging the typeof "counter.innerHTML" which returns "string"' width="286" height="56"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Turns out, that the number 0 I had added to HTML was actually a string. So when trying to add 1 to it, it wasn't working because the data types didn't match.&lt;/p&gt;

&lt;p&gt;The solution is more elegant because it solves two problems at once. It will use the classic &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment"&gt;increment operator (++)&lt;/a&gt;, and at the same time, will convert the string of "0" to an integer, alleviating our data inconsistency! The new function will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function clickHandler() {
  counter.innerHTML++
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try it out for yourself!&lt;/p&gt;

&lt;p&gt;While we're here, though, let's look at how we can simplify this just a little bit further. &lt;/p&gt;

&lt;h3&gt;
  
  
  The Refactor
&lt;/h3&gt;

&lt;p&gt;We can refactor this 3 line function into a single-line arrow function:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const clickHandler = () =&amp;gt; counter.innerHTML++&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Excellent! This still works and trims our code while not making it any harder to read. But can we go &lt;em&gt;even further&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Of course we can.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Remember the event listener we created that looked like this?&lt;/p&gt;

&lt;p&gt;&lt;code&gt;button.addEventListener("click", () =&amp;gt; clickHandler())&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Well, since we were able to shorten our function down to one line of code, we can just go ahead and execute the function's action directly within the event listener itself! Here's what it looks like when you edit the event listener to perform this action immediately:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;button.addEventListener("click", () =&amp;gt; counter.innerHTML++)&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Final Product
&lt;/h3&gt;

&lt;p&gt;Now we've successfully created a functional counter button that our users can interact with on the DOM in about 10 lines of code including both HTML and JavaScript (minus the boilerplate)!&lt;/p&gt;

&lt;p&gt;To recap what the final code looks like altogether, see below!&lt;/p&gt;

&lt;p&gt;HTML body:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  &amp;lt;body&amp;gt;
    &amp;lt;div&amp;gt;
      &amp;lt;button type="button"&amp;gt;Click Me!&amp;lt;/button&amp;gt;
      Number of Clicks: &amp;lt;span id="counter"&amp;gt;0&amp;lt;/span&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;script src="./index.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const button = document.querySelector("button")
const counter = document.getElementById("counter")
button.addEventListener("click", () =&amp;gt; counter.innerHTML++)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Gif Demonstration:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mxedd8qd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjgi764si4ahspku0xtr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mxedd8qd--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qjgi764si4ahspku0xtr.gif" alt="Gif of the working counter. When clicked, the counter increments by 1." width="880" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope you found this post helpful in your coding journey, feel free to reach out if you have any questions or comments. :)&lt;/p&gt;

&lt;p&gt;-Trevor&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>programming</category>
    </item>
    <item>
      <title>My First Project and a Dive Into Dark Mode</title>
      <dc:creator>Trevor Vardeman</dc:creator>
      <pubDate>Tue, 05 Apr 2022 02:40:44 +0000</pubDate>
      <link>https://forem.com/trevortx/first-project-worldwide-weather-4dno</link>
      <guid>https://forem.com/trevortx/first-project-worldwide-weather-4dno</guid>
      <description>&lt;p&gt;Hello, world! My name is Trevor, and I'm a software engineering student at Flatiron School. I just wrapped up my very first project called "Worldwide Weather" and I'm pretty excited about how the project turned out!&lt;/p&gt;

&lt;p&gt;For this project, our requirements were to use HTML, CSS, and JavaScript to create a front end, single page app that also utilizes an external API. Additionally, we needed to incorporate at least three different types of event listeners and allow for some level of interactivity. &lt;/p&gt;

&lt;p&gt;I decided to build a weather app, because I find myself regularly having to search for the forecast since I go outside daily, if only to take my dog for a walk. I live in San Francisco, and while people say the weather here is the same year-round, I find that it's highly dependent on how fast the wind is blowing and from which direction it's blowing from. This makes knowing the forecast a requirement before leaving the apartment to venture into the world.&lt;/p&gt;

&lt;p&gt;I also really like to travel and learn about new places. So in order to combine the two, I wanted to utilize a second API to pull a Wikipedia summary of the location the user searched and add it to the page in addition to the forecast. To further take advantage of this, I added a "random" button that pulls from a list of over 23,000 cities so that the user can learn about a new city, or potentially use the app to suggest a random destination for their next vacation if they're feeling adventurous.&lt;/p&gt;

&lt;p&gt;One of the features I'd like to discuss in depth is the dark mode that I created for the app. Super quick background on that - I love dark mode &lt;em&gt;everything&lt;/em&gt;. If it's an option, I will always select dark mode. On my laptop, on my phone, every time. Back to the app...&lt;/p&gt;

&lt;p&gt;I wanted to use one of my required event listeners on a dark mode button. Specifically, since I used Bootstrap, I added a switch at the top of my page that's really just a checkbox but looks much more slick. I had many elements on the page change when this switch was toggled, but for simplicity's sake, I'll highlight just one of them to demonstrate how I utilized the switch.&lt;/p&gt;

&lt;p&gt;First thing's first, since I'm using Boostrap, here's what the HTML looked like for the switch itself:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      &amp;lt;input class="form-check-input" type="checkbox" role="switch" id="flexSwitchCheckDefault"&amp;gt;
      &amp;lt;label class="form-check-label" for="flexSwitchCheckDefault"&amp;gt;Dark Mode&amp;lt;/label&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the screenshot below, you'll notice that I have a "Random location!" button: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G0tLxhDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86i7rfxalkqdtea201y8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G0tLxhDq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/86i7rfxalkqdtea201y8.png" alt='Screenshot of my app, Worldwide Weather, showing the top of the page including the "Random location!" button' width="880" height="399"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's use that as our example. In HTML, here is what that button looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button id="random-btn" type="button" class="btn btn-dark"&amp;gt;Random location!&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the class above, &lt;code&gt;btn btn-dark&lt;/code&gt;, is what's making the default button appear as black when the page is first loaded.&lt;/p&gt;

&lt;p&gt;Within my JavaScript, I need to do a few things next:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create variables for both the Dark Mode switch and the Random button&lt;/li&gt;
&lt;li&gt;Add an event listener to the Dark Mode switch so that, when the switch is &lt;em&gt;changed&lt;/em&gt;, it will toggle the Random button's class (in accordance with Bootstrap) to make the button turn from its default color, black, to white&lt;/li&gt;
&lt;li&gt;Ensure the functionality above works both ways (as in, the button will go back to black when the switch is toggled a second time and Dark Mode is turned "off")&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I created the variable to store the value of the Dark Mode switch:&lt;br&gt;
&lt;code&gt;const darkSwitch = document.getElementById("flexSwitchCheckDefault")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Then I did the same for the Random button: &lt;br&gt;
&lt;code&gt;const randomBtn = document.getElementById("random-btn")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There are 3 main components to the event listener I added. Here's the code, and then I'll break it down:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;darkSwitch.addEventListener("change", () =&amp;gt; {
  if (darkSwitch.checked) {
    // remove and set attributes for dark mode
    randomBtn.removeAttribute("class")
    randomBtn.setAttribute("class", "btn btn-light")
  } else {
    // set attributes back to light mode
    randomBtn.setAttribute("class", "btn btn-dark")
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first thing I did was add an event listener to the Dark Mode switch so that it detects a &lt;em&gt;change&lt;/em&gt; event. A change event will run a function whenever the element is &lt;em&gt;changed&lt;/em&gt; from its current state - in this case, when the switch is toggled by the user.&lt;/p&gt;

&lt;p&gt;One of the other things I needed to do was to ensure that the button &lt;em&gt;changes back to light mode&lt;/em&gt; when toggled a second time. Within my function, I'll address that with an &lt;code&gt;if&lt;/code&gt; statement.&lt;/p&gt;

&lt;p&gt;The function begins with the &lt;code&gt;if&lt;/code&gt; statement. First, it's checking to see if the switch has been toggled to Dark Mode (in this case, if it's "checked," since, again, my switch is actually a checkbox that's appearing as a switch). &lt;/p&gt;

&lt;p&gt;If it has, I'm actually &lt;em&gt;removing&lt;/em&gt; any classes associated to the &lt;code&gt;randomBtn&lt;/code&gt; using &lt;code&gt;.removeAttribute&lt;/code&gt; and passing in the type of attribute I"m removing ("class"). Then I'll add a new class using &lt;code&gt;.setAttribute&lt;/code&gt; which will change the color of the button from its original dark color to a light color. First I specify that I'm adding a class ("class"), then I'm specifying &lt;em&gt;what&lt;/em&gt; class ("btn btn-light"). Thus changing it from the dark button you see when the page loads to a white button to contrast the now-darkened-page.&lt;/p&gt;

&lt;p&gt;*Note that in this case, I probably don't need to remove the attribute, but it was added because other, similar elements were giving me trouble.&lt;/p&gt;

&lt;p&gt;If the switch is &lt;strong&gt;no longer checked&lt;/strong&gt;, the code will run the &lt;code&gt;else&lt;/code&gt; statement; it'll set the &lt;code&gt;randomBtn&lt;/code&gt; back to the dark switch that you saw when the page first loaded using the same &lt;code&gt;setAttribute&lt;/code&gt; method described above.&lt;/p&gt;

&lt;p&gt;Extrapolate that process to the other elements of the page, and you get a functional dark mode! See the gif below to see dark mode in action, or feel free to visit &lt;a href="https://github.com/trevortx/worldwide-weather"&gt;Worldwide Weather's GitHub page&lt;/a&gt; and download the app and play around with it yourself.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--jpx1CMui--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjcokre3jm36adqct7ei.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jpx1CMui--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjcokre3jm36adqct7ei.gif" alt="Gif of Worldwide Weather functionality, including dark mode" width="240" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Since this app was small, it wasn't too much trouble to build a functioning dark mode by myself. However, there are alternatives! Tailwind CSS, for example, has a dark mode built in. Since I decided to use Bootstrap for this project, I needed to build it myself since there's not night mode included in Bootstrap. So, depending on your needs and the size of your app, you have options on whether dark mode is something you want to tackle by yourself or if you'd prefer to use another framework that has a built-in dark mode option.&lt;/p&gt;

&lt;p&gt;I hope you enjoyed reading about this project and dark mode in particular. Don't hesitate to reach out to me if you have any questions, and thanks so much for reading.&lt;/p&gt;

&lt;p&gt;-Trevor&lt;/p&gt;

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