<?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: Brian Francoeur</title>
    <description>The latest articles on DEV Community by Brian Francoeur (@bfrancoeur).</description>
    <link>https://dev.to/bfrancoeur</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%2F386207%2Fdb188651-68d5-4e3d-89ff-f2f0d2cad4a3.jpeg</url>
      <title>DEV Community: Brian Francoeur</title>
      <link>https://dev.to/bfrancoeur</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bfrancoeur"/>
    <language>en</language>
    <item>
      <title>Javascript30, Lesson 1: Drum Kit Project</title>
      <dc:creator>Brian Francoeur</dc:creator>
      <pubDate>Mon, 18 May 2020 20:53:23 +0000</pubDate>
      <link>https://dev.to/bfrancoeur/javascript30-lesson-1-drum-kit-project-522m</link>
      <guid>https://dev.to/bfrancoeur/javascript30-lesson-1-drum-kit-project-522m</guid>
      <description>&lt;p&gt;The goal for this lesson is to build a &lt;strong&gt;drum kit app&lt;/strong&gt; that runs in the browser. The main concepts presented in the video are &lt;strong&gt;key events&lt;/strong&gt; and &lt;strong&gt;CSS animations&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Code
&lt;/h2&gt;

&lt;p&gt;Writing the HTML and CSS on my own was straightforward. Structurally, the biggest difference between the project’s code and mine was that I used an unordered list for the keys and I didn’t even think to use data attributes. My CSS was similar to the project CSS, although I used different class names. &lt;/p&gt;

&lt;p&gt;All was well until I tried writing the JavaScript. I spent over an hour trying to figure out how to get anything to work. Finally, I swallowed my pride and followed along with the video lesson (note to self -- don’t waste more than &lt;strong&gt;30 minutes&lt;/strong&gt; trying to figure it out on my own). &lt;/p&gt;

&lt;p&gt;To focus on the JavaScript in the lesson, I replaced my HTML and CSS with the project’s code. That way, I wouldn’t spend a lot of time rewriting most of the code with the data-keys and CSS classes used in the lesson's JavaScript. &lt;/p&gt;

&lt;h2&gt;
  
  
  The JavaScript
&lt;/h2&gt;

&lt;p&gt;The main concept of this lesson is &lt;strong&gt;key events&lt;/strong&gt;. I have built a lot of websites, forms, and UIs, but never worked with key events. This was new territory for me and the drum kit was a fun way to learn about them. &lt;br&gt;
Key events begin with using the &lt;code&gt;&amp;lt;kbd&amp;gt;&lt;/code&gt; HTML tag. I had never even heard of that tag before I started this lesson! Within each tag, the &lt;code&gt;data-key attribute&lt;/code&gt; is created and its value is set to the key code value for each key. For example, the letter ‘A’ on the keyboard has a key code of ‘65’, the ‘S’ on the keyboard has a key code of ‘83’, and so on. To make the pressed key play its assigned sound, it’s bound to the &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; tag with the data-key value, like so: &lt;/p&gt;

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

&lt;p&gt;Each audio tag then has its source audio file added and voila -- the HTML for this project is complete. &lt;br&gt;
So, what about the JavaScript? We’re getting to that now. It will make a lot more sense now that we have a clear context for what the JavaScript needs to do, which is: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Listen for a &lt;code&gt;keydown event&lt;/code&gt; to play a drum sound&lt;/li&gt;
&lt;li&gt;Animate the keys on the screen when they are played&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  The ‘keydown’ event
&lt;/h2&gt;

&lt;p&gt;To play the correct sound when a key is pressed, we need an event listener that will listen for a keydown event from the keyboard. Here is the code for that event listener and the function it triggers: &lt;/p&gt;

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

&lt;p&gt;Rather than creating an event listener for every single key in this project, it’s more efficient to create an event listener that listens for any keydown event from the keyboard. The event listener triggers the &lt;code&gt;playSound()&lt;/code&gt; function which plays a drum sound when a key is pressed. &lt;/p&gt;

