<?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: Joe Avila</title>
    <description>The latest articles on DEV Community by Joe Avila (@javila35).</description>
    <link>https://dev.to/javila35</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%2F339858%2Fb1532353-1d7d-421a-8dd4-2bfff6bb18e4.png</url>
      <title>DEV Community: Joe Avila</title>
      <link>https://dev.to/javila35</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/javila35"/>
    <language>en</language>
    <item>
      <title>Smashing my first bug</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Tue, 18 May 2021 23:52:14 +0000</pubDate>
      <link>https://dev.to/javila35/smashing-my-first-bug-4629</link>
      <guid>https://dev.to/javila35/smashing-my-first-bug-4629</guid>
      <description>&lt;h3&gt;
  
  
  Contribute to open source they said, it's a great way to break into tech they said.
&lt;/h3&gt;

&lt;p&gt;A lot easier said than done. I have been developing on the modern web for about a year. I used to code all the time back in the early 2000's when vanilla Javascript, HTML, and CSS was the law of the land. Things have changed a lot since then. Just like learning modern web development, getting involved in open source can feel like drinking from a fire hose.&lt;/p&gt;

&lt;h3&gt;
  
  
  Open Source Background
&lt;/h3&gt;

&lt;p&gt;I haven't made any meaningful contributions to projects. I helped &lt;a href="https://github.com/jsjoeio/joeprevite.com/issues/170" rel="noopener noreferrer"&gt;Joe Previte&lt;/a&gt; with a styling bug on his portfolio, but he already had the solution coded out.&lt;/p&gt;

&lt;p&gt;I spent 6 months on a contract for a consulting firm where I got neck deep in a React/Typescript + GraphQL/NodeJS application. That really boosted my confidence in reading others code, and reading &lt;strong&gt;very large&lt;/strong&gt; code files. That experience made me feel more prepared to jump into forem's codebase.&lt;/p&gt;

&lt;h3&gt;
  
  
  About This Bug
&lt;/h3&gt;


&lt;div class="ltag_github-liquid-tag"&gt;
  &lt;h1&gt;
    &lt;a href="https://github.com/forem/forem/issues/12545" rel="noopener noreferrer"&gt;
      &lt;img class="github-logo" alt="GitHub logo" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg"&gt;
      &lt;span class="issue-title"&gt;
        "Unable to load Comment Subscription component"
      &lt;/span&gt;
      &lt;span class="issue-number"&gt;#12545&lt;/span&gt;
    &lt;/a&gt;
  &lt;/h1&gt;
  &lt;div class="github-thread"&gt;
    &lt;div class="timeline-comment-header"&gt;
      &lt;a href="https://github.com/ludwiczakpawel" rel="noopener noreferrer"&gt;
        &lt;img class="github-liquid-tag-img" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Favatars.githubusercontent.com%2Fu%2F108287%3Fv%3D4" alt="ludwiczakpawel avatar"&gt;
      &lt;/a&gt;
      &lt;div class="timeline-comment-header-text"&gt;
        &lt;strong&gt;
          &lt;a href="https://github.com/ludwiczakpawel" rel="noopener noreferrer"&gt;ludwiczakpawel&lt;/a&gt;
        &lt;/strong&gt; posted on &lt;a href="https://github.com/forem/forem/issues/12545" rel="noopener noreferrer"&gt;&lt;time&gt;Feb 02, 2021&lt;/time&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag-github-body"&gt;
      

&lt;p&gt;&lt;strong&gt;Describe the bug&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="https://user-images.githubusercontent.com/108287/106653335-f253a580-6596-11eb-8ecc-3582965616a1.png" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F108287%2F106653335-f253a580-6596-11eb-8ecc-3582965616a1.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;It's been happening quite often recently for me but I can't really tell how exactly this should be reproduced. It does work after refreshing the page, but then when I go to another post it's broken again.&lt;/p&gt;
&lt;p&gt;Also, this is what's happening in the console:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://user-images.githubusercontent.com/108287/106653573-465e8a00-6597-11eb-8ff7-03bf34f7b404.png" rel="nofollow noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fuser-images.githubusercontent.com%2F108287%2F106653573-465e8a00-6597-11eb-8ff7-03bf34f7b404.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;To Reproduce&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;See above.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Expected behavior&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Fo this component to work properly :)&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Screenshots&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;See above.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Desktop (please complete the following information):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;n/a&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Smartphone (please complete the following information):&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;n/a&lt;/p&gt;

    &lt;/div&gt;
    &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/forem/forem/issues/12545" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;There was a console error thrown any time a text editor component was rendered after the first time. So if you visited an article, left and went to a different article then it would say a variable, &lt;code&gt;subscribeBtn&lt;/code&gt;, had already been declared.&lt;/p&gt;

&lt;h3&gt;
  
  
  How I Smashed It
&lt;/h3&gt;


&lt;div class="ltag_github-liquid-tag"&gt;
  &lt;h1&gt;
    &lt;a href="https://github.com/forem/forem/pull/13694" rel="noopener noreferrer"&gt;
      &lt;img class="github-logo" alt="GitHub logo" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev.to%2Fassets%2Fgithub-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg"&gt;
      &lt;span class="issue-title"&gt;
        Fix "Unable to load Comment Subscription component"
      &lt;/span&gt;
      &lt;span class="issue-number"&gt;#13694&lt;/span&gt;
    &lt;/a&gt;
  &lt;/h1&gt;
  &lt;div class="github-thread"&gt;
    &lt;div class="timeline-comment-header"&gt;
      &lt;a href="https://github.com/javila35" rel="noopener noreferrer"&gt;
        &lt;img class="github-liquid-tag-img" src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Favatars.githubusercontent.com%2Fu%2F58503695%3Fv%3D4" alt="javila35 avatar"&gt;
      &lt;/a&gt;
      &lt;div class="timeline-comment-header-text"&gt;
        &lt;strong&gt;
          &lt;a href="https://github.com/javila35" rel="noopener noreferrer"&gt;javila35&lt;/a&gt;
        &lt;/strong&gt; posted on &lt;a href="https://github.com/forem/forem/pull/13694" rel="noopener noreferrer"&gt;&lt;time&gt;May 09, 2021&lt;/time&gt;&lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag-github-body"&gt;
      
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;What type of PR is this? (check all applicable)&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;[ ] Refactor&lt;/li&gt;
&lt;li&gt;[ ] Feature&lt;/li&gt;
&lt;li&gt;[x] Bug Fix&lt;/li&gt;
&lt;li&gt;[ ] Optimization&lt;/li&gt;
&lt;li&gt;[ ] Documentation Update&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;Description&lt;/h2&gt;
&lt;p&gt;This PR fixes the console error described in &lt;a href="https://github.com/forem/forem/issues/12545" rel="noopener noreferrer"&gt;the issue&lt;/a&gt; by changing the &lt;code&gt;const&lt;/code&gt; declaration to &lt;code&gt;var&lt;/code&gt;. I was unable to replicate the UI bug without repro steps.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;changePage&lt;/code&gt; function in &lt;code&gt;base.js.erb&lt;/code&gt; sending over the &lt;code&gt;user_subscription_tag&lt;/code&gt; when it's called, and declaring the &lt;code&gt;subscribeBtn&lt;/code&gt; as a const was causing the error. I'm not familiar with liquid tags, but it seems that the other liquid tags that use JS declare variables with &lt;code&gt;var&lt;/code&gt; instead of ES6.&lt;/p&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;Related Tickets &amp;amp; Documents&lt;/h2&gt;
&lt;p&gt;Resolves #12545&lt;/p&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;Added tests?&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;[ ] Yes&lt;/li&gt;
&lt;li&gt;[ ] No, and this is why: &lt;em&gt;please replace this line with details on why tests
have not been included&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;[x] I need help with writing tests&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;[Forem core team only] How will this change be communicated?&lt;/h2&gt;
&lt;p&gt;&lt;em&gt;Will this PR introduce a change that impacts Forem members or creators, the
development process, or any of our internal teams? If so, please note how you
will share this change with the people who need to know about it.&lt;/em&gt;&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;[ ] I've updated the &lt;a href="https://docs.forem.com" rel="nofollow noopener noreferrer"&gt;Developer Docs&lt;/a&gt; and/or
&lt;a href="https://forem.gitbook.io/forem-admin-guide/" rel="nofollow noopener noreferrer"&gt;Admin Guide&lt;/a&gt;, or
&lt;a href="https://storybook.forem.com/" rel="nofollow noopener noreferrer"&gt;Storybook&lt;/a&gt; (for Crayons components)&lt;/li&gt;
&lt;li&gt;[ ] I've updated the README or added inline documentation&lt;/li&gt;
&lt;li&gt;[ ] I will share this change in a &lt;a href="https://forem.dev/t/changelog" rel="nofollow noopener noreferrer"&gt;Changelog&lt;/a&gt;
or in a &lt;a href="http://forem.dev" rel="nofollow noopener noreferrer"&gt;forem.dev&lt;/a&gt; post&lt;/li&gt;
&lt;li&gt;[ ] I will share this change internally with the appropriate teams&lt;/li&gt;
&lt;li&gt;[ ] I'm not sure how best to communicate this change and need help&lt;/li&gt;
&lt;li&gt;[ ] This change does not need to be communicated, and this is why not: &lt;em&gt;please
replace this line with details on why this change doesn't need to be
shared&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;[optional] Are there any post deployment tasks we need to perform?&lt;/h2&gt;
&lt;h2&gt;
&lt;span class="octicon octicon-link"&gt;&lt;/span&gt;[optional] What gif best describes this PR or how it makes you feel?&lt;/h2&gt;
&lt;p&gt;&lt;a href="gif_link" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/gif_link" alt="alt_text"&gt;&lt;/a&gt;&lt;/p&gt;

    &lt;/div&gt;
    &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/forem/forem/pull/13694" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;
  
  
  Reflection
&lt;/h3&gt;

&lt;p&gt;My biggest challenge in contributing to open source is believing in myself. Before asking to be assigned to the bug, I went into the code base and searched out the identifying variable. Once I found it, I had no idea what was going on or what the fix was but I knew it was within my grasp. Before I could think myself out of the challenge, I asked to be assigned.&lt;/p&gt;

&lt;p&gt;I'm really glad I signed up for it because I was forced to play around in someone else's code base using a tool I'm not I'm not super familiar with: liquid tags. But my knowledge of javascript and googling powers helped me solve it. &lt;/p&gt;

&lt;p&gt;Someone had declared a variable in Dev's text editor component with &lt;code&gt;const&lt;/code&gt;. When a user (you) switched pages (to edit a post or comment, or to a new article) the entire bundle was being sent again. Because the original value was declared with &lt;code&gt;const&lt;/code&gt; the application was throwing an error saying there was already a variable with the name &lt;code&gt;subscribeBtn&lt;/code&gt; present. &lt;/p&gt;

&lt;h3&gt;
  
  
  Suggestions for DEV Community Bug Smash
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;bin/startup&lt;/code&gt; command didn't work for me. Couldn't get foreman to run properly. I just ran the three commands in the  that Procfile in separate terminal windows and that worked fine. Never looked into the foreman issue.&lt;/p&gt;

</description>
      <category>devbugsmash</category>
    </item>
    <item>
      <title>React Router Hook =&gt; useParam() (now w/ Typescript)</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Thu, 08 Apr 2021 21:33:28 +0000</pubDate>
      <link>https://dev.to/javila35/react-router-hook-useparam-now-w-typescript-m93</link>
      <guid>https://dev.to/javila35/react-router-hook-useparam-now-w-typescript-m93</guid>
      <description>&lt;p&gt;While working on a project I needed to get some information out of the URL in the browser. Using the URL to declare what to display is a common situation for frontend, client-side rendering. For example, grabbing an ID or username to send to the backend which is the situation for my current project. &lt;/p&gt;

&lt;p&gt;I was using class-based React components when I began learning React. Six months later I'm learning the ins and outs of functional components with Hooks to maintain necessary stateful logic without all the extra lifecycle methods. This learning led me to discover the &lt;a href="https://reactrouter.com/web/api/Hooks"&gt;Hooks of React Router&lt;/a&gt;. &lt;/p&gt;






&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/quizzes/:id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Quiz&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="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;Quiz&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//necessary state values here&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;

   &lt;span class="nx"&gt;componentDidMount&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getQuiz&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;match&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;params&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
         &lt;span class="c1"&gt;// logic&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;Here's a code snippet from a project I worked on when I first started with React. This &lt;code&gt;Route Path&lt;/code&gt; component has parameters that I want to use. While using the render method of Route, I have to destructure or "spread" props inside the component to gain access to the &lt;a href="https://reactrouter.com/web/api/match"&gt;match object&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;I declare that anything following the &lt;code&gt;/quizzes/&lt;/code&gt; portion of the Route will be an &lt;code&gt;:id&lt;/code&gt;. I pass props into the callback and spread the props object into the Quiz component as outlined by the React Router documentation. All this to gain access to the piece of the URL I need. Now when the user is at &lt;code&gt;myapp.com/quizzes/1&lt;/code&gt; a Quiz component will be rendered. Once this component mounts it will use the 1 from the URL as an ID  and make a call to my backend to filter through my Quiz database. If the URL were &lt;code&gt;myapp.com/quizzes/cat&lt;/code&gt; then my API call would try using &lt;code&gt;cat&lt;/code&gt; as an ID and an error would be sent back from my database. Unless of course, my database contains a quiz with the ID of &lt;code&gt;cat&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ngs9Nqu0pqkec/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ngs9Nqu0pqkec/source.gif" alt="Cat Data being sent back"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;It's worth noting when I wrote the first project I was very new to React and I had been instructed to use the render method to gain access to props. Looking back, I may have been able to make this code less tedious by using the component method of Route where the components already have access to &lt;code&gt;match, history &amp;amp; history&lt;/code&gt; as you'll see me do in the next example.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Here is what that same functionality looks like with the useParams() hook.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Route&lt;/span&gt; &lt;span class="nx"&gt;exact&lt;/span&gt; &lt;span class="nx"&gt;path&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/seasons/:id&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;Season&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Team&lt;/span&gt; &lt;span class="o"&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;({});&lt;/span&gt;
   &lt;span class="kd"&gt;const&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useParams&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

   &lt;span class="nx"&gt;useEffect&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="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getTeam&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="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&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;//logic &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;Once again I declare anything following &lt;code&gt;/seasons/&lt;/code&gt; will be an &lt;code&gt;:id&lt;/code&gt;. Gaining access to the &lt;code&gt;params&lt;/code&gt; is as simple as calling the hook and destructuring the match object, which I know will have a key of &lt;code&gt;id&lt;/code&gt;, which points to my value (ie &lt;code&gt;1&lt;/code&gt; or &lt;code&gt;cat&lt;/code&gt;). I don't have to chain calls like in the last example &lt;code&gt;props.match.params.id&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;I'm still learning about all the benefits of hooks but I do prefer the look of the code. Thanks for reading, and I'm happy to share my limited knowledge if you have any questions. I'm also very interested in feedback on my use cases.&lt;/p&gt;