&lt;p&gt;Before going any further, I need to back up and explain what those &lt;code&gt;const&lt;/code&gt; variables are. The &lt;code&gt;const audio variable&lt;/code&gt; defines the HTML elements that play the drum sounds in this app. The &lt;code&gt;const key variable&lt;/code&gt; defines the HTML elements by their key codes so that the correct sound plays when a specific key is pressed. The twist in this variable is the use of the &lt;strong&gt;template literal&lt;/strong&gt; (see the bright green box in the screenshot below) to include the key codes as variables. Using the template literal in this context makes it simple to pull in the key code of any key pressed on the keyboard.&lt;/p&gt;

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

&lt;p&gt;To make sure that sounds play only when the correct keys are pressed, an if condition is included. In plain English, it says, “if the triggered &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; tag doesn’t have the &lt;code&gt;data-key keyCode&lt;/code&gt; attribute, exit the function.” This isn’t all that intuitive, because what we’re actually doing is making sure that, if a key is pressed, it’s bound to an &lt;code&gt;&amp;lt;audio&amp;gt;&lt;/code&gt; tag with the &lt;code&gt;data-key&lt;/code&gt; attribute. For example, if you press down on the ‘H’ key, you’ll hear the ride cymbal sound. If you press the ‘Y’ key, nothing will happen, because the ‘Y’ key isn’t defined in the HTML. &lt;/p&gt;

&lt;p&gt;We could just run the &lt;code&gt;audio.play()&lt;/code&gt; method, but this creates a bit of a problem. The sound plays once, but won’t play again if the key is pressed fast or held down. To allow the key to play the sound repeatedly, we need to ‘rewind’ (reset) the sound. We do that by setting &lt;code&gt;audio.currentTime&lt;/code&gt; to 0. Now, when we press the same key really fast or hold it down, the sound plays over and over again, like it should. &lt;/p&gt;

&lt;p&gt;With the audio part of this project covered, let’s dig into the animation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Animation
&lt;/h2&gt;

&lt;p&gt;The nifty little animation that executes every time a drum key is played happens through a combination of CSS and JavaScript. First, the relevant CSS rules: &lt;/p&gt;

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

&lt;p&gt;...and the JavaScript:&lt;/p&gt;

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

&lt;p&gt;There are a few things going on here. First, we need to identify which HTML elements to target with &lt;code&gt;const keys&lt;/code&gt;. To accomplish that, we use the &lt;code&gt;forEach()&lt;/code&gt; loop to add the event listener to every key on the keyboard. &lt;/p&gt;

&lt;p&gt;Before adding the event listener, when a key was played, the animation triggered, but was stuck on, like below:&lt;/p&gt;

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

&lt;p&gt;This is definitely an undesired effect. What we want is for the effect to automatically disappear after the sound has played. To do that, we need to use a new method, ‘transitionend’, for the event listener. &lt;/p&gt;

&lt;p&gt;When a drum key is pressed, the event listener listens for the CSS transition property to end.&lt;/p&gt;

&lt;p&gt;With the event listener implemented, each drum key displays the transition and returns to its original state automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I Learned from This Project
&lt;/h2&gt;

&lt;p&gt;I learned how to code key events and bind them to other HTML tags to do something, like play a sound. While animations are nothing new to me, I learned about the addEventListener transitionend method, and plan to use this in other projects. &lt;/p&gt;

&lt;p&gt;Gaining the new knowledge and putting it to work right away is great, and I am doing that. Aside from the technical aspects of this project, I learned a lot about how I tend to approach new projects and where I need improvement. &lt;/p&gt;

&lt;h2&gt;
  
  
  Test The Code, Stupid!
&lt;/h2&gt;