&lt;p&gt;Update: I've been recently working in typescript. I'm revisiting this article to add how to use this hook with TS. This is assuming you have some basic understanding of Typescript. If you don't, I'd recommend this very basic guide : &lt;a href="https://ts.chibicode.com/"&gt;Typescript for Beginner Programmers by ChibiCode&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In order to &lt;code&gt;useParams&lt;/code&gt; you need to implement a generic for useParams. Building on my example above, I need to type the &lt;code&gt;id&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;QuizParams&lt;/span&gt; &lt;span class="o"&gt;=&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="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// In order to implement that, I'd apply my type to the hook when calling it.&lt;/span&gt;

&lt;span class="kd"&gt;const&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;useParams&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;QuizParams&lt;/span&gt;&lt;span class="o"&gt;&amp;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 typescript knows what to expect as a return value from  the &lt;code&gt;useParams&lt;/code&gt; hook.&lt;/p&gt;

</description>
      <category>react</category>
      <category>typescript</category>
      <category>javascript</category>
    </item>
    <item>
      <title>BDD Testing with Cucumber</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Sat, 28 Nov 2020 00:21:26 +0000</pubDate>
      <link>https://dev.to/javila35/bdd-testing-with-cucumber-1k39</link>
      <guid>https://dev.to/javila35/bdd-testing-with-cucumber-1k39</guid>
      <description>&lt;p&gt;Recently at work I got the opportunity to write Behavior Driven Development tests using &lt;a href="https://cucumber.io/docs/installation/javascript/" rel="noopener noreferrer"&gt;Cucumber.js&lt;/a&gt;. Cucumber uses a language called Gherkin to write the test steps and uses javascript to execute them. Gherkin is written in plain english &lt;a href="https://cucumber.io/docs/gherkin/languages/" rel="noopener noreferrer"&gt;(or a selection of other languages)&lt;/a&gt; and is made to be read by any (particularly non-dev) team members. If you haven't written tests before Cucumber is a great place to start.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setting up the WebDriver
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ~/cuc-test/bdd/features/step_definitions/stepdefs.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Capabilities&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;By&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;selenium-webdriver&lt;/span&gt;&lt;span class="dl"&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;driver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;withCapabilities&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Capabilities&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;chrome&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.selenium.dev/documentation/en/webdriver/" rel="noopener noreferrer"&gt;Selenium WebDriver&lt;/a&gt; is the technology we'll use to run our tests. At a high level, it follows the test steps and executes them in a browser. &lt;code&gt;Builder&lt;/code&gt; creates the driver when we run our tests. &lt;code&gt;Capabilities&lt;/code&gt; is how you declare your browser. &lt;code&gt;By&lt;/code&gt; is a keyword we'll use in our tests to define what we're looking for.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I found the documentation for Selenium a bit hard to navigate. I mostly referenced the &lt;a href="https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/index.html" rel="noopener noreferrer"&gt;API documentation for Javascript&lt;/a&gt; or just searched the issue I was having.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Gherkin keywords
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fx0n9xd9dakv85k0a3xfl.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%2Fx0n9xd9dakv85k0a3xfl.png" alt="Screen shot of the initial test page"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I spun up a simple html page with a few headers, a text input, a drop down menu, and a button to submit your changes. The first test I wrote checks that the default values are all as I'd expect them to be.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="kn"&gt;Scenario&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;I &lt;/span&gt;want to check default values of the page
    &lt;span class="nf"&gt;Given &lt;/span&gt;I am on the home page
    &lt;span class="nf"&gt;Then &lt;/span&gt;I should see the default title
    &lt;span class="nf"&gt;And &lt;/span&gt;I should see the text input field
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Scenario, Given, Then &amp;amp; And&lt;/strong&gt; are all keywords that come from the gherkin syntax. In my &lt;code&gt;Scenario&lt;/code&gt; statement I summarize what the following test steps do. This keyword is used to group steps together and give context for users running the tests.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Given, Then &amp;amp; And&lt;/code&gt; declare steps for a test. The string following these declaration keywords connect the step and code to execute. If you have a typo or don't match them exactly you'll receive an &lt;code&gt;Undefined&lt;/code&gt; error when you run the tests.  This is the meat and potatoes of Cucumber testing.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/I0WQIk0xbBVvO/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/I0WQIk0xbBVvO/source.gif" alt="Sasha with Meat and Potatoes GIF"&gt;&lt;/a&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="nc"&gt;Given&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I am on the home page&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost:3000/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nc"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I should see the default title&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;xpath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`.//*[text()[contains(.,'Welcome to the site.')]]`&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nc"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I should see the text input field&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;textbox&lt;/span&gt;&lt;span class="dl"&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;p&gt;The first step of my scenario navigates to the the page I pass into the &lt;code&gt;get&lt;/code&gt; method. The second step tells the webdriver to search the page for the text I pass into the &lt;code&gt;xpath&lt;/code&gt;. The third searches for any element with an ID of textbox. &lt;em&gt;Remember, we imported &lt;code&gt;By&lt;/code&gt; from Selenium in our set up. &lt;a href="https://www.selenium.dev/selenium/docs/api/javascript/module/selenium-webdriver/index_exports_By.html" rel="noopener noreferrer"&gt;There a plenty of options to search by.&lt;/a&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight gherkin"&gt;&lt;code&gt;&lt;span class="kn"&gt;Scenario&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;I &lt;/span&gt;want to make sure all the colors work
        &lt;span class="nf"&gt;Given &lt;/span&gt;I am on the home page
        &lt;span class="nf"&gt;Then &lt;/span&gt;I want to select the color 'Green' from the drop down
        &lt;span class="nf"&gt;And &lt;/span&gt;I should submit the changes
        &lt;span class="nf"&gt;Then &lt;/span&gt;I should make sure the third header is 'Green'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since I'm checking the default values in the last scenario I hardcode the parameters to search for. But it's common to pass parameters through the test steps. Notice how green is in quotations in steps two and four.&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="nc"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I want to select the color {string} from the drop down&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;dropDown&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;selector&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="nx"&gt;dropDown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nx"&gt;dropDown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sendKeys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;dropDown&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;click&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nc"&gt;Then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;I should make sure the third header is {string}&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;string&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;driver&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;By&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;changing-header&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;getCssValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;color&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// getCSSValue returns an rgb value&lt;/span&gt;
    &lt;span class="c1"&gt;// colorMap is an object with keys of string and values of the associated value&lt;/span&gt;
    &lt;span class="nf"&gt;expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;to&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;colorMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;string&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;&lt;code&gt;Then&lt;/code&gt; is a function that takes two parameters. The first parameter is the string that we used to declare the test step. Inside of that string we signal a nested variable by wrapping it in curly braces, &lt;code&gt;{string}&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The second parameter is a callback function, where we pass in the variable. &lt;em&gt;Cucumber is particular about what you call the variable, if you pass in a number character you'd use &lt;code&gt;int&lt;/code&gt;. If you have more than one variable you'd declare it as &lt;code&gt;string, string2, string3&lt;/code&gt; in the callback but just as &lt;code&gt;{string}&lt;/code&gt; in the first parameter.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;I also used a couple other methods of the driver like &lt;code&gt;click()&lt;/code&gt;, &lt;code&gt;sendKeys(string)&lt;/code&gt;, and &lt;code&gt;getCssValue&lt;/code&gt;. Those methods do exactly what the name implies. &lt;code&gt;sendKeys&lt;/code&gt; sends the string you pass in to the value field.&lt;/p&gt;




&lt;p&gt;While working on writing a bunch of tests for old components I began to think of the tests I would write as I created new components. What types of identifiers does this component need to be testable? Is it a class name, an ID, or certain unique text that won't appear elsewhere when this is loaded? I began to think about how I would write my new components to comply with the tests I'd eventually write. In some cases, I started with the tests first. Cucumber encourages that by giving giving you the line of code you need to write when it encounters a step it doesn't recognize.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fhyf8nuqv95kc1blsuc6l.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%2Fhyf8nuqv95kc1blsuc6l.png" alt="Undefined error thrown by Cucumber"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can &lt;a href="https://github.com/javila35/cucumber-example" rel="noopener noreferrer"&gt;fork this repo&lt;/a&gt; and open it up on your machine if you'd like to learn by playing instead. There's a couple of tests that need to fixing in order to pass. Feel free to submit a PR if you come up with any cool tests.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cover image: &lt;a href="https://creativecommons.org/licenses/by/2.0/" rel="noopener noreferrer"&gt;"cucumbers en route to pickledom" by Stacy Spensley is licensed with CC BY 2.0.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>testing</category>
    </item>
    <item>
      <title>Object prototype property and Hunter x Hunter</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Mon, 28 Sep 2020 17:15:05 +0000</pubDate>
      <link>https://dev.to/javila35/object-prototype-property-and-hunter-x-hunter-39fk</link>
      <guid>https://dev.to/javila35/object-prototype-property-and-hunter-x-hunter-39fk</guid>
      <description>&lt;p&gt;Recently, I have been learning about inheritance and the prototype chain. If you have read my posts you already know I've been diving into the fundamentals of Javascript. You probably don't know I am a huge Hunter x Hunter fan as well. I put my learning about in practice by creating a Hunter prototype object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Mdd4TcFG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x9xfc573laezb8y0g1g7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Mdd4TcFG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/x9xfc573laezb8y0g1g7.gif" alt="Gon and Killua"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Function Constructor
&lt;/h2&gt;