&lt;p&gt;One new trick I learned is to run console.log() to test every single new function and method (unless it’s part of JavaScript). I generally have written the code first, then went back to debug. This is a huge waste of time and effort! I’ll be adopting this new ‘test as I code’ approach to every project I work on. &lt;/p&gt;

&lt;h2&gt;
  
  
  You Don’t Know What You Don’t Know
&lt;/h2&gt;

&lt;p&gt;I wasted time trying to do this project without first watching the course. My takeaway from this is to spend time carefully researching something before I dive in. Part of me feels like this is cheating, but it really isn’t. It’s about looking at similar projects from other developers and seeing their code before I jump into a project blind. This saves a lot of time and frustration.  &lt;/p&gt;

&lt;p&gt;I value your feedback. Please share your thoughts in the comment section. Thanks!&lt;/p&gt;

</description>
      <category>wesbos</category>
      <category>javascript</category>
      <category>javascript30</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Introduction to the Series</title>
      <dc:creator>Brian Francoeur</dc:creator>
      <pubDate>Mon, 18 May 2020 19:35:33 +0000</pubDate>
      <link>https://dev.to/bfrancoeur/introduction-to-the-series-28pl</link>
      <guid>https://dev.to/bfrancoeur/introduction-to-the-series-28pl</guid>
      <description>&lt;p&gt;Before heading into the first blog post, allow me to share a little bit about my experience as a web developer and why I took the &lt;a href="https://javascript30.com/"&gt;&lt;strong&gt;JavaScript30&lt;/strong&gt;&lt;/a&gt; course. &lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;In 2014, I got my first contract as a WordPress developer. Two years later, I was working 70 hours a week as a freelance web developer while keeping a 25-hour-per-week part-time job. I couldn’t hang with the 95 hour weeks, so I quit the job and became a full-time freelance developer. For the next four years, I got by. I was just good enough to get contracts and make a little money from them. Early this year (2020), I landed a GREAT contract, the best one ever. Two months later, I &lt;em&gt;lost&lt;/em&gt; that contract because my JavaScript skills weren’t at the level needed by the client.&lt;/p&gt;

&lt;h2&gt;
  
  
  Failure is a Strong Motivator
&lt;/h2&gt;

&lt;p&gt;The loss of that contract made me question if I should even continue being a web developer. After a week or so, I decided to solve the problem. Over the following weeks, I leaned on my &lt;a href="https://codingcoach.io"&gt;&lt;strong&gt;Coding Coach&lt;/strong&gt;&lt;/a&gt; mentor &lt;strong&gt;Robert Mion&lt;/strong&gt; to help me find a way forward that would help me fill in the gaps in my knowledge and skills. Along the way, I discovered &lt;strong&gt;Wes Bos’&lt;/strong&gt; &lt;a href="https://javascript30.com/"&gt;&lt;strong&gt;JavaScript30&lt;/strong&gt;&lt;/a&gt; course and signed up for it. It was free and, what’s the worst that can happen -- I discover the course isn’t what I need and I find another one? &lt;/p&gt;

&lt;p&gt;As it turns out, the &lt;strong&gt;JavaScript30&lt;/strong&gt; course was a perfect fit. What follows are a series of posts, one per lesson, where I share what the lesson was about and what I learned from it.&lt;/p&gt;

&lt;p&gt;Turns out that I learned a lot more than just JavaScript. I’ll share those details as I write each blog post. &lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach
&lt;/h2&gt;

&lt;p&gt;Since I already have six years of experience as a web developer, I am approaching each lesson with a ‘let’s see if I can do this on my own first’ approach. In some cases, I was pretty close to Wes’ code. Other times, I couldn’t have been more lost if I had wandered into a strange land without a compass, precisely my experience with the first lesson. &lt;/p&gt;

&lt;p&gt;So, let’s dive into the &lt;strong&gt;Javascript30 Drum Kit&lt;/strong&gt; lesson and find out how I got lost so fast!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>javascript30</category>
      <category>wesbos</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