&lt;p&gt;A function constructor can be seen as a blueprint for a particular type of object. The convention is to name a function constructor with a capital letter. For example, my function constructor is a blueprint to create &lt;strong&gt;Hunters&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;I declare that all Hunters will take two parameters, a &lt;code&gt;name&lt;/code&gt; and &lt;code&gt;hatsu&lt;/code&gt; &lt;em&gt;(or special power for those who aren't familiar)&lt;/em&gt;. Each Hunter will be initialized with a &lt;code&gt;powerLevel&lt;/code&gt; of 100. Now let's add some abilities to our Hunters.&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;const&lt;/span&gt; &lt;span class="nx"&gt;Hunter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hatsu&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&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;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&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;hatsu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;hatsu&lt;/span&gt;&lt;span class="p"&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;powerLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;100&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Hunter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;charge&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;number&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="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;number&lt;/span&gt;&lt;span class="dl"&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please enter a number&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;}&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;powerLevel&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;number&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; power level at &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&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;powerLevel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;Hunter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useHatsu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;powerLevel&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; used &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&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;hatsu&lt;/span&gt;&lt;span class="p"&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;powerLevel&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="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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&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;name&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt; needs to charge.&lt;/span&gt;&lt;span class="dl"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using the &lt;code&gt;prototype&lt;/code&gt; property to store the methods &lt;code&gt;charge&lt;/code&gt; and &lt;code&gt;useHatsu&lt;/code&gt; all future hunters will have the ability to charge up and use their powers. I could declare these functions in my constructor. That would mean each new Hunter created would have it's own &lt;code&gt;charge&lt;/code&gt; and &lt;code&gt;useHatsu&lt;/code&gt; method. That's a bit excessive, I don't need each Hunter to own the method I just need each Hunter to have access to it.&lt;/p&gt;

&lt;p&gt;I save memory by creating the function once and allowing each Hunter to inherit these methods from the &lt;code&gt;Hunter.prototype&lt;/code&gt; property.&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;const&lt;/span&gt; &lt;span class="nx"&gt;gon&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Hunter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Gon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ja-Jan-Ken&lt;/span&gt;&lt;span class="dl"&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;killua&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Hunter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Killua&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Lightningbolt&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;gon&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;killua&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;killua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;charge&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="nx"&gt;gon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;charge&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="nx"&gt;killua&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useHatsu&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;gon&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useHatsu&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you haven't entered the code into the console yet, go ahead and add it all. Take a look at the objects created. You'll notice they have their &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;hatsu&lt;/code&gt;, and &lt;code&gt;powerLevel&lt;/code&gt; as expected. You may notice the function we created is not stored in the object. They are stored in its &lt;code&gt;___proto___&lt;/code&gt; property. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YCtg9aBv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cauybmvmj083i5dl554a.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YCtg9aBv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cauybmvmj083i5dl554a.png" alt="Examining an object's proto property."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We don't need to include the &lt;code&gt;prototype&lt;/code&gt; property in our function call because of the &lt;strong&gt;prototype chain&lt;/strong&gt;. Similar to the &lt;a href="https://dev.to/javila35/scoping-chain-50il"&gt;scoping chain&lt;/a&gt;, javascript checks the object's own methods and properties. If nothing is found it checks the object's prototype. It continues this process until it reaches the top-level of the prototype chain which is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I covered a lot of complicated topics in a very brief manner. If something doesn't make sense please reach out. &lt;a href="https://timkadlec.com/2008/01/using-prototypes-in-javascript"&gt;This article by Tim Kadlec&lt;/a&gt; helped a lot.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RgCltbr0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b3d9rfo72ha3i3raq5qu.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RgCltbr0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/b3d9rfo72ha3i3raq5qu.gif" alt="Netero has gratitude"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What is this?</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Mon, 21 Sep 2020 17:51:37 +0000</pubDate>
      <link>https://dev.to/javila35/what-is-this-33aa</link>
      <guid>https://dev.to/javila35/what-is-this-33aa</guid>
      <description>&lt;p&gt;In javascript, you have access to &lt;code&gt;this&lt;/code&gt; keyword at any time. Unless there are certain conditions met &lt;code&gt;this&lt;/code&gt; will always refer to the global object &lt;code&gt;Window&lt;/code&gt;. &lt;code&gt;console.log(this)&lt;/code&gt; in your console to see the &lt;code&gt;Window object&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Methods and thisBinding
&lt;/h1&gt;

&lt;p&gt;One use case where &lt;code&gt;this&lt;/code&gt; will change is in an object method. &lt;a href="https://www.w3schools.com/js/js_object_methods.asp"&gt;A method is a function stored as an object property&lt;/a&gt;, like &lt;code&gt;displayObject&lt;/code&gt; below. If you run the code block in your console you'll first see the window object like before and then the updated value for &lt;code&gt;this&lt;/code&gt;, our new object.&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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;myObject&lt;/span&gt; &lt;span class="o"&gt;=&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="s1"&gt;Object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;purpose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Give an example.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="na"&gt;displayObject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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="nx"&gt;myObject&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;displayObject&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you understand the process for an execution context than &lt;code&gt;this&lt;/code&gt; will be easy to understand. Every time a new execution context is created &lt;code&gt;thisBinding&lt;/code&gt; is evaluated. If it's called on a method, &lt;code&gt;this&lt;/code&gt; becomes the object that owns the method. Otherwise, it remains the &lt;code&gt;Window&lt;/code&gt; object. &lt;a href="https://dev.to/digitalocean/understanding-this-bind-call-and-apply-in-javascript-dla"&gt;There are other circumstances where &lt;code&gt;this&lt;/code&gt; can change,&lt;/a&gt; and if you're interested in learning more &lt;a href="https://dev.to/skaytech/what-is-this-in-javascript-3d3i"&gt;other users go into a much deeper dive in the topic than I do in this article.&lt;/a&gt; &lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Scoping Chain</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Tue, 15 Sep 2020 05:03:18 +0000</pubDate>
      <link>https://dev.to/javila35/scoping-chain-50il</link>
      <guid>https://dev.to/javila35/scoping-chain-50il</guid>
      <description>&lt;p&gt;Calling a function in Javascript creates a new &lt;a href="https://dev.to/javila35/what-is-the-execution-context-in-js-in"&gt;execution context&lt;/a&gt;, and when that is created so is a scope chain for that function. The scope chain defines what variables and functions this execution context has access to. &lt;/p&gt;

&lt;p&gt;There is a global context which is the javascript file currently being executed. It's safe to assume that all execution contexts on the execution stack have access to the global context, and it's environment record. Consider the following code:&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;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is the global context.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;one&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;insideOne&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside of function one.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I have access to globalVar here.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;globalVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

   &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;two&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;insideTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside of function two.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nx"&gt;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I also have access to globalVar in function two.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;insideOne&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;insideTwo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;globalVar&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;function&lt;/span&gt; &lt;span class="nx"&gt;three&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;insideThree&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;this is inside function three.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;globalVar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I also have access to globalVar in function thre.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;insideThree&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;globalVar&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;insideOne&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;one&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nx"&gt;three&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you run this in your console, you'll see the messages up until we hit the last line of &lt;code&gt;function three()&lt;/code&gt; where we try to call the variable &lt;code&gt;insideOne&lt;/code&gt;. We don't have access to &lt;code&gt;insideOne&lt;/code&gt; because it's outside of the current scope chain. That variable only exists in the execution context of &lt;code&gt;function one()&lt;/code&gt; and &lt;code&gt;function two()&lt;/code&gt;. Once those are cleared off the execution stack, javascript erases them from memory.&lt;/p&gt;

&lt;p&gt;Notice how in our nested function we do have access to the variable &lt;code&gt;insideOne&lt;/code&gt;, and &lt;code&gt;globalVar&lt;/code&gt;. When an execution context creates a scope for a function it maintains the variable objects of its parent function. The global context is the parent of all functions called in a javascript file. As I mentioned earlier, we'll always have access to global variables such as &lt;code&gt;globalVar&lt;/code&gt;. Because &lt;code&gt;function two()&lt;/code&gt; is nested within &lt;code&gt;function one()&lt;/code&gt; we also have access to any variables declared in &lt;code&gt;function one()&lt;/code&gt;. &lt;em&gt;Fun fact, this scoping chain is how &lt;a href="https://www.w3schools.com/js/js_function_closures.asp"&gt;closures&lt;/a&gt; are created.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You may have noticed I called &lt;code&gt;two()&lt;/code&gt; before declaring the function. You can read about that in my &lt;a href="https://dev.to/javila35/hoisting-4b80"&gt;Hoisting article&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Hoisting</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Mon, 07 Sep 2020 17:27:17 +0000</pubDate>
      <link>https://dev.to/javila35/hoisting-4b80</link>
      <guid>https://dev.to/javila35/hoisting-4b80</guid>
      <description>&lt;p&gt;Hoisting variables and function declarations in Javascript happens when the variable object is created for an execution context. &lt;em&gt;&lt;a href="https://dev.to/javila35/what-is-the-execution-context-in-js-in"&gt;See my last article to read about execution contexts.&lt;/a&gt;&lt;/em&gt; Before running any code your browsers &lt;a href="https://en.wikipedia.org/wiki/JavaScript_engine"&gt;javascript engine&lt;/a&gt;  scans the current execution context for variable and function declarations. In plain terms, functions and variables are found before code is executed so javascript knows how to compute once it encounters them in execution mode. This process is known as hoisting by the javascript community.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function declaration hoisting
&lt;/h3&gt;

&lt;p&gt;For each function declaration (&lt;code&gt;function myFunc()&lt;/code&gt;) encountered, a property is created in the variable object that acts as a pointer to that function. This pointer allows you to call a function in your code before it has been declared. Try the code below in your console.&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;myFunc&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;myFunc&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Your number is `&lt;/span&gt; &lt;span class="o"&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's worth mentioning that &lt;strong&gt;function expressions are treated as variables&lt;/strong&gt;. Calling a function expression before the declaration will not run the 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="nx"&gt;runCode&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;runCode&lt;/span&gt; &lt;span class="o"&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;I'm a variable, that points to a function. I don't get defined until execution.&lt;/span&gt;&lt;span class="dl"&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;h3&gt;
  
  
  Variable declaration hoisting
&lt;/h3&gt;

&lt;p&gt;Your browsers engine also looks for variable declarations. For each variable declaration, a property is created in the variable object and (depending on the keyword used) set to undefined.&lt;/p&gt;

&lt;h4&gt;
  
  
  var
&lt;/h4&gt;

&lt;p&gt;Declaring a variable using &lt;code&gt;var&lt;/code&gt; will set that variable to &lt;code&gt;undefined&lt;/code&gt; when hoisting. This allows you to call your variable before declaring. Of course, the value will be &lt;code&gt;undefined&lt;/code&gt;. Try the code below in your console.&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  const &amp;amp;&amp;amp; let
&lt;/h4&gt;

&lt;p&gt;Using &lt;code&gt;const&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt;  to declare your variable will create the pointer like normal, but the variable will not be initialized &lt;em&gt;(aka set to &lt;code&gt;undefined&lt;/code&gt;)&lt;/em&gt;. If you call your variable you'll get an &lt;code&gt;Uncaught ReferenceError&lt;/code&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;cat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;dog&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;cat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Ozma&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;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pippin&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;h4&gt;
  
  
  What hoisting does not do.
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;a strict definition of hoisting suggests that variable and function declarations are physically moved to the top of your code, but this is not in fact what happens. Instead, the variable and function declarations are put into memory during the compile phase, but stay exactly where you typed them in your code.&lt;/p&gt;
&lt;h6&gt;
  
  
  &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Hoisting"&gt;From the MDN&lt;/a&gt;
&lt;/h6&gt;
&lt;/blockquote&gt;

&lt;h6&gt;
  
  
  &lt;em&gt;Cover image: just a cool photo found while searching hoisting on &lt;a href="https://search.creativecommons.org/photos/c64dc13c-e745-42ad-b133-c1c622fad58a"&gt;creative commons&lt;/a&gt;&lt;/em&gt;
&lt;/h6&gt;

&lt;h6&gt;
  
  
  &lt;em&gt;"Great hoists, Hulett and Brown electric at work" by Boston Public Library is licensed under CC BY 2.0&lt;/em&gt;
&lt;/h6&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What is the execution context in JS?</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Sun, 06 Sep 2020 17:12:52 +0000</pubDate>
      <link>https://dev.to/javila35/what-is-the-execution-context-in-js-in</link>
      <guid>https://dev.to/javila35/what-is-the-execution-context-in-js-in</guid>
      <description>&lt;p&gt;If you've written any code in Javascript, you've interacted with the execution context and execution stack. It's a high-level concept that happens behind the scenes of javascript. You don't need to know anything about it to start coding in Javascript. But understanding the basics will help you avoid code that won't execute, and understanding it well will help you debug code that executes in a way you don't expect.&lt;/p&gt;

&lt;p&gt;Loading a javascript file into a browser creates a global execution context. Every time a function in that global context is called, a new function execution context is added to the execution stack. The last function called is the first function to be cleared off the stack. This is referred to as &lt;strong&gt;Last In First Out (LIFO)&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;The execution context is removed from the stack after executing a return statement, or all code has been parsed. There can only ever be one execution context running at a time, but that execution context may be paused at any time to enter a new execution context. &lt;a href="https://www.ecma-international.org/ecma-262/#sec-execution-contexts"&gt;ECMAScript refers to this as the &lt;em&gt;running execution context&lt;/em&gt;.&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;An execution context contains at least three properties: a variable object, a scope chain, and thisBinding. &lt;/p&gt;

&lt;h4&gt;
  
  
  Variable Object
&lt;/h4&gt;

&lt;p&gt;The variable object contains the variables and functions declared inside of the current execution context, and if this execution context was created by a function it also contains the function arguments. This is when hoisting happens, &lt;a href="https://dev.to/javila35/hoisting-4b80"&gt;you can read my article about that&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Scope Chain
&lt;/h4&gt;

&lt;p&gt;Contains the current variable object as well as the variable object of the outer context &lt;em&gt;(execution context on the stack one below the current execution context)&lt;/em&gt; variable object. If the current execution context is the global execution context there is no outer context.&lt;/p&gt;

&lt;h4&gt;
  
  
  thisBinding
&lt;/h4&gt;

&lt;p&gt;Gives the current execution context access to &lt;code&gt;this&lt;/code&gt; keyword.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Shuffle the Array (Javascript)</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Wed, 26 Aug 2020 23:56:07 +0000</pubDate>
      <link>https://dev.to/javila35/shuffle-the-array-javascript-4jd7</link>
      <guid>https://dev.to/javila35/shuffle-the-array-javascript-4jd7</guid>
      <description>&lt;p&gt;Algorithms are something I struggle with. A few of the interviews or screening processes for companies I've done recently have involved algorithms. Being eliminated before even getting to speak to someone is very discouraging. In response, I'm being intentional about solving algorithms recently. This time I'll be tackling &lt;a href="https://leetcode.com/contest/weekly-contest-192/problems/shuffle-the-array/"&gt;this Leetcode problem&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0MYv61yrzZu6roFG/source.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0MYv61yrzZu6roFG/source.gif" alt="Snoopy shuffling"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given the array nums consisting of 2n elements in the form&lt;br&gt;
[x1,x2,...,xn,y1,y2,...,yn].&lt;br&gt;
Return the array in the form [x1,y1,x2,y2,...,xn,yn].&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My job is to shuffle the elements of the array together like a deck of cards. This problem is simple with Javascripts &lt;code&gt;.push()&lt;/code&gt; method.&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;var&lt;/span&gt; &lt;span class="nx"&gt;shuffle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&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;arr&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;n&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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="nx"&gt;nums&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="nx"&gt;n&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;arr&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;ul&gt;
&lt;li&gt;Declare an empty array &lt;code&gt;arr&lt;/code&gt; which will be my return value.&lt;/li&gt;
&lt;li&gt;Begin a &lt;code&gt;for&lt;/code&gt; loop that will run until the index is greater than &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In each iteration: 

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;push()&lt;/code&gt; the current index &lt;code&gt;nums[i]&lt;/code&gt; and &lt;code&gt;nums[i + n]&lt;/code&gt; into the return array &lt;code&gt;arr&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After iterating &lt;code&gt;n&lt;/code&gt; times return our new array. &lt;/p&gt;




&lt;p&gt;The difficulty was increased when I approached it without &lt;code&gt;push()&lt;/code&gt;. I was having trouble keeping track of the indexes I need access to, so I wrote it out on a whiteboard. After writing it out I was able to find the pattern. Stepping through every iteration really cleared up what counters I needed. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CItw8KWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rc9bzvgsicwktr4l1fz.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CItw8KWx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1rc9bzvgsicwktr4l1fz.JPG" alt="Whiteboard of my solution"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I ended up with four variables &lt;code&gt;i, y, x, b&lt;/code&gt;. &lt;code&gt;i&lt;/code&gt; pointed to the first index for the return array of my code block. &lt;code&gt;y&lt;/code&gt; pointed to the second index for the return array. &lt;strong&gt;These two variables were incremented by 2 each iteration.&lt;/strong&gt; &lt;code&gt;x&lt;/code&gt; pointed to the first index for the input array. &lt;code&gt;b&lt;/code&gt; pointed to the second index for the input array. &lt;strong&gt;These two variables were incremented by 1 each iteration.&lt;/strong&gt; To catch the pattern I really needed to be thorough. After that I was quickly able to deduce what I needed to do. This was my final solution:&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;var&lt;/span&gt; &lt;span class="nx"&gt;shuffle&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;n&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;arr&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;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;x&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;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;x&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;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="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&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="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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;n&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="mi"&gt;2&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;arr&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;ul&gt;
&lt;li&gt;Declare an empty array &lt;code&gt;arr&lt;/code&gt; which will be my return value.&lt;/li&gt;
&lt;li&gt;Declare a counter &lt;code&gt;i&lt;/code&gt; outside of the loop that I'll increment differently.&lt;/li&gt;
&lt;li&gt;Begin a &lt;code&gt;for&lt;/code&gt; loop that will run until the index &lt;code&gt;x&lt;/code&gt; is greater than &lt;code&gt;n&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In each iteration:

&lt;ol&gt;
&lt;li&gt;Set &lt;code&gt;arr[i]&lt;/code&gt; to &lt;code&gt;nums[x]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Set &lt;code&gt;arr[i+1]&lt;/code&gt; to &lt;code&gt;nums[x+n]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Increment &lt;code&gt;i&lt;/code&gt; by 2&lt;/li&gt;
&lt;li&gt;Check if &lt;code&gt;x &amp;lt; n&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;Return the array.&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Max Consecutive Ones (Javascript)</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Fri, 14 Aug 2020 05:22:32 +0000</pubDate>
      <link>https://dev.to/javila35/max-consecutive-ones-javascript-ph2</link>
      <guid>https://dev.to/javila35/max-consecutive-ones-javascript-ph2</guid>
      <description>&lt;p&gt;Algorithms have been a struggle for me to learn. I don't particularly enjoy them (until I solve them). I've been told they're an important part of being a developer, especially when searching for your first job. I practice on Leetcode and I'll start posting my accepted submissions here.&lt;/p&gt;

&lt;p&gt;Here is my solution for &lt;a href="https://leetcode.com/problems/max-consecutive-ones/"&gt;this Leetcode problem.&lt;/a&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a binary array, find the maximum number of consecutive 1s in this array.&lt;br&gt;
Note:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The input array will only contain 0 and 1.&lt;/li&gt;
&lt;li&gt;The length of the input array is a positive integer and will not exceed 10,000&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;My job is to count the number of times the number 1 appears in a row and return the largest run of 1's. Just from reading the description using a &lt;code&gt;for&lt;/code&gt; loop to touch each entry in the array is my plan. I'll use two variables. One to keep track of the current count and the second, the return value, to keep track of the largest number of 1's in a row.  I'll call those &lt;code&gt;currentCount&lt;/code&gt; and &lt;code&gt;largestCount&lt;/code&gt; respectively.&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;var&lt;/span&gt; &lt;span class="nx"&gt;findMaxConsecutiveOnes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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;largestCount&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentCount&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="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;nums&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;nums&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;currentCount&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="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;currentCount&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;currentCount&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;largestCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;largestCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentCount&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;largestCount&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;Going through the array:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First I check if the element (&lt;code&gt;nums[i]&lt;/code&gt;) is 0.

&lt;ul&gt;
&lt;li&gt;If it is 0, I set &lt;code&gt;currentCount&lt;/code&gt; to 0. Because that ends any run I have going.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;If the element is not 0, I add one to &lt;code&gt;currentCount&lt;/code&gt;.

&lt;ul&gt;
&lt;li&gt;&lt;em&gt;This works because of the note supplied by Leetcode. The only elements in the array will only be 1 or 0.&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;Before ending the current iteration of the loop, I check and see if the &lt;code&gt;currentCount&lt;/code&gt; is greater than the &lt;code&gt;largestCount&lt;/code&gt;. If so, set that to &lt;code&gt;largestCount&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After iterating through the entire array return the &lt;code&gt;largestCount&lt;/code&gt;. &lt;/p&gt;




&lt;p&gt;Here is a refactor of this code using a ternary instead of an if/else statement.&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;var&lt;/span&gt; &lt;span class="nx"&gt;findMaxConsecutiveOnes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&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;largest&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;current&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="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;nums&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;nums&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;0&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;current&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;current&lt;/span&gt;&lt;span class="o"&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;current&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;largest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;largest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;current&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;largest&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;



</description>
      <category>javascript</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Validating a unique record</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Tue, 28 Jul 2020 07:05:27 +0000</pubDate>
      <link>https://dev.to/javila35/validating-a-unique-record-1e3</link>
      <guid>https://dev.to/javila35/validating-a-unique-record-1e3</guid>
      <description>&lt;p&gt;I'm building a stat tracking site for my local basketball league using a Rails API. I started with &lt;a href="https://dev.to/javila35/creating-a-custom-name-for-a-foreign-key-3d8i"&gt;a post about creating custom names&lt;/a&gt; (or aliases) in Rails. In this post I'll be sharing my experience creating a customized &lt;code&gt;uniqueness&lt;/code&gt; validation.&lt;/p&gt;

&lt;p&gt;Validations in general are an important piece of keeping a tidy database. Fortunately Rails comes with plenty built in. There's &lt;a href="https://guides.rubyonrails.org/active_record_validations.html"&gt;a very thorough guide&lt;/a&gt; on implementing validations.&lt;/p&gt;

&lt;h2&gt;
  
  
  My use case
&lt;/h2&gt;

&lt;p&gt;I keep track of the data for a game by the &lt;code&gt;Player&lt;/code&gt;, instead of storing all that information on the &lt;code&gt;Game&lt;/code&gt; table. To find the winner I tally up the points scored by each &lt;code&gt;Player&lt;/code&gt; on the &lt;code&gt;home_team&lt;/code&gt; and &lt;code&gt;away_team&lt;/code&gt; mentioned in my &lt;a href="https://dev.to/javila35/creating-a-custom-name-for-a-foreign-key-3d8i"&gt;last post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The validation I am creating will ensure that there is only one record (&lt;code&gt;GameStat&lt;/code&gt;) per &lt;code&gt;Player&lt;/code&gt;, per &lt;code&gt;Game&lt;/code&gt;. This custom validation ensures that a &lt;code&gt;Player&lt;/code&gt; only has one entry on each &lt;code&gt;Game&lt;/code&gt;. Without this check user error could cause problems for my front end (i.e. displaying an incorrect winner).&lt;/p&gt;

&lt;p&gt;The code below is simple. If you're familiar with ActiveRecord it may be easy to understand.&lt;br&gt;
&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;GameStat&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;:player&lt;/span&gt;
  &lt;span class="n"&gt;belongs_to&lt;/span&gt; &lt;span class="ss"&gt;:game&lt;/span&gt;
  &lt;span class="n"&gt;validates&lt;/span&gt; &lt;span class="ss"&gt;:game_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;uniqueness: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;scope: :player_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;message: &lt;/span&gt;&lt;span class="s1"&gt;'record already exists'&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;p&gt;On the 4th line I declare that a &lt;code&gt;Game&lt;/code&gt; should only have one &lt;code&gt;GameStat&lt;/code&gt; per &lt;code&gt;Player&lt;/code&gt; using the &lt;code&gt;scope:&lt;/code&gt; option. I also provide a custom message to give myself my users a clear idea of why their attempt at posting statistics is failing. &lt;em&gt;The message is not necessary as there is a provided default.&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;To explain in it a nontechnical way: every time I create a &lt;code&gt;GameStat&lt;/code&gt; I check to see if one exists with the same ID as the current game and player. If there is, an error is sent to the user.&lt;/p&gt;

&lt;p&gt;Explaining it technically &lt;em&gt;(pretty much the same)&lt;/em&gt;: Before the new &lt;code&gt;GameStat&lt;/code&gt; record is created, Rails runs a SQL query on the &lt;code&gt;GameStat&lt;/code&gt; table checking to see if a record of the same value (&lt;code&gt;game_id&lt;/code&gt;, and &lt;code&gt;player_id&lt;/code&gt; thanks to the scope addition) already exists. If the record exists, throw an error.&lt;/p&gt;

&lt;p&gt;There is one more step mentioned by the &lt;a href="https://guides.rubyonrails.org/active_record_validations.html#uniqueness"&gt;Rails guide&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Should you wish to create a database constraint to prevent possible violations of a uniqueness validation using the :scope option, you must create a unique index on both columns in your database.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I have to add an index to my &lt;code&gt;player_id&lt;/code&gt; and &lt;code&gt;game_id&lt;/code&gt; on my &lt;code&gt;GameStat&lt;/code&gt; table. I don't fully understand indexing yet. So I had to find some help. Thanks to google, I &lt;a href="https://stackoverflow.com/a/34720645/5425583"&gt;found an answer on Stack Overflow&lt;/a&gt;.&lt;br&gt;
&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;AddUniqueIndexForGameStat&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Migration&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;6.0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;change&lt;/span&gt;
    &lt;span class="n"&gt;add_index&lt;/span&gt; &lt;span class="ss"&gt;:game_stats&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:player_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:game_id&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="ss"&gt;unique: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Just like that I am no longer able to create duplicate records for a game, or player. &lt;/p&gt;

&lt;p&gt;Thanks for reading, please comment any questions or suggestions for future blog posts. If you have any good resources on learning more about index's I'd love to see them!&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>database</category>
    </item>
    <item>
      <title>Javascript Order of Operations</title>
      <dc:creator>Joe Avila</dc:creator>
      <pubDate>Thu, 23 Jul 2020 19:03:00 +0000</pubDate>
      <link>https://dev.to/javila35/javascript-order-of-operations-31an</link>
      <guid>https://dev.to/javila35/javascript-order-of-operations-31an</guid>
      <description>&lt;p&gt;If you're from the US, you may be familiar with the old saying, &lt;b&gt;Please Excuse My Dear Aunt Sally&lt;/b&gt; or &lt;b&gt;PEMDAS&lt;/b&gt;. It represents the order of operations or operator precedence of mathematic equations.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parentheses&lt;/li&gt;
&lt;li&gt;Exponents&lt;/li&gt;
&lt;li&gt;Multiplication/Division&lt;/li&gt;
&lt;li&gt;Addition/Subtraction&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Programming languages have expanded version on these rules, but they follow a similar pattern. Just without the famous phrase. &lt;/p&gt;

&lt;p&gt;If you've ever declared a variable, then you've already interacted with Javascripts operator precedence. You can look at the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table"&gt;operator precedence table at MDN docs&lt;/a&gt;. This table is broken down into sections with a number tied to each operators precedence. The important part to grasp is: &lt;b&gt;the larger number is parsed first&lt;/b&gt;.&lt;/p&gt;




&lt;h2&gt;Let's look at some examples&lt;/h2&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;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt; &lt;span class="o"&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;How many operators do you see in the example above? There are two obvious operators (&lt;b&gt;+&lt;/b&gt; &amp;amp; &lt;b&gt;*&lt;/b&gt;) but there's a third less obvious operator: the assignment operator &lt;b&gt;=&lt;/b&gt;. &lt;/p&gt;

&lt;p&gt;The order of operations in this line of code is pretty easy to follow. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There's a declaration of a variable named `a`.&lt;/li&gt;
&lt;li&gt;`a` points to a math equation.&lt;/li&gt;
&lt;li&gt;JS calculates the equation:
  &lt;ol&gt;
  &lt;li&gt; 6 * 3 happens. Multiplication part of PEMDAS, or 
     precedence power 15 on the table.&lt;/li&gt;
  &lt;li&gt;5 + 18 happens. Addition part of PEMDAS, or precedence 
     power 14.
  &lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;The assignment operator, with precedence power of 3, resolves assigning a value of 23 to our variable &lt;code&gt;a&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;I'll add a comparative operator (precedence power: 12) in the next example, as well as some parentheses (power: 21). I'll also introduce associativity. From the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Precedence_And_Associativity"&gt;MDN docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The difference in associativity comes into play when there are multiple operators of the same precedence.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'll briefly cover what happens in less detail. Pay attention to step two where we encounter associativity. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Parentheses has the highest precedence: 16 - 2.&lt;/li&gt;
&lt;li&gt;Addition and subtraction both go next, in their associative order: left-to-right
&lt;ol&gt;
&lt;li&gt; 14 + 4 &lt;/li&gt;
&lt;li&gt; 1 + 5 &lt;/li&gt;
&lt;/ol&gt;




&lt;/li&gt;

&lt;li&gt;Now our logical operator: 18 &amp;gt; 6&lt;/li&gt;

&lt;li&gt;Finally, b is assigned the value of true. 18 is greater than 6.&lt;/li&gt;

&lt;/ol&gt;




&lt;p&gt;The above examples don't stray too far from math, and should be easy to understand. There are plenty of edge cases involving operator precedence, probably lots that I haven't even encountered yet. &lt;/p&gt;

&lt;p&gt;One tricky example is declaring variables in succession. This just involves an understanding of associativity.&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;const&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;second&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// #=&amp;gt; output: 10, 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The assignment operator resolves from right to left, so the first line could be rewritten as: &lt;code&gt;const first = (second = 10)&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;I have a lot to learn about operator precedence, but I hope that this post clearly explains some javascript fundamentals for someone trying to learn. &lt;/p&gt;

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