<?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: Vince Campanale</title>
    <description>The latest articles on DEV Community by Vince Campanale (@vincecampanale).</description>
    <link>https://dev.to/vincecampanale</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%2F21445%2Fb2874282-cb15-4bae-8be1-a31842962f79.jpeg</url>
      <title>DEV Community: Vince Campanale</title>
      <link>https://dev.to/vincecampanale</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vincecampanale"/>
    <language>en</language>
    <item>
      <title>Using Recursion to Loop in Elm</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Tue, 10 Jul 2018 03:08:20 +0000</pubDate>
      <link>https://dev.to/vincecampanale/using-recursion-to-loop-in-elm-19pf</link>
      <guid>https://dev.to/vincecampanale/using-recursion-to-loop-in-elm-19pf</guid>
      <description>&lt;p&gt;This post is centered on the following problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Find the difference between the square of the sum and the sum of the squares of the first N natural numbers.

The square of the sum of the first ten natural numbers is (1 + 2 + ... + 10)² = 55² = 3025.

The sum of the squares of the first ten natural numbers is 1² + 2² + ... + 10² = 385.

Hence the difference between the square of the sum of the first ten natural numbers and the sum of the squares of the first ten natural numbers is 3025 - 385 = 2640.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Credit for the problem goes to &lt;a href="//exercism.io/exercises/elm/difference-of-squares/readme"&gt;exercism.io&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The plan is to first solve it with a &lt;code&gt;for&lt;/code&gt; loop in Javascript, then solve it with recursion in Javascript, and finally translate the recursive solution to Elm.&lt;/p&gt;

&lt;h3&gt;
  
  
  With a &lt;code&gt;for&lt;/code&gt; Loop
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop solution, in barely-pseudocode looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- get the square of the sum of n by:
  -- going from 1 to n
  -- and adding each number to a total
-- return the total after the loop is done

-- get the sum of the squares of n by:
  -- going from 1 to n
  -- and adding the square of each number to a total
-- return the total after the loop is done

-- subtract the latter from the former
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Translated to Javascript, we get this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;squareOfSum&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&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;1&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;number&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;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;sum&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;1&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;number&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;sum&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&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="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;sum&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;difference&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;return&lt;/span&gt; &lt;span class="nx"&gt;squareOfSum&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="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="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;difference&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="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;2640&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks to my extensive test suite, I can confidently refactor and use recursion instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  In Order to Understand Recursion...
&lt;/h3&gt;

&lt;p&gt;The recursive equivalent of the above solution goes like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- get the square of the sum of n by:
  -- getting the triangular number for n by:
    -- returning 0 if n is 0
    -- adding n to the triangular number of n - 1

-- get the sum of the squares of n by:
  -- returning 0 if n is 0
  -- adding the square of n to the sum of the squares of n - 1

-- subtract the latter from the former
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, recursion is acting as a different way of looping by defining an action for each number &lt;code&gt;n&lt;/code&gt; down to 1 and a final action to end the loop when &lt;code&gt;n&lt;/code&gt; gets to 0.&lt;/p&gt;

&lt;p&gt;I googled "factorial with adding instead of multiplying" and found &lt;a href="https://en.wikipedia.org/wiki/Triangular_number"&gt;"triangular numbers"&lt;/a&gt;, so the function for calculating the sum of positive integers from 1 to &lt;code&gt;N&lt;/code&gt; is called &lt;code&gt;triangulate&lt;/code&gt; 🤷🏻‍♂️.&lt;/p&gt;

&lt;p&gt;Let's write that function first:&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;function&lt;/span&gt; &lt;span class="nx"&gt;triangulate&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;return&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;triangulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// which can be simplified to:&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;triangulate&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&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="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;triangulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the triangulate function, we can get the &lt;code&gt;squareOfSum&lt;/code&gt; 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="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;squareOfSum&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;sum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;triangulate&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;sumOfSquares&lt;/code&gt; function can also use recursion:&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;function&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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;return&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="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&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="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="nx"&gt;sumOfSquares&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// again, can be reduced to..&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;n&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="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&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="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="nx"&gt;sumOfSquares&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One final thought on the Javascript solution is to make &lt;code&gt;triangulate&lt;/code&gt; a bit more generic and add a second parameter for an exponent.&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;triangulate&lt;/span&gt; &lt;span class="o"&gt;=&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="nx"&gt;exp&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;=&amp;gt;&lt;/span&gt; 
  &lt;span class="nx"&gt;n&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="mi"&gt;0&lt;/span&gt;
  &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&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="nx"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;triangulate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&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="nx"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then &lt;code&gt;sumOfSquares&lt;/code&gt; can be written as the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;triangulate&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="mi"&gt;2&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;
  
  
  How about some Elm?
&lt;/h3&gt;

&lt;p&gt;Elm doesn't have &lt;code&gt;for&lt;/code&gt; loops. Whaaaaa&lt;/p&gt;

&lt;p&gt;Yea, for real.&lt;/p&gt;

&lt;p&gt;Luckily, we already know this problem can be solved without a &lt;code&gt;for&lt;/code&gt; loop. So what's the Elm equivalent of the recursive solution above? Well, let's refactor &lt;code&gt;sumOfSquares&lt;/code&gt; just &lt;em&gt;one&lt;/em&gt; more time in Javascript, using a switch statement with only two cases this time.&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;function&lt;/span&gt; &lt;span class="nx"&gt;sumOfSquares&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="k"&gt;switch&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="k"&gt;case&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;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&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="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="nx"&gt;sumOfSquares&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elm has a &lt;code&gt;case&lt;/code&gt; statement, so a nearly equivalent function will work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
    &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="n"&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="n"&gt;n&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="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can apply a similar approach to &lt;code&gt;squareOfSum&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="k"&gt;let&lt;/span&gt;
    &lt;span class="n"&gt;triangulate&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;
          &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
          &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;triangulate&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;in&lt;/span&gt; 
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;triangulate&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then the final function &lt;code&gt;difference&lt;/code&gt; is just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;difference&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;difference&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And voila, we have solved a &lt;code&gt;for&lt;/code&gt;-loop-friendly problem in Elm, a language with no &lt;code&gt;for&lt;/code&gt; loop. &lt;/p&gt;

&lt;h3&gt;
  
  
  A Better Way?
&lt;/h3&gt;

&lt;p&gt;While we &lt;em&gt;can&lt;/em&gt; use recursion to loop through the numbers between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;N&lt;/code&gt;, we can also make use of other utilities exposed in &lt;a href="http://package.elm-lang.org/packages/elm-lang/core/latest/"&gt;Elm Core&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For example, &lt;code&gt;List.range&lt;/code&gt; and &lt;code&gt;List.sum&lt;/code&gt; make this problem much easier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt; &lt;span class="k"&gt;exposing&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;range&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;


&lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;^&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;


&lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt;


&lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;range&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;map&lt;/span&gt; &lt;span class="n"&gt;square&lt;/span&gt; &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sum&lt;/span&gt;


&lt;span class="n"&gt;difference&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="n"&gt;difference&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;squareOfSum&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sumOfSquares&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since &lt;code&gt;for&lt;/code&gt; loops are one of the first things we learn as programmers, it's easy to fall back on &lt;code&gt;for&lt;/code&gt; loops in solutions to everyday problems. Using Elm has taught me that &lt;code&gt;for&lt;/code&gt; loops aren't necessary most of the time and seeking a different solution can lead to more declarative and readable code.&lt;/p&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

</description>
      <category>elm</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Think Before You Test: A Guide to Writing Effective Unit Tests for Custom Events in Angular</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Sat, 31 Mar 2018 14:25:43 +0000</pubDate>
      <link>https://dev.to/vincecampanale/think-before-you-test-a-guide-to-writing-effective-unit-tests-for-custom-events-in-angular-3d8i</link>
      <guid>https://dev.to/vincecampanale/think-before-you-test-a-guide-to-writing-effective-unit-tests-for-custom-events-in-angular-3d8i</guid>
      <description>&lt;p&gt;Angular was built for testability. Powerful tools like dependency injection, the &lt;code&gt;TestBed&lt;/code&gt; API, and out-of-the-box integration with Jasmine give us the power to test our Angular apps thoroughly and reliably. The catch is that learning these API's can take some time. Throw in a mix of Angular, Jasmine, and RxJS jargon and it can be a real uphill battle to feel comfortable testing the hairier parts of your application, which are the most important parts to test of course. In this post, I'll cover a couple different approaches you can take to testing custom events in Angular. If this is helpful or interesting to you, you can check out my &lt;a href="https://twitter.com/_vincecampanale"&gt;twitter page&lt;/a&gt;, where I share similar content. Also, here's a &lt;a href="https://stackblitz.com/edit/testing-event-emitter"&gt;link to the Stackblitz app&lt;/a&gt; I used when drafting this post. It may come in handy.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is an &lt;code&gt;@Output&lt;/code&gt; property?
&lt;/h2&gt;

&lt;p&gt;An &lt;code&gt;@Output&lt;/code&gt; property is an Angular utility used to create custom events. An &lt;code&gt;@Output&lt;/code&gt; is an &lt;a href="https://angular.io/api/core/EventEmitter"&gt;&lt;code&gt;EventEmitter&lt;/code&gt;&lt;/a&gt;, meaning it has two methods: &lt;code&gt;emit&lt;/code&gt; and &lt;code&gt;subscribe&lt;/code&gt;. You probably won't need to &lt;code&gt;subscribe&lt;/code&gt; to it directly, since Angular handles that with it's event binding syntax (e.g. &lt;code&gt;&amp;lt;div (someEvent)="onSomeEvent()"&amp;gt;&amp;lt;/div&amp;gt;&lt;/code&gt;). The &lt;code&gt;emit&lt;/code&gt; method allows you to notify the parent of an event and pass data up.&lt;/p&gt;

&lt;h2&gt;
  
  
  What should a unit test for a custom event do?
&lt;/h2&gt;

&lt;p&gt;When the component you are testing is responsible for emitting the custom event (the child component), the unit test should target two things: 1) the &lt;code&gt;@Output&lt;/code&gt; property's &lt;code&gt;emit&lt;/code&gt; method is invoked when it should be and 2) the &lt;code&gt;emit&lt;/code&gt; method is emitting the expected data.&lt;/p&gt;

&lt;p&gt;When testing the component listening to the &lt;code&gt;@Output&lt;/code&gt; (the parent / container component), the unit test should check that the emitted data is handled correctly (e.g. passed to the correct method).&lt;/p&gt;

&lt;h2&gt;
  
  
  The component
&lt;/h2&gt;

&lt;p&gt;The example child component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'counter',
  template: `
    &amp;lt;div&amp;gt;
      &amp;lt;button (click)="onClick()"&amp;gt;1&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  `
})
export class CounterComponent {
  @Output() change = new EventEmitter&amp;lt;number&amp;gt;();

  onClick() {
    this.change.emit(1);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;change&lt;/code&gt; property is the &lt;code&gt;EventEmitter&lt;/code&gt; to be tested. &lt;/p&gt;

&lt;p&gt;We listen for change events in &lt;code&gt;AppComponent&lt;/code&gt; to increment a counter by the amount emitted:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Component({
  selector: 'my-app',
  template: `
  &amp;lt;counter (change)="onChange($event)"&amp;gt;&amp;lt;/counter&amp;gt;
  `
})
export class AppComponent  {
  count = 0;

  onChange(event: number): void {
    this.count += event;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Testing the child
&lt;/h2&gt;

&lt;p&gt;First, we'll do some set up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('CounterComponent', () =&amp;gt; {
  let fixture: ComponentFixture&amp;lt;CounterComponent&amp;gt;;
  let component: CounterComponent;
  let de: DebugElement;
  let button: ElementRef;

  beforeEach(() =&amp;gt; {
    TestBed.configureTestingModule({
      declarations: [CounterComponent]
    });
  });

  beforeEach(() =&amp;gt; {
    fixture = TestBed.createComponent(CounterComponent);
    component = fixture.componentInstance;
    de = fixture.debugElement;
    button = de.query(By.css('button'));
  });
});

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

&lt;/div&gt;



&lt;p&gt;I won't go into the details of how this set up works, as it's outside the scope of this post. &lt;a href="https://angular.io/guide/testing"&gt;Angular's testing tutorial&lt;/a&gt; is a great resource to learn more about it. What matters is that we can test everything we need to test using &lt;code&gt;component&lt;/code&gt; and &lt;code&gt;button&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Every custom &lt;code&gt;@Output&lt;/code&gt; must get triggered by another event. Whether that event be a click in the DOM, a response from the server, a custom event on yet another nested child component, there must be a cause for the &lt;code&gt;emit&lt;/code&gt; method to be invoked. The first step is to mock that cause and ensure the &lt;code&gt;EventEmitter&lt;/code&gt; actually emits.&lt;/p&gt;

&lt;p&gt;We know from the component code that a click event on the button should make the &lt;code&gt;onClick()&lt;/code&gt; method run. The &lt;code&gt;change&lt;/code&gt; property's &lt;code&gt;emit&lt;/code&gt; method should be called when &lt;code&gt;onClick()&lt;/code&gt; executes. We can get &lt;code&gt;onClick()&lt;/code&gt; to execute in two ways: mock a &lt;code&gt;click&lt;/code&gt; on the button, or just call &lt;code&gt;component.onClick()&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;Here is one of many ways to mock a &lt;code&gt;click&lt;/code&gt; on the button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button.nativeElement.click();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to detect when the &lt;code&gt;@Output&lt;/code&gt; will emit, we can create a spy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spyOn(component.change, 'emit');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you have everything you need to effectively test the &lt;code&gt;@Output&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A unit test might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('change', () =&amp;gt; {
  it('should emit when the button is clicked', () =&amp;gt; {
    spyOn(component.change, 'emit');
    button.nativeElement.click();
    expect(component.change.emit).toHaveBeenCalled();
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it. Now, let's target goal #2: ensure the &lt;code&gt;@Output&lt;/code&gt; is emitting the expected data to the parent. &lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;toHaveBeenCalledWith()&lt;/code&gt;, we can kill two birds with one stone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('change', () =&amp;gt; {
  it('should emit when the button is clicked', () =&amp;gt; {
    spyOn(component.change, 'emit');
    button.nativeElement.click();
    expect(component.change.emit).toHaveBeenCalledWith(1);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in one unit test, you are ensuring that the &lt;code&gt;emit&lt;/code&gt; method is being called when it should be and that it is emitting the correct data. There are a couple of other ways to accomplish this, which are worth mentioning.&lt;/p&gt;

&lt;p&gt;I think it's safe to say that Angular has &lt;code&gt;click&lt;/code&gt; events down, so we don't need to worry about that not working as expected. Because of this, it is safe to call the &lt;code&gt;onClick()&lt;/code&gt; method directly, instead of mocking a click on the button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('change', () =&amp;gt; {
  it('should emit when the button is clicked', () =&amp;gt; {
    spyOn(component.change, 'emit');
    component.onClick();
    expect(component.change.emit).toHaveBeenCalledWith(1);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a bit easier because we don't have to worry about querying the &lt;code&gt;DebugElement&lt;/code&gt; or mocking click events, we just call the method directly and trust Angular to handle the rest.&lt;/p&gt;

&lt;p&gt;A final approach to testing the &lt;code&gt;EventEmitter&lt;/code&gt; is to actually subscribe to it and trigger the event, making your assertion in the subscribe block.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('change', () =&amp;gt; {
  it('should emit when the button is clicked', () =&amp;gt; {
    component.change.subscribe(next =&amp;gt; {
      expect(next).toEqual(1);
    });

    component.onClick(); // or button.nativeElement.click()
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I don't recommend this approach for a couple of reasons:  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It's weird. Typically, a unit test makes its assertion(s) at the &lt;em&gt;end&lt;/em&gt;. This approach breaks that pattern and will cause future developers to have to look sideways and squint to understand how the test works. Unit tests should be easy to read and understand.
&lt;/li&gt;
&lt;li&gt;Order of statements matters. If you call &lt;code&gt;component.onClick()&lt;/code&gt; before subscribing to the &lt;code&gt;change&lt;/code&gt; emitter, you won't get into the subscribe block and make the assertion. This is made even worse by the fact that your test will pass! A faulty, passing test is worse than no test at all.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Testing the parent
&lt;/h1&gt;

&lt;p&gt;We can take three approaches to testing the behavior of the &lt;code&gt;EventEmitter&lt;/code&gt; from the perspective of the parent (the component listening for the event):  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Invoke the &lt;code&gt;@Output&lt;/code&gt; property's &lt;code&gt;emit&lt;/code&gt; method (since the &lt;code&gt;EventEmitter&lt;/code&gt; is a public property)
&lt;/li&gt;
&lt;li&gt;Dig in to the counter's &lt;code&gt;DebugElement&lt;/code&gt; and simulate a click on the button&lt;/li&gt;
&lt;li&gt;Call the function directly (trust that Angular will work)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's what the set up looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('AppComponent', () =&amp;gt; {
  let fixture: ComponentFixture&amp;lt;AppComponent&amp;gt;;
  let component: AppComponent;
  let de: DebugElement;

  beforeEach(() =&amp;gt; {
    TestBed.configureTestingModule({
      declarations: [AppComponent, CounterComponent]
    });
  });

  beforeEach(() =&amp;gt; {
    fixture = TestBed.createComponent(AppComponent);
    component = fixture.componentInstance;
    de = fixture.debugElement;
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to invoke the &lt;code&gt;@Output&lt;/code&gt; property's &lt;code&gt;emit&lt;/code&gt; method, we had to declare the component with the &lt;code&gt;@Output&lt;/code&gt; in the testing module.&lt;/p&gt;

&lt;p&gt;Now, we can use the &lt;code&gt;DebugElement&lt;/code&gt; for the &lt;code&gt;AppComponent&lt;/code&gt; to get the counter component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('onChange', () =&amp;gt; { 
  it('should be called with whatever the counter change event emits', () =&amp;gt; {
    spyOn(component, 'onChange');
    const counter = de.query(By.directive(CounterComponent));
    const cmp = counter.componentInstance;
    cmp.change.emit(1);
    expect(component.onChange).toHaveBeenCalledWith(1);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above unit test, we spy on the &lt;code&gt;onChange&lt;/code&gt; method (the method that should be called when &lt;code&gt;change&lt;/code&gt; emits). Then, we query for the counter component fixture based on it's directive class and get the component itself through the &lt;code&gt;componentInstance&lt;/code&gt; property. &lt;em&gt;Now&lt;/em&gt;, we have access to the &lt;code&gt;change&lt;/code&gt; property and can tell it to &lt;code&gt;emit&lt;/code&gt; a value of &lt;code&gt;1&lt;/code&gt;. In order to test that we are handling the event correctly, we'll just check that the &lt;code&gt;onChange&lt;/code&gt; spy gets called with the value that the &lt;code&gt;change&lt;/code&gt; event emitted. This is overkill, but not nearly as overkill as the next test.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('onChange', () =&amp;gt; {
  it('should be called with whatever the counter change event emits', () =&amp;gt; {
    spyOn(component, 'onChange');
    const counter = de.query(By.directive(CounterComponent));
    const button = counter.query(By.css('button'));
    button.nativeElement.click();
    expect(component.onChange).toHaveBeenCalledWith(1); 
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we are querying the fixture of the child element for the actual, physical button and dispatching a &lt;code&gt;click&lt;/code&gt; event to the button. This &lt;code&gt;click&lt;/code&gt; event will fire off the chain reaction which should eventually lead to our &lt;code&gt;AppComponent&lt;/code&gt;'s &lt;code&gt;onChange&lt;/code&gt; method being called with the value emitted from the &lt;code&gt;change&lt;/code&gt; event. But wait, let's check in with what we're actually testing here. A unit test should be responsible for one &lt;em&gt;unit&lt;/em&gt; of functionality. The test we just wrote is testing 1) that the button's click works, 2) that Angular's handling of the click event works, 3) that our &lt;code&gt;onClick&lt;/code&gt; method in the &lt;code&gt;CounterComponent&lt;/code&gt; gets called with the correct data and makes the appropriate call the &lt;code&gt;change&lt;/code&gt; property's &lt;code&gt;emit&lt;/code&gt; method, 4) that Angular's handling of the &lt;code&gt;change&lt;/code&gt; event works, 5) that our &lt;code&gt;onChange&lt;/code&gt; method works. That's not a unit test. &lt;/p&gt;

&lt;p&gt;Now that you've seen all the crazy stuff you &lt;em&gt;can&lt;/em&gt; do with this powerful set of testing tools, you will be relieved to see what you actually &lt;em&gt;need&lt;/em&gt; to do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('onChange', () =&amp;gt; {
  it('should increment the count by the amount provided', () =&amp;gt; {
    component.count = 2;
    component.onChange(2);
    expect(component.count).toEqual(4);
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The only thing that needs to be tested on this end is the &lt;code&gt;onChange&lt;/code&gt; method itself. That's the only logic we wrote. Everything else is handled by Angular. Feel free to double check &lt;a href="https://github.com/angular/angular/blob/master/packages/core/test/event_emitter_spec.ts"&gt;the &lt;code&gt;EventEmitter&lt;/code&gt; tests&lt;/a&gt; if you're skeptical.&lt;/p&gt;

&lt;h1&gt;
  
  
  Takeaways
&lt;/h1&gt;

&lt;p&gt;Tests are good. We have a lot of powerful tools at our disposal for testing in Angular so it's easy to make sure our components work as they should. Finally, it's important to understand the difference between what we &lt;em&gt;can&lt;/em&gt; test and what actually needs to be tested.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>testing</category>
    </item>
    <item>
      <title>Elm Kata 3: Using Elm Core List and String Functions to Detect Anagrams</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Mon, 23 Oct 2017 17:13:46 +0000</pubDate>
      <link>https://dev.to/vincecampanale/elm-kata-3-using-elm-core-list-and-string-functions-to-detect-anagrams-1ba</link>
      <guid>https://dev.to/vincecampanale/elm-kata-3-using-elm-core-list-and-string-functions-to-detect-anagrams-1ba</guid>
      <description>&lt;p&gt;Welcome to the third installment of &lt;strong&gt;Elm Kata&lt;/strong&gt;, a series in which we learn the ins and outs of Elm by solving different exercises.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;Another day, another problem to solve. Call me Sherlock. Here's the problem statement we're working with, courtesy of exercism.io:  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given a word and a list of possible anagrams, select the correct sublist.&lt;br&gt;&lt;br&gt;
Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets". &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Round 1
&lt;/h3&gt;

&lt;p&gt;As per usual, I like to start out with a function signature so let's start there:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;detect : String -&amp;gt; List String -&amp;gt; List String
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We are going to create a function that takes a word and a list of words and returns a new list containing all the anagrams of the original word in the given list (if any).&lt;/p&gt;

&lt;p&gt;My first thought upon seeing this problem was "permutations." If I could generate a list of all the combinations of letters in the given word and check that list for each of the words in the given list of possible candidates, I'd end up with a list of anagrams.  &lt;/p&gt;

&lt;p&gt;I'll show that solution now, but keep in mind there's a much better way. Kudos if you can see it already, it took me the full process of solving the problem the hard way before I saw the easy way -- I feel like Confucius would nod knowingly if he heard me say that. &lt;/p&gt;

&lt;p&gt;Here's the first pass in all its glory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module Anagram exposing (..)

import List.Extra exposing (permutations)


getPermutationsOf : String -&amp;gt; List String
getPermutationsOf word =
    word
        |&amp;gt; String.toList
        |&amp;gt; List.Extra.permutations
        |&amp;gt; List.map String.fromList


isPermutationOf : String -&amp;gt; String -&amp;gt; Bool
isPermutationOf word other =
    let
        uppercaseWord =
            String.toUpper word

        uppercaseOther =
            String.toUpper other
    in
        not (uppercaseWord == uppercaseOther)
            &amp;amp;&amp;amp; List.member uppercaseOther (getPermutationsOf uppercaseWord)


detect : String -&amp;gt; List String -&amp;gt; List String
detect word possibleMatches =
    possibleMatches
        |&amp;gt; List.filter (isPermutationOf word)

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

&lt;/div&gt;



&lt;p&gt;Just a heads up, if you want to run this yourself you will need to run &lt;code&gt;elm-package install elm-community/list-extra&lt;/code&gt; to get access to the fancy permutations method. &lt;/p&gt;

&lt;p&gt;So, allow me to walk you through this solution. Let start with the final function and unravel the program step by step.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;detect&lt;/code&gt;, I filter the list of &lt;code&gt;possibleMatches&lt;/code&gt; for words that are a permutation of the word for which I am seeking anagrams.  In order to do this, I needed a helper function to tell me if a word is a permutation of another word, hence the second function in the program. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;isPermutationOf&lt;/code&gt; takes two words and returns whether or not the second word is present in the permutation list of the first one. First I convert the two words to upper case (to handle edge cases and weird characters), then I make sure that the two words are not the exact same (another edge case), then I check the permutation list for the second word. &lt;/p&gt;

&lt;p&gt;Getting the permutation list takes a bit of work as well, though. Hence, the first function in the program. &lt;code&gt;getPermutationsOf&lt;/code&gt; is where I actually use the &lt;code&gt;List.extra.permutations&lt;/code&gt; utility. I take the word, convert it to a list of characters, turn that into a list of lists (each list containing a possible order list of characters could be in) and then map over that list of lists, turning it back into a list of strings. &lt;/p&gt;

&lt;p&gt;The result is a very slow, very brute force, but very correct solution.&lt;/p&gt;

&lt;h3&gt;
  
  
  Round 2
&lt;/h3&gt;

&lt;p&gt;How did I not see it in the first place? Me-oh-my, this can be done much more cleanly.&lt;/p&gt;

&lt;p&gt;Instead of changing the original word to make the check easy for the list, why don't we change all the words in the list to make the check easy for the word?!&lt;/p&gt;

&lt;p&gt;How can we check that two words are the same, just with the letters all jumbled up? By sorting them and comparing their sorted forms!&lt;/p&gt;

&lt;p&gt;First, we need a sort function that will work for strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sortStr = String.toList &amp;gt;&amp;gt; List.sort &amp;gt;&amp;gt; String.fromList
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;&amp;gt;&amp;gt;&lt;/code&gt; was new for me -- it lets you compose functions together to make bigger, badder functions. It's similar to the pipe (&lt;code&gt;|&amp;gt;&lt;/code&gt;) operator, except you aren't passing in any arguments, you're just mashing the functions together in anticipation of a higher purpose.&lt;/p&gt;

&lt;p&gt;Now we need to put our &lt;code&gt;sortStr&lt;/code&gt; function to use in a function that will take two words and see if they are anagrams!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isAnagram word possibleMatch =
  not (String.toUpper word == String.toUpper possibleMatch)
    &amp;amp;&amp;amp; sortStr (String.toUpper word) 
    == sortStr (String.toUpper possibleMatch)        
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's use our helpers to make a clever &lt;code&gt;detect&lt;/code&gt; function that makes our old &lt;code&gt;detect&lt;/code&gt; function look like baby food.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;detect : String -&amp;gt; List String -&amp;gt; List String
detect word possibleMatches =
    let
        sortStr =
            String.toList &amp;gt;&amp;gt; List.sort &amp;gt;&amp;gt; String.fromList

        isAnagram possibleMatch =
            not (String.toUpper word == String.toUpper possibleMatch)
                &amp;amp;&amp;amp; sortStr (String.toUpper word)
                == sortStr (String.toUpper possibleMatch)
    in
        List.filter isAnagram possibleMatches
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom, there ya have it.&lt;/p&gt;

&lt;p&gt;Follow &lt;a href="https://twitter.com/_vincecampanale"&gt;@_vincecampanale&lt;/a&gt; to get updates on future installments of the &lt;strong&gt;Elm Kata&lt;/strong&gt; series. Also, it will make me feel good about myself.&lt;/p&gt;

&lt;p&gt;Thanks for your time &amp;lt;3, hope it was worth it.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>learning</category>
    </item>
    <item>
      <title>Scoring Scrabble Words with Elm</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Tue, 10 Oct 2017 13:42:41 +0000</pubDate>
      <link>https://dev.to/vincecampanale/solving-cleobuluss-problem-with-elm-afi</link>
      <guid>https://dev.to/vincecampanale/solving-cleobuluss-problem-with-elm-afi</guid>
      <description>&lt;p&gt;&lt;a title="By Gortyna (Own work) [CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0)], via Wikimedia Commons" href="https://commons.wikimedia.org/wiki/File%3ACleobulus_Rhodes.JPG"&gt;&lt;img width="256" alt="Cleobulus Rhodes" src="https://res.cloudinary.com/practicaldev/image/fetch/s--rBRQbsZa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Cleobulus_Rhodes.JPG/256px-Cleobulus_Rhodes.JPG"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem
&lt;/h3&gt;

&lt;p&gt;In approximately 6th century B.C., Cleobulus of Lindos, one of the Seven Sages of Greece, presented to the world a word-based mind game&lt;sup&gt;1&lt;/sup&gt;. He called it "Scrabble". When outlining his game, he omitted an important part, perhaps on purpose -- how to compute the score of a word. A universal solution has yet to arise. &lt;/p&gt;

&lt;h3&gt;
  
  
  Assumptions
&lt;/h3&gt;

&lt;p&gt;In order to provide a reasonable solution that will hold in this dimension, we must first make some assumptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) The letters A, E, I, O, U, L, N, R, S, and T have the value 1.
2) The letters D and G have the value 2.  
3) The letters B, C, M, and P have the value 3.  
4) The letters F, H, V, W, and Y have the value 4.  
5) The letter K has the value 5.  
6) The letters J and X have the value 8.  
7) The letters Q and Z have the value 10.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A Solution
&lt;/h3&gt;

&lt;p&gt;Let us begin.  &lt;/p&gt;

&lt;p&gt;Breaking the problem down and outlining a solution in pseudocode is good for simple scripting problems like this one, so let's start there.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;-- given a word
-- split the word up into letters
-- get the points value for each letter
-- store / record the point values for each letter
-- add up all the point values
-- return the sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I find it helpful to start with the type signature when constructing functions in Elm.  The function we want to build will take a word and return a score. In terms the compiler can understand, it will take a &lt;code&gt;String&lt;/code&gt; and return an &lt;code&gt;Int&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord: String -&amp;gt; Int
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, we can start building our function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord: String -&amp;gt; Int
scoreWord word = 
   word
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, so splitting the word up into letters is easy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord: String -&amp;gt; Int
scoreWord word = 
  String.split "" word
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to get the points value for each letter. This introduces a bit of complexity into our problem -- where do the point values come from? Elm doesn't know what a letter is worth. One way to tell it is to make a record, a series of key-value pairs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pointsCategories =
    { one = [ "A", "E", "I", "O", "U", "L", "N", "R", "S", "T" ]
    , two = [ "D", "G" ]
    , three = [ "B", "C", "M", "P" ]
    , four = [ "F", "H", "V", "W", "Y" ]
    , five = [ "K" ]
    , eight = [ "J", "X" ]
    , ten = [ "Q", "Z" ]
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each key of our record is the word version of the point value. Each value is a list containing every letter that has that point value. Records in Elm cannot have number literals for keys, so this is the closest we can get.&lt;/p&gt;

&lt;p&gt;Now, we need a function that will score a letter by looking up it's point value in the &lt;code&gt;pointsCategories&lt;/code&gt; record and converting that to a number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getPointsForLetter : String -&amp;gt; Int
getPointsForLetter letter =
    if List.member letter pointsCategories.one then
        1
    else if List.member letter pointsCategories.two then
        2
    else if List.member letter pointsCategories.three then
        3
    else if List.member letter pointsCategories.four then
        4
    else if List.member letter pointsCategories.five then
        5
    else if List.member letter pointsCategories.eight then
        8
    else if List.member letter pointsCategories.ten then
        10
    else
        0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, this function is a little (very) redundant. I'm starting to get the feeling that this program is a wee bit smelly and could use some tidying up, but let's get this solution working before we refactor.&lt;/p&gt;

&lt;p&gt;This would be a good time to note that all the letters in our &lt;code&gt;pointsCategories&lt;/code&gt; record are uppercase, but we do not have that guarantee for a given word, unless you're playing a very loud version of Cleobulus's game.&lt;/p&gt;

&lt;p&gt;In order to address this, we might as well make all of our letters uppercase, since that's how they appear in the &lt;code&gt;pointsCategories&lt;/code&gt; record -- WITH ENERGY!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord : String -&amp;gt; Int
scoreWord word =
  List.map String.toUpper (String.split "" word)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we have a list of uppercase letters to work with so we can use our &lt;code&gt;getPointsValue&lt;/code&gt; function to convert letters to points.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord : String -&amp;gt; Int
scoreWord word = 
  List.map getPointsForLetter (List.map String.toUpper (String.split "" word))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These parentheses are getting out of hand -- time to take advantage of the Elm's brilliant pipe operator to make our solution readable.&lt;/p&gt;

&lt;p&gt;The pipe operator is nice because it turns&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;f(g(h(i(x))))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;into&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x 
  |&amp;gt; i 
  |&amp;gt; h 
  |&amp;gt; g 
  |&amp;gt; f
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;where &lt;code&gt;x&lt;/code&gt; is some value and &lt;code&gt;i&lt;/code&gt;, &lt;code&gt;h&lt;/code&gt;, &lt;code&gt;g&lt;/code&gt;, and &lt;code&gt;f&lt;/code&gt; are functions.&lt;/p&gt;

&lt;p&gt;Applying this nifty tool to our solution, we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord : String -&amp;gt; Int
scoreWord word =
  word 
    |&amp;gt; String.split ""
    |&amp;gt; List.map String.toUpper
    |&amp;gt; List.map getPointsForLetter
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The astute reader will notice we are not yet returning an &lt;code&gt;Int&lt;/code&gt;, but rather a list of &lt;code&gt;Int&lt;/code&gt;'s (or &lt;code&gt;List Int&lt;/code&gt; using Elm syntax). Let's fix that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord : String -&amp;gt; Int
scoreWord word = 
  word 
    |&amp;gt; String.split ""
    |&amp;gt; List.map String.toUpper
    |&amp;gt; List.map getPointsForLetter
    |&amp;gt; List.sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution works, but I detest the repetitive chain of &lt;code&gt;if else if&lt;/code&gt;'s in the &lt;code&gt;getPointsForLetter&lt;/code&gt; function. Unfortunately, this is the crux of the solution, so this refactor is going to be a big one!&lt;/p&gt;

&lt;h3&gt;
  
  
  All Aboard the Refactor Tractor
&lt;/h3&gt;

&lt;p&gt;The main issue with the above solution is the fact that we can't use a number literal as the key in our &lt;code&gt;pointsCategories&lt;/code&gt; record. This means we are separating looking up the value and converting it to an &lt;code&gt;Int&lt;/code&gt;. We will need to reach into our toolbox and find a new data structure to use in order to solve this. I propose we use a dictionary (&lt;code&gt;Dict&lt;/code&gt;) to tie a number to a character. We need to import &lt;code&gt;Dict&lt;/code&gt; to use it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import Dict exposing (..)

pointValues : Dict Char Int
pointValues =
    Dict.fromList
        [ ( 'A', 1 )
        , ( 'E', 1 )
        , ( 'I', 1 )
        , ( 'O', 1 )
        , ( 'U', 1 )
        , ( 'L', 1 )
        , ( 'N', 1 )
        , ( 'R', 1 )
        , ( 'S', 1 )
        , ( 'T', 1 )
        , ( 'D', 2 )
        , ( 'G', 2 )
        , ( 'B', 3 )
        , ( 'C', 3 )
        , ( 'M', 3 )
        , ( 'P', 3 )
        , ( 'F', 4 )
        , ( 'H', 4 )
        , ( 'V', 4 )
        , ( 'M', 4 )
        , ( 'Y', 4 )
        , ( 'K', 5 )
        , ( 'J', 8 )
        , ( 'X', 8 )
        , ( 'Q', 10 )
        , ( 'Z', 10 )
        ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can access the point value for a letter using the &lt;code&gt;Dict.get&lt;/code&gt; method. This change of data structure is crucial because we have moved the redundancy out of our program and into the data itself. Redundancy in data is gravy, redundancy in code is gunk.&lt;/p&gt;

&lt;p&gt;Let's write a function that takes a character and returns it's point value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getPointValue : Char -&amp;gt; Int
getPointValue letter = 
    let
        letterScore =
            Dict.get letter pointValues
    in
    case letterScore of
        Just score -&amp;gt;
            score

        Nothing -&amp;gt;
            0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Dict.get&lt;/code&gt; is not guaranteed to return something since it can be called with a value that may not correspond to a key in the dictionary, like if we tried to get the point value of a Chinese character. In terms that Elm's compiler can understand, &lt;code&gt;Dict.get&lt;/code&gt; returns a &lt;code&gt;Maybe&lt;/code&gt; type, so we need to tell the program how to handle both cases of the &lt;code&gt;Maybe&lt;/code&gt; type -- &lt;code&gt;Just&lt;/code&gt; (we got something) and &lt;code&gt;Nothing&lt;/code&gt; (no luck).&lt;/p&gt;

&lt;p&gt;If we get a score back, then return that score. If not, default to 0.&lt;/p&gt;

&lt;p&gt;Time to rewrite &lt;code&gt;scoreWord&lt;/code&gt; so it splits our word into a list of &lt;code&gt;Char&lt;/code&gt;'s (&lt;code&gt;String.toList&lt;/code&gt;) and uses our new &lt;code&gt;getPointValue&lt;/code&gt; function to get the value of each character in the list.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scoreWord : String -&amp;gt; Int
scoreWord word =
    word
        |&amp;gt; String.toUpper
        |&amp;gt; String.toList
        |&amp;gt; List.map getPointValue
        |&amp;gt; List.sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This works, but I still think &lt;code&gt;getPointValue&lt;/code&gt; is a bit verbose. Let's use a couple tricks to make it easier to understand.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flip&lt;/code&gt; is a neat tool that lets you flip the order of arguments to a function. &lt;code&gt;Maybe.withDefault&lt;/code&gt; is a shorthand way to do what we did in our &lt;code&gt;case&lt;/code&gt; statement. Taking advantage of these two handy mechanisms and the pipe operator, we can write &lt;code&gt;getPointValue&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;getPointValue : Char -&amp;gt; Int
getPointValue letter =
  letter 
    |&amp;gt; flip Dict.get pointValues
    |&amp;gt; Maybe.withDefault 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ah, much better.&lt;/p&gt;

&lt;p&gt;To cap off the post, here's the entire script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module ScrabbleScore exposing (..)

import Dict exposing (..)


pointValues : Dict Char Int
pointValues =
    Dict.fromList
        [ ( 'A', 1 )
        , ( 'E', 1 )
        , ( 'I', 1 )
        , ( 'O', 1 )
        , ( 'U', 1 )
        , ( 'L', 1 )
        , ( 'N', 1 )
        , ( 'R', 1 )
        , ( 'S', 1 )
        , ( 'T', 1 )
        , ( 'D', 2 )
        , ( 'G', 2 )
        , ( 'B', 3 )
        , ( 'C', 3 )
        , ( 'M', 3 )
        , ( 'P', 3 )
        , ( 'F', 4 )
        , ( 'H', 4 )
        , ( 'V', 4 )
        , ( 'M', 4 )
        , ( 'Y', 4 )
        , ( 'K', 5 )
        , ( 'J', 8 )
        , ( 'X', 8 )
        , ( 'Q', 10 )
        , ( 'Z', 10 )
        ]


getPointValue : Char -&amp;gt; Int
getPointValue letter =
    letter
        |&amp;gt; flip Dict.get pointValues
        |&amp;gt; Maybe.withDefault 0


scoreWord : String -&amp;gt; Int
scoreWord word =
    word
        |&amp;gt; String.toUpper
        |&amp;gt; String.toList
        |&amp;gt; List.map getPointValue
        |&amp;gt; List.sum
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While we can never know exactly how Cleobulus intended for a word to be scored, we can give our best guess and this is mine.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;If you like my style, &lt;a href="http://www.vincecampanale.com/blog/"&gt;check out my blog&lt;/a&gt; or follow me on twitter at &lt;a href="https://twitter.com/_vincecampanale"&gt;@_vincecampanale&lt;/a&gt; for occasional web development goodies in between memes and retweets :)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt; I made this up.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>elm</category>
      <category>beginners</category>
      <category>humor</category>
    </item>
    <item>
      <title>Developer Halloween Costumes</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Fri, 22 Sep 2017 13:04:54 +0000</pubDate>
      <link>https://dev.to/vincecampanale/developer-halloween-costumes</link>
      <guid>https://dev.to/vincecampanale/developer-halloween-costumes</guid>
      <description></description>
      <category>discuss</category>
    </item>
    <item>
      <title> I Found My People at a Programming Meetup</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Mon, 11 Sep 2017 14:03:58 +0000</pubDate>
      <link>https://dev.to/vincecampanale/i-found-my-people-at-a-programming-meetup</link>
      <guid>https://dev.to/vincecampanale/i-found-my-people-at-a-programming-meetup</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/http%3A%2F%2Fallfaithcenter.org%2Fwp-content%2Fuploads%2F2013%2F07%2FBirds-of-a-feather-flock-together-a5098s.jpg" 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/http%3A%2F%2Fallfaithcenter.org%2Fwp-content%2Fuploads%2F2013%2F07%2FBirds-of-a-feather-flock-together-a5098s.jpg" alt="Image found at: http://allfaithcenter.org/wp-content/uploads/2013/07/Birds-of-a-feather-flock-together-a5098s.jpg."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I recently took over Elm DC, a meetup in the D.C. Metro area centered on an interesting new functional language for the web called Elm.&lt;/p&gt;

&lt;p&gt;At the most recent meetup, I had the idea to wire up my Macbook to the big TV and work through a coding challenge in Elm on the premise that anyone interested could join in.&lt;/p&gt;

&lt;p&gt;The first few minutes were a bit awkward, as a room full of programmers may be, but not much time passed before people started looking up from their screens and chipping in an idea here, a comment there. Before I knew it, every single person in that room (there were about 7 of us) had rallied around the TV and was completely immersed in the challenge, some even standing up from their seats and moving closer to the screen.&lt;/p&gt;

&lt;p&gt;In that moment, I knew I had found my people. I picked the right profession after all. &lt;/p&gt;

&lt;p&gt;Being new to this industry, the feeling of being a "noob" is one I'm quite familiar with. But in that room, experience and knowledge didn't matter. Despite our many different backgrounds, personalities, and levels of experience, we were all on the same page -- we were programmers, problem solvers. Put a problem in front of us and we wouldn't have been able to stop our minds from going to work on that problem. &lt;/p&gt;

&lt;p&gt;As programmers, we gravitate towards unsolved problems and stare them in the face until they become solutions, then we go look for new problems to grapple with.&lt;/p&gt;

&lt;p&gt;As individuals, our effectiveness is determined by our unique blend of personality characteristics, mental strengths, and domain specific knowledge, but the spine of every programmer's mentality is the urge to solve problems. It doesn't matter how early or late we are in our career, what technologies and tools we happen to use in the moment, what we studied in college, whether we are trying to get a system to mimic the human brain or tracking down a TypeError -- the ability to identify problems and the drive to solve them powers everything we do and is the only reason we can do what we do (and do it well). It's a beautiful thing and I'm proud to be in the club.&lt;/p&gt;

&lt;p&gt;In the spirit of dev.to's culture of debate and discussion, I'm curious about your take on this. Do you see this quality within yourself? Did I miss the mark, hit the nail on the head, or somewhere in between? What other defining traits make you a programmer?&lt;/p&gt;

&lt;p&gt;Also if you like Elm, follow me on twitter at &lt;a href="https://twitter.com/_vincecampanale" rel="noopener noreferrer"&gt;@_vincecampanale&lt;/a&gt;. I post about Elm and Javascript and other programming-related things there.&lt;/p&gt;

</description>
      <category>elm</category>
      <category>webdev</category>
      <category>career</category>
      <category>discuss</category>
    </item>
    <item>
      <title> "Elm Has Me Leaping For Joy" </title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Mon, 28 Aug 2017 12:07:15 +0000</pubDate>
      <link>https://dev.to/vincecampanale/elm-has-me-leaping-for-joy</link>
      <guid>https://dev.to/vincecampanale/elm-has-me-leaping-for-joy</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Ffrontendmasters.com%2Fassets%2FElm.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%2Ffrontendmasters.com%2Fassets%2FElm.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I'm very new to Elm (a couple of weeks) and am working through exercises on exercism.io. I see a lot of potential in this language and am enjoying the learning process. Writing about what I learn usually helps me understand it better. Thus, this article was born.&lt;/p&gt;

&lt;p&gt;The exercise is simple: identify leap years with Elm. So, given a year, determine whether that year is a leap year or not. The rules are as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;leap years are on: 
- every year that is evenly divisible by 4
- except every year that is evenly divisible by 100
- unless the year is also evenly divisible by 400
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's start by writing some functions for checking these divisibility rules.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;divisibleBy4&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&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;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;divisibleBy100&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="n"&gt;divisibleBy400&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now for the boolean expression...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy4&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy100&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy400&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool. So now we can put the pieces together and build our function. &lt;br&gt;
But first, a function signature!&lt;/p&gt;

&lt;p&gt;We want a function that takes an integer (&lt;code&gt;Int&lt;/code&gt;) and returns a boolean (&lt;code&gt;Bool&lt;/code&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;isLeapYear&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bam.&lt;/p&gt;

&lt;p&gt;Now, we can add a &lt;code&gt;let-in&lt;/code&gt; expression to create a local function scope for our &lt;code&gt;divisibleByX&lt;/code&gt; helpers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;isLeapYear&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="k"&gt;let&lt;/span&gt;
    &lt;span class="n"&gt;divisibleBy4&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
        &lt;span class="n"&gt;y&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;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="n"&gt;divisibleBy100&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

    &lt;span class="n"&gt;divisibleBy400&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;400&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;in&lt;/span&gt; 
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy4&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy100&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;divisibleBy400&lt;/span&gt; &lt;span class="n"&gt;year&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This could actually be much better.&lt;/p&gt;

&lt;p&gt;First off, we can make &lt;code&gt;divisibleByX&lt;/code&gt; a lot more useful by having it take two arguments rather than one: the number to divide and the number to divide by.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;divisibleBy&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
 &lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Nice, but there's more. One of Elm's core libraries, "Basics" has a function called &lt;code&gt;rem&lt;/code&gt; with the following type signature:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight elm"&gt;&lt;code&gt;&lt;span class="n"&gt;rem&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to the docs, &lt;br&gt;
it does this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Find the remainder after dividing one number by another.

e.g. rem 11 4 == 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is this exactly what we need? Yes, this is exactly what we need.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;divisibleBy n y = rem y n == 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Noice. Time to refactor.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isLeapYear : Int -&amp;gt; Bool
isLeapYear y =
    let
        divisibleBy n y =
            rem y n == 0
    in
        (divisibleBy 4 y) &amp;amp;&amp;amp; (not (divisibleBy 100 y) || (divisibleBy 400 y))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That boolean expression looks like it could be simplified. There's another interesting boolean operator in the Basics package...&lt;code&gt;xor&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xor : Bool -&amp;gt; Bool -&amp;gt; Bool

The exclusive-or operator. True if exactly one input is True.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool beans. Let's refactor our solution.&lt;/p&gt;

&lt;p&gt;If we read our requirements again, this problem actually lends itself quite well to &lt;code&gt;xor&lt;/code&gt;. &lt;br&gt;
We want all years divisible by 4 EXCEPT the ones divisible by 100.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xor (divisibleBy 4 year) (divisibleBy 100 year) || (divisibleBy 400 year)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This leaves us with a third iteration of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;isLeapYear : Int -&amp;gt; Bool
isLeapYear year =
    let
        divisibleBy n y =
            rem y n == 0
    in
        xor (divisibleBy 4 year) (divisibleBy 100 year) || (divisibleBy 400 year)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I've not even scratched the surface of this powerful language. Getting into the FRP mindset has been a challenge but I can already see the benefits of thinking in this paradigm. While the solution I've put together over the last few paragraphs gets the job done, there is surely a better way...&lt;/p&gt;

&lt;p&gt;Follow me on Twitter at &lt;a href="https://twitter.com/_vincecampanale" rel="noopener noreferrer"&gt;@_vincecampanale&lt;/a&gt; and/or &lt;a href="https://github.com/vincecampanale" rel="noopener noreferrer"&gt;Github&lt;/a&gt; for more Elm stuff soon to come...&lt;/p&gt;

</description>
      <category>elm</category>
      <category>beginners</category>
      <category>problemsolving</category>
      <category>fun</category>
    </item>
    <item>
      <title>Harnessing the power of Javascript's .map and .filter</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Thu, 10 Aug 2017 13:18:52 +0000</pubDate>
      <link>https://dev.to/vincecampanale/harnessing-the-power-of-javascripts-map-and-filter</link>
      <guid>https://dev.to/vincecampanale/harnessing-the-power-of-javascripts-map-and-filter</guid>
      <description>&lt;p&gt;Functional programming has been a hot topic as of late. JavaScript is a multi-paradigm language and one of the paradigms available to us lucky JS devs is the functional paradigm. If you think this stuff is cool, go google Elm when you're done with this article ;)&lt;/p&gt;

&lt;p&gt;Disclaimer: I'm still learning myself, so some of the terms I use may not be precise or match the academic / proper definition. If you spot an error, please throw an exception in the comments. That being said, I'm confident about the big idea of this post so read on!&lt;/p&gt;

&lt;p&gt;Map &amp;amp; filter are two crucial methods in functional programming. You don't have to know anything about functional programming to know how to use them and I guarantee you that these two little functions &lt;em&gt;will&lt;/em&gt; improve your code.&lt;/p&gt;

&lt;p&gt;Let's learn by example. This exercise is taken straight from a tutorial I'm currently working through called "Functional Programming in Javascript," which is intended to serve as an introduction to RxJs (Reactive Extensions). &lt;a href="http://reactivex.io/learnrx/" rel="noopener noreferrer"&gt;Check it out&lt;/a&gt; when you're done reading this article. I'm going to solve the same problem in a couple different ways to show the true beauty of &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here we have some data containing a list of newly released movies in JSON format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Excellent test data from http://reactivex.io/learnrx/
 var newReleases = [
     {
       "id": 70111470,
       "title": "Die Hard",
       "boxart": "http://cdn-0.nflximg.com/images/2891/DieHard.jpg",
       "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
       "rating": 4.0,
       "bookmark": []
     },
     {
       "id": 654356453,
       "title": "Bad Boys",
       "boxart": "http://cdn-0.nflximg.com/images/2891/BadBoys.jpg",
       "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
       "rating": 5.0,
       "bookmark": [{ id: 432534, time: 65876586 }]
     },
     {
       "id": 65432445,
       "title": "The Chamber",
       "boxart": "http://cdn-0.nflximg.com/images/2891/TheChamber.jpg",
       "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
       "rating": 4.0,
       "bookmark": []
     },
     {
       "id": 675465,
       "title": "Fracture",
       "boxart": "http://cdn-0.nflximg.com/images/2891/Fracture.jpg",
       "uri": "http://api.netflix.com/catalog/titles/movies/70111470",
       "rating": 5.0,
       "bookmark": [{ id: 432534, time: 65876586 }]
     }
   ];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each movie has several properties: &lt;code&gt;id&lt;/code&gt;, &lt;code&gt;title&lt;/code&gt;, &lt;code&gt;boxart&lt;/code&gt;, &lt;code&gt;uri&lt;/code&gt;, &lt;code&gt;rating&lt;/code&gt;, and &lt;code&gt;bookmark&lt;/code&gt; (an array of JSON objects).&lt;/p&gt;

&lt;p&gt;In this tutorial, I'm going to solve a simple problem: &lt;strong&gt;Collect the IDs of movies with 5.0 ratings&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  ðŸ’“ For the Love of Loops ðŸ’“
&lt;/h4&gt;

&lt;p&gt;The first way I will solve this problem makes use of our oldest friend, the humble &lt;code&gt;for&lt;/code&gt; loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var counter,
  otherCounter,
  favorites = [],
  favoriteIds = [];

for ( counter = 0; counter &amp;lt; newReleases.length; counter++ ) {
  if ( newReleases[counter].rating === 5.0 ) {
    favorites.push(newReleases[counter]);
  }
}

for ( otherCounter = 0; otherCounter &amp;lt; favorites.length; otherCounter++ ) {
  favoriteIds.push(favorites[otherCounter].id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lovely. This gets the job done, but I have three problems with this code:  &lt;/p&gt;

&lt;p&gt;1 There's a lot of code here to do a simple task.  &lt;/p&gt;

&lt;p&gt;2 We're making a lot of variables to track our values, which is pretty wasteful memory-wise.  &lt;/p&gt;

&lt;p&gt;3 Does it really matter that we traverse the movie list from the beginning to end? Couldn't we do it in any order? Do we really &lt;em&gt;need&lt;/em&gt; to explicitly spell that out in our code?  &lt;/p&gt;

&lt;p&gt;Ask yourself number three and really sit and contemplate that for a moment. When we use the &lt;code&gt;for&lt;/code&gt; loop to tell an iterator to traverse an array, we have to spell out in code the &lt;em&gt;order&lt;/em&gt; in which the array is traversed. This is useful sometimes, but most of the time we're just going from beginning to end -- smells like an opportunity for abstraction to me.&lt;/p&gt;

&lt;h4&gt;
  
  
  For Each or Not For Each ðŸ“–
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;.forEach()&lt;/code&gt; abstracts the explicit logic of the &lt;code&gt;for&lt;/code&gt; loop away. We call &lt;code&gt;.forEach()&lt;/code&gt; on our &lt;code&gt;newReleases&lt;/code&gt; array and trust that the computer will traverse the array. The computer can traverse the array beginning to end, end to beginning, middle out, upside-down, it really doesn't matter. The point is: &lt;em&gt;we don't have to tell the computer about how the array is traversed -- we just know we're going to do something on each element of the array&lt;/em&gt;. That's where the &lt;strong&gt;iterator function&lt;/strong&gt; comes in. The iterator function is our instruction to the computer about &lt;em&gt;what&lt;/em&gt; should happen when the iterating mechanism (the hidden / implied &lt;code&gt;for&lt;/code&gt; loop) encounters each element in the array. For example, let's say we want to check if a movie has a rating of 5 stars and push it to a new array called &lt;code&gt;favorites&lt;/code&gt; if it does. Our function would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function (movie) {
  if ( movie.rating === 5.0) {
    favorites.push(movie);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By passing this function as an iterator to &lt;code&gt;.forEach()&lt;/code&gt;, we run it on every element in the array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var favorites = [],
    favoriteIds = [];

newReleases.forEach(function(movie) {
  if ( movie.rating === 5.0 ) {
    favorites.push(movie);
  }
});

favorites.forEach(function(movie) {
  favoriteIds.push(movie.id);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, the problems I had with the &lt;code&gt;for&lt;/code&gt; loop solution remain with the &lt;code&gt;.forEach()&lt;/code&gt; solution.  &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) Still a lot of code for such a simple task.

2) Still using variables to hold values as we go along.

3) We may have gotten rid of the explicit `for` loops, but I still see the word "for" in there. The extra code defining the order of traversal is gone, but we're still saying "for each element in this array, do something." I think the fact that we want to apply this function to each element should be *implied*.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Introducing the ðŸŒŸStarsðŸŒŸ of the Show
&lt;/h4&gt;

&lt;p&gt;Time to use &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt; to get the job done. Now that we understand exactly what needs to be done to solve this problem, it should be easy to reverse understand what &lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt; do for us.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.map()&lt;/code&gt; and &lt;code&gt;.filter()&lt;/code&gt; are just unique variations on the classic &lt;code&gt;.forEach()&lt;/code&gt;. The nice thing about them is that they handle a specific case for us so we don't have to bother telling the computer "for this element, do this". It just goes without saying that we want each element of the collection to be processed by the &lt;strong&gt;reducer function&lt;/strong&gt; (the same thing as the iterator function in &lt;code&gt;.forEach()&lt;/code&gt;).&lt;br&gt;&lt;br&gt;
 &lt;code&gt;.filter()&lt;/code&gt; is used when we want to *&lt;em&gt;ahem&lt;/em&gt;* filter each element in the collection based on a certain condition.&lt;br&gt;&lt;br&gt;
&lt;code&gt;.map()&lt;/code&gt; is used when we want to change each element in the array in some way. We're "mapping" each element from one value to another.  &lt;/p&gt;

&lt;p&gt;The moment we've all been waiting for:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var favorites = newReleases.filter(function(movie) {
  return movie.rating === 5.0;
});

var favoriteIds = favorites.map(function(movie) {
  return movie.id;
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hmmm... better...  &lt;/p&gt;

&lt;p&gt;Let's look at our original pain points and compare:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) I still think we could do this with less code.

2) Still skeptical about the need for two variables to compute one value...

3) âœ”ï¸ No more "for"! I'd say this problem is solved.  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Whew, abstracting away that &lt;code&gt;for&lt;/code&gt; loop took some effort, but it's officially taken care of now. We're almost done, I promise.&lt;/p&gt;

&lt;h4&gt;
  
  
  FILTERðŸ”—MAP
&lt;/h4&gt;

&lt;p&gt;Method chaining is a wonderful thing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var favoriteIds = newReleases
  .filter(function(movie) {
    return movie.rating === 5.0;
  })
  .map(function(movie) {
    return movie.id;
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That takes care of number 2.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) Still a bit verbose. I think we could sweeten this up with some syntactic sugar.*

2) âœ”ï¸ One value. One variable.

3) âœ”ï¸ No more "for"!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;*&lt;em&gt;Note: Despite what some may believe, arrow functions in Javascript are much more than mere syntactic sugar. Just wanted to use the 'sweeten' joke.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  ARROWS â†—ï¸ â¬…ï¸ â¬†ï¸ âž¡ï¸ â†˜ï¸
&lt;/h4&gt;

&lt;p&gt;Let's shorten this with some ES6 arrows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var favoriteIds = newReleases.
  filter( movie =&amp;gt; { return movie.rating === 5.0 }).
  map( movie =&amp;gt; { return movie.id });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Abbreviated Variables, &lt;code&gt;const&lt;/code&gt;, &amp;amp; Implicit Return
&lt;/h4&gt;

&lt;p&gt;Proceed with caution. Someone call the fire department. ðŸš’&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const favIds = newReleases.filter( m =&amp;gt; m.rating === 5.0 ).map( m =&amp;gt; m.id );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) âœ”ï¸ Short &amp;amp; sweet.

2) âœ”ï¸ One value. One variable.

3) âœ”ï¸ No more "for"!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Aren't &lt;code&gt;.map()&lt;/code&gt; &amp;amp; &lt;code&gt;.filter()&lt;/code&gt; the best?&lt;/p&gt;

&lt;p&gt;To Learn More:&lt;br&gt;&lt;br&gt;
     Here's the link to the tutorial I got this problem from: &lt;a href="http://reactivex.io/learnrx/" rel="noopener noreferrer"&gt;http://reactivex.io/learnrx/&lt;/a&gt;.  &lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>functional</category>
      <category>declarative</category>
    </item>
    <item>
      <title> "My First Two Weeks Using VIM"</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Tue, 01 Aug 2017 13:42:59 +0000</pubDate>
      <link>https://dev.to/vincecampanale/my-first-two-weeks-using-vim</link>
      <guid>https://dev.to/vincecampanale/my-first-two-weeks-using-vim</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.imgflip.com%2F1teh21.jpg" 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%2Fi.imgflip.com%2F1teh21.jpg"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have been putting this off for a while. I have always admired people that use Vim. I view them as sort of "super devs". If they use Dvorak, I have to pull out my shades to keep from being blinded by their genius. In this article, I will document the beginning of my journey to become as bright as the sun and faster than lightning. Here goes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Days 1 &amp;amp; 2
&lt;/h3&gt;

&lt;p&gt;My first day was actually yesterday, but I started the journal today so let's catch up. I started out doing what any good developer would do and googled 'Learn VIM'. Lo and behold, I received millions of results in split seconds. Hail the almighty Google.&lt;/p&gt;

&lt;p&gt;I found a course by a company called Upcase that basically said: go do &lt;code&gt;vimtutor&lt;/code&gt;. This means going to the terminal (with Vim installed) and typing &lt;code&gt;vimtutor&lt;/code&gt;. That's it. I started with it and spent a lot of time experimenting. I have to say, it's a little unnatural. I get a little sweaty using Vim because I'm resisting my natural tendencies. Don't judge me, I sweat a lot. That said, I can already see the benefits of having millions of combinations of commands literally at your fingertips. Here's a couple things I learned from the first day:&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 1
&lt;/h4&gt;

&lt;p&gt;Do not use arrow keys. Ever. This is an anti-pattern in VIM. The &lt;code&gt;hjkl&lt;/code&gt; method of moving the cursor allows you to keep your hand on the home row of the keyboard, which is crucial for efficiency.&lt;br&gt;
Here's a nice little diagram from &lt;code&gt;vimtutor&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ** To move the cursor, press the h,j,k,l keys as indicated. **
             ^
             k              Hint:  The h key is at the left and moves left.
       &amp;lt; h       l &amp;gt;               The l key is at the right and moves right.
             j                     The j key looks like a down arrow.
             v
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Tip 2
&lt;/h4&gt;

&lt;p&gt;By the same token, avoid using &lt;code&gt;DEL&lt;/code&gt; to delete characters as much as possible. Just navigate to the character you want to delete and press &lt;code&gt;x&lt;/code&gt;. Using the delete key is similar to using the arrow keys. It defeats the entire purpose of Vim.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 3
&lt;/h4&gt;

&lt;p&gt;Enable Vim shortcuts in your favorite editor. This will allow you to keep the familiar environment with colors and icons and whatnot. I found this much more enjoyable than just looking at my naked terminal. There are plugins and stuff that can add colors and handy tools to your terminal, but the resources I've looked at so far warn against installing a shit load of plugins right off the bat, and I shall take their word for it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 4
&lt;/h4&gt;

&lt;p&gt;Don't be too hard on yourself. I just pressed the delete key to delete something, right before I used &lt;code&gt;CTRL + Z&lt;/code&gt; to undo. This is a frustrating and difficult time, the first two weeks of Vim. I'm only on Day 2 and I'm feeling the burn already. But we will prevail. If I can do this, so can you.&lt;/p&gt;

&lt;h3&gt;
  
  
  Day 3
&lt;/h3&gt;

&lt;p&gt;Alrighty - new day, new tips.  Many thanks to those of you who commented with tips and tricks that you've learned from your own experience with Vim. I actually really like how this post is evolving and would love to see some more tips in the comments if you have any, no matter how small or insignicant they may seem. I promise it will help someone (me).&lt;/p&gt;

&lt;p&gt;I ended up writing down some of the key navigation shortcuts for Vim on some post-it notes and sticking them to my monitor -- here's a brief rundown of the shortcuts I have found to be essential (for a complete newbie). I've found it's useful to think of the keys as actions, which can be divided into different categories. The two categories I've identified this far are "edit" and "move". There may be more formal ways to refer to these categories -- post a comment if you know of a better way to refer to them.&lt;br&gt;
&lt;/p&gt;

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

hjkl - move around (see Days 1 &amp;amp; 2)

w - beginning of next word
e - end of next word

b - beginning of previous word

$ - end of current line
0 - beginning of current line

Edit commands:

i (insert) - insert before the character the cursor is on
a (append) - insert after the character the cursor is on
SHIFT + A (super append) - insert after the current line

x (delete) - delete the character that the cursor is on
d (delete*) - delete ... 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Tip 5
&lt;/h4&gt;

&lt;p&gt;I put a * on the delete command because it's a little special. By itself it does nothing. Combine it with a move command and you can delete all the things.&lt;/p&gt;

&lt;p&gt;Let's say I want to delete two words that are next to each other.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) Navigate to the beginning of the first word using your move commands.
2) Type d2w.
3) Press enter.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So what's happening here? &lt;code&gt;d&lt;/code&gt; tells Vim I want to delete something. It then patiently waits for me to tell it what to delete (so polite). I say to Vim, &lt;code&gt;2w&lt;/code&gt; (read: two words) and poof, they're gone. That move there shows the power of Vim. Each of these keys does something on its own and lets you run with Vim. Combine them, and you can fly.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 6
&lt;/h4&gt;

&lt;p&gt;Yesterday, I called it VIM. I called it that in the title too. I won't change it for the sake of preserving my growth as a Vimmer. TIL, it's supposed to be "Vim", like "Jim".&lt;br&gt;
From &lt;a href="http://vimhelp.appspot.com/intro.txt.html#pronounce" rel="noopener noreferrer"&gt;&lt;code&gt;:help pronounce&lt;/code&gt;&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Vim is pronounced as one word, like Jim, not vi-ai-em.  It's written with a
capital, since it's a name, again like Jim.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I know.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 7
&lt;/h4&gt;

&lt;p&gt;This tip comes from &lt;a href="https://dev.to/jeanbernard"&gt;Jean Bernard&lt;/a&gt; - thanks Jean!&lt;br&gt;
Quoting him here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Pro-tip: I use "jj" as a shortcut to the ESC key. That way when you want to switch back to normal mode you don't have to go all the way to the ESC key.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Love it.&lt;/p&gt;

&lt;p&gt;I want to add a "Comfort-o-meter" to this post. I'd say I've risen from a 1 to a 2. I no longer sweat when I use Vim, so that's progress. Still have a tendency to reach for comfortable shortcuts for selecting text and moving lines, copy-pasting, etc. However, the momentum is building on itself and I'm having fun. &lt;/p&gt;

&lt;p&gt;COMFORT-O-METER: 2&lt;/p&gt;

&lt;h3&gt;
  
  
  After Week 1
&lt;/h3&gt;

&lt;p&gt;It's been a week now since I started making an effort to learn Vim. I haven't been using it at work since I find it too distracting to really learn and practice the shortcuts while thinking about writing clean code and building a maintainable application. That being said, I think I'm nearing the point where enough basic movements and commands are automatic enough to warrant making the switch full time. I'm in no hurry. &lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 8
&lt;/h4&gt;

&lt;p&gt;The basic shortcuts will take you far. The ones I outlined earlier in the post have been basically the only ones I've used up to this point. You can accomplish most things with the &lt;code&gt;d (motion)&lt;/code&gt; command when it comes to deleting / cutting text. Similarly &lt;code&gt;y (motion)&lt;/code&gt; for copying lines or words. I've found it's worthwhile to start thinking of sections of text in these terms rather than thinking about the space around them for selecting.&lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 9
&lt;/h4&gt;

&lt;p&gt;Get good with &lt;code&gt;:help&lt;/code&gt;. Vim's help system is very searchable and useful if you know what your doing. &lt;/p&gt;

&lt;h4&gt;
  
  
  Tip 10
&lt;/h4&gt;

&lt;p&gt;This one comes from Alan Campora:&lt;br&gt;
"Edit and move are modes, not actions. Actions are something different. For example, if you want to replace a word, put your cursor on it and press c(change) i(inner) w(word). That's a great level up :D"&lt;/p&gt;

&lt;p&gt;This turned out to be a crucial tip for me. This subtle paradigm shift made me understand that &lt;code&gt;d&lt;/code&gt; is not so much a verb to delete something, rather just a button to put me in &lt;code&gt;delete mode&lt;/code&gt;. This is obvious in retrospect, but Alan's tip was what I needed to fully understand -- hopefully it helps someone else, too.&lt;/p&gt;

&lt;p&gt;COMFORT-O-METER: 4&lt;br&gt;
Treating the comfort-o-meter as similar to the Richter scale for Earthquakes, this is pretty sizable progress. The few basic &lt;code&gt;(mode) (motion)&lt;/code&gt; combos make editing with Vim pretty breezy. It's growing on me and I'm glad I pushed through the first day of discomfort. That really is the worst part.&lt;/p&gt;




&lt;p&gt;Post in progress...follow me on &lt;a href="https://twitter.com/_vincecampanale" rel="noopener noreferrer"&gt;twitter&lt;/a&gt;, &lt;a href="https://github.com/vincecampanale" rel="noopener noreferrer"&gt;github&lt;/a&gt;, or &lt;a href="https://dev.to/vincecampanale"&gt;dev.to&lt;/a&gt; for updates :)&lt;/p&gt;

</description>
      <category>learning</category>
      <category>vim</category>
      <category>beginners</category>
      <category>fun</category>
    </item>
    <item>
      <title>Revisited: A Beginner's Solution to a Common Javascript Interview Challenge</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Mon, 10 Jul 2017 12:16:15 +0000</pubDate>
      <link>https://dev.to/vincecampanale/revisited-a-beginners-solution-to-a-common-javascript-interview-question</link>
      <guid>https://dev.to/vincecampanale/revisited-a-beginners-solution-to-a-common-javascript-interview-question</guid>
      <description>&lt;p&gt;"Confirm the Ending" is another great problem from Free Code Camp's algorithm scripting challenges. The goal is to check if a string (first argument, &lt;code&gt;str&lt;/code&gt;) ends with the given target string (second argument, &lt;code&gt;target&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;As is common for coding exercises, this problem can be solved countless ways. Today, I'm going to walk through my solution from when I first started coding, improve it, and conclude with a description of the difference between &lt;code&gt;.substr()&lt;/code&gt; and &lt;code&gt;.substring()&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Beginner Solution
&lt;/h4&gt;

&lt;p&gt;When I had first ventured into learning to code, I took a stab at this problem and successfully solved it, however my solution is a little..."verbose"...  &lt;/p&gt;

&lt;p&gt;I used the &lt;code&gt;.substr()&lt;/code&gt; method which takes two arguments. The first argument is the index in the string to &lt;em&gt;start&lt;/em&gt; at and the second argument number of characters to extract from that index. So, say you want to get &lt;code&gt;"lo"&lt;/code&gt; from &lt;code&gt;"hello!""&lt;/code&gt;. In order to do this with &lt;code&gt;.substr()&lt;/code&gt;, you would say &lt;code&gt;"hello!".substr(3, 2)&lt;/code&gt;. This will go to the third character in &lt;code&gt;"hello"!&lt;/code&gt;, which is the first &lt;code&gt;"l"&lt;/code&gt;, then go for one more character before stopping at the fourth character in &lt;code&gt;"hello!"&lt;/code&gt;, which is &lt;code&gt;"o"&lt;/code&gt;. If you're thinking my counting skills need some work, you're probably right, but in Javascript, string indices are 0-based (like arrays). So the character &lt;code&gt;"h"&lt;/code&gt; has index 0 in the string.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;confirmEnding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&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;substr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;target&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;target&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Just to briefly recap what I did here: create a variable containing a string chopped off the end of the &lt;code&gt;str&lt;/code&gt; argument that has the same length as the target string. If the string contained in that variable matches the target, return true. Otherwise, return false. Pretty simple solution to a pretty simple problem but this can be way better.&lt;/p&gt;

&lt;h4&gt;
  
  
  Better Solution
&lt;/h4&gt;

&lt;p&gt;There's a neat trick you can use when calling &lt;code&gt;.substr&lt;/code&gt;. If you give it a negative number as it's first and only argument, e.g. &lt;code&gt;"string".substr(-3)&lt;/code&gt;, you will get a string containing the characters starting at the given number &lt;em&gt;from the end of the string&lt;/em&gt;. So &lt;code&gt;"string".substr(-3)&lt;/code&gt; returns &lt;code&gt;"ing"&lt;/code&gt;. Let's employ this trick in our solution:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;confirmEnding&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&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;target&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;target&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In English, this function says: return a string with the same length as &lt;code&gt;target&lt;/code&gt; from the end of &lt;code&gt;str&lt;/code&gt; and compare the new string to &lt;code&gt;target&lt;/code&gt;. If they are &lt;em&gt;strictly&lt;/em&gt; equal, return true. Otherwise, return false. This will work with two equal signs as well, but it's typically good practice to use the triple equals.&lt;/p&gt;

&lt;p&gt;We can now use ES6 syntax to make a very nice, compact, pure function:&lt;/p&gt;



&lt;div class="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;confirmEnding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;target&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substr&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;target&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h4&gt;
  
  
  The Difference Between &lt;code&gt;.susbtr()&lt;/code&gt; and &lt;code&gt;.substring()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;This feat can also be accomplished with &lt;code&gt;.substring()&lt;/code&gt;, however I don't think the solution is quite as elegant. It's a good exercise though, and I'll leave it to you to try it out. These methods may appear to be aliases for one another, however they actually do different things.&lt;/p&gt;

&lt;p&gt;From the MDN docs (aka the Javascript Bible):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.substr()&lt;/code&gt;: "returns the characters in a string beginning at the specified location through the specified number of characters".&lt;br&gt;
&lt;code&gt;.substring()&lt;/code&gt;: "returns a subset of a string between one index and another, or through the end of the string".&lt;/p&gt;

&lt;p&gt;Perhaps an easier way to think about it is this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.substr()&lt;/code&gt;: starting at index &lt;em&gt;argument one&lt;/em&gt;, get &lt;em&gt;argument two&lt;/em&gt; number of characters.&lt;br&gt;
&lt;code&gt;.substring()&lt;/code&gt;: starting at index &lt;em&gt;argument one&lt;/em&gt;, get the characters up until &lt;em&gt;argument two&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Yet another way to look at it:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.substr(from this index, for this many characters)&lt;/code&gt;&lt;br&gt;
&lt;code&gt;.substring(from this index, to this index)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Hope this was helpful! Reach out anytime with feedback, questions, tips, and especially compliments. If you want to follow along as I explore this wonderful language of the web, find me on &lt;a href="https://twitter.com/_vincecampanale"&gt;Twitter&lt;/a&gt; or at my &lt;a href="http://www.vincecampanale.com/"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Vinny out. âœŒï¸&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>problemsolving</category>
      <category>challenge</category>
    </item>
    <item>
      <title> "Using the Vigenère Cipher to Encrypt a Message with Javascript"</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Tue, 27 Jun 2017 19:20:28 +0000</pubDate>
      <link>https://dev.to/vincecampanale/using-the-vignere-cipher-to-encrypt-a-message-with-javascript</link>
      <guid>https://dev.to/vincecampanale/using-the-vignere-cipher-to-encrypt-a-message-with-javascript</guid>
      <description>&lt;p&gt;This article is technically Part 3 of a three part series, but don't worry, the first two parts are not critical to understanding this part (and this is the more interesting section tbh). &lt;/p&gt;

&lt;p&gt;Here are the links to the others if you're curious:&lt;br&gt;
&lt;a href="http://www.vincecampanale.com/blog/2017/01/20/vignere-cipher-part1/" rel="noopener noreferrer"&gt;Using the Vigenère Cipher to Encrypt a Message (Part 1)&lt;/a&gt;&lt;br&gt;
&lt;a href="http://www.vincecampanale.com/blog/2017/02/01/vigenere-cipher-part2/" rel="noopener noreferrer"&gt;Using the Vigenère Cipher to Encrypt a Message (Part 2)&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;In &lt;a href="http://www.vincecampanale.com/blog/2017/01/20/vignere-cipher-part1/" rel="noopener noreferrer"&gt;Part 1&lt;/a&gt;, I gave a brief overview of the Vigenère cipher and discussed the two approaches to solving it (the two approaches that I could come up with - there are definitely others). In &lt;a href="http://www.vincecampanale.com/blog/2017/02/01/vigenere-cipher-part2/" rel="noopener noreferrer"&gt;Part 2&lt;/a&gt;, I covered the first approach, which is essentially a Caesar cipher with a dynamic shift number. In this part, I'm going to step through the more interesting solution - the way it's really intended to be done - using the magical Vigenère table.  &lt;/p&gt;

&lt;p&gt;The Vigenère table looks like this:  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9a%2FVigen%25C3%25A8re_square_shading.svg%2F864px-Vigen%25C3%25A8re_square_shading.svg.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%2Fupload.wikimedia.org%2Fwikipedia%2Fcommons%2Fthumb%2F9%2F9a%2FVigen%25C3%25A8re_square_shading.svg%2F864px-Vigen%25C3%25A8re_square_shading.svg.png"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h6&gt;
  
  
  By Brandon T. Fields (&lt;a href="//commons.wikimedia.org/w/index.php?title=User:Cdated&amp;amp;action=edit&amp;amp;redlink=1" title="User:Cdated (page does not exist)"&gt;cdated&lt;/a&gt;) - Based upon &lt;a href="//commons.wikimedia.org/wiki/File:Vigenere-square.png" title="File:Vigenere-square.png"&gt;Vigenere-square.png&lt;/a&gt; by &lt;a href="https://en.wikipedia.org/wiki/User:Matt_Crypto" title="en:User:Matt Crypto" rel="noopener noreferrer"&gt;en:User:Matt Crypto&lt;/a&gt;. This version created by &lt;a href="//commons.wikimedia.org/wiki/User:Bdesham" title="User:Bdesham"&gt;bdesham&lt;/a&gt; in Inkscape, and modified by &lt;a href="//commons.wikimedia.org/w/index.php?title=User:Cdated&amp;amp;action=edit&amp;amp;redlink=1" title="User:Cdated (page does not exist)"&gt;cdated&lt;/a&gt; to include visual guides.&lt;a href="//commons.wikimedia.org/wiki/File:Inkscape_Logo.svg" title="File:Inkscape Logo.svg"&gt;&lt;/a&gt;This &lt;a href="https://en.wikipedia.org/wiki/Vector_images" title="w:Vector images" rel="noopener noreferrer"&gt;vector image&lt;/a&gt; was created with &lt;a href="//commons.wikimedia.org/wiki/Help:Inkscape" title="Help:Inkscape"&gt;Inkscape&lt;/a&gt;., Public Domain, &lt;a href="https://commons.wikimedia.org/w/index.php?curid=15037524" rel="noopener noreferrer"&gt;Link&lt;/a&gt;
&lt;/h6&gt;

&lt;p&gt;Don't worry about deciphering that behemoth right now, you'll gain a deeper understanding as I go over the code to build this thing.&lt;/p&gt;

&lt;p&gt;The process breaks down into four primary functions: the &lt;code&gt;generateAlphabet&lt;/code&gt; function, the &lt;code&gt;generateVigenereTable&lt;/code&gt; function, the &lt;code&gt;encodeWithTable&lt;/code&gt; function, and of course the &lt;code&gt;vigenereCipherWithTable&lt;/code&gt; function. Note: this solution is rather imperative and I hope to reimplement it in a more declarative way in the future so follow me on twitter &lt;a href="https://twitter.com/_vincecampanale" rel="noopener noreferrer"&gt;@_vincecampanale&lt;/a&gt; or on &lt;a href="https://dev.to/vincecampanale"&gt;dev.to&lt;/a&gt; for humor and updates (all about JS of course).&lt;/p&gt;

&lt;p&gt;So here's the plan:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) Generate an alphabet starting with a given letter (a in the first column, b in the second, etc) - note: the alphabet must wrap around to the beginning when it reaches z
2) Generate a Vigenere table
  - The keys consist of the standard alphabet (a-z)
  - Each key's value is an alphabet starting with that key and wrapping back   around to a (each value is 26 letters)
3) Encode the message by looking up each letter of the original message in the   keys of the Vigenere table, then traversing the table to get the value from the   character code of the keyword letter  
4) Put it all together in the final function  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 1: Build the &lt;code&gt;generateAlphabet&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;In this function, the parameter will be a starting index. We are going to iterate over twenty-six char codes, starting at the provided start index. Presumably, the first char code will be 97, and they will go up from there. In order to account for char codes over 122, we add some if/else logic into the &lt;code&gt;String.fromCharCode&lt;/code&gt; method. Ternary operators allow us to keep this code succinct.&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;function&lt;/span&gt; &lt;span class="nf"&gt;generateAlphabet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;start&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;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="c1"&gt;//from start index to 26 chars later&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="nx"&gt;start&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;start&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;26&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="c1"&gt;//convert the char code into a letter and push it to the alphabet array&lt;/span&gt;
    &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fromCharCode&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;gt;&lt;/span&gt; &lt;span class="mi"&gt;122&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;26&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;//if char code &amp;gt; 122, return code - 26, else&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="mi"&gt;97&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;26&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;//if char code &amp;lt; 97, return code + 26, else&lt;/span&gt;
      &lt;span class="nx"&gt;i&lt;/span&gt;                  &lt;span class="c1"&gt;//just return the code&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;alphabet&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//return the alphabet array&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 2: Build the &lt;code&gt;generateVigenereTable&lt;/code&gt; function
&lt;/h3&gt;

&lt;p&gt;Dedicating a function to generating alphabets with different starting character codes allows us to keep the Vigenère table function surprisingly simple.&lt;/p&gt;

&lt;p&gt;All we need to do is instantiate an empty object, &lt;code&gt;table&lt;/code&gt;. Load the keys of that object up with the standard alphabet, starting with the letter 'a' (char code 97). Then for each key in the table, we generate an alphabet that starts at the key's index. So the second key ('b') has an alphabet starting with b and wrapping back around to end with a. The third key ('c') has an alphabet starting with c and wrapping back around to end with b. And so on.&lt;/p&gt;

&lt;p&gt;In 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="c1"&gt;//generate an object, where each key is a letter and each value is another alphabet&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;generateVigenereTable&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;table&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;//instantiate a temporary object to hold the table&lt;/span&gt;
  &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateAlphabet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;97&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//set the keys of the object equal to the standard alphabet (starting at 97)&lt;/span&gt;
  &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&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;table&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateAlphabet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;97&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;  &lt;span class="c1"&gt;//set the value of each key as the alphabet&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;table&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//return the table&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 3: Encode each character using a Vigenère table
&lt;/h3&gt;

&lt;p&gt;This is the most important step in the solution - the piece where we put our Vigenère table to use. See the comments for a line-by-line explanation.&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;function&lt;/span&gt; &lt;span class="nf"&gt;encodeWithTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keywordStr&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;messageArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//split the message into an array&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;keywordArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keywordStr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//split the keyword string into an array&lt;/span&gt;
  &lt;span class="nx"&gt;messageArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;//for each letter and index in the message array&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;messageChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;letter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//make a temp variable to hold the letter&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;keywordChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keywordArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;//get the corresponding letter from the keyword string using the index&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;keywordCharIndex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keywordChar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charCodeAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;97&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//get the index of the keyword by subtracting 97 from the charcode&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;vigenereTable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateVigenereTable&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;//create a vigenere table&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;cipherChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;vigenereTable&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;messageChar&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;keywordCharIndex&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;//look up the corresponding letter in the table&lt;/span&gt;

    &lt;span class="nx"&gt;messageArray&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;cipherChar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;//replace the letter in the message with the cipher letter&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;messageArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;//convert the messageArray back to a string and return it&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 4: The Actual Function
&lt;/h3&gt;

&lt;p&gt;Since we've taken the time to break down our problem and write thorough helper functions, the cipher function itself is nothing special. In fact, it's identical to the function in Part 2, except now we are encoding with the Vigenère table, rather than with the boring old Caesar cipher.&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;function&lt;/span&gt; &lt;span class="nf"&gt;vigenereCipherWithTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keyword&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;lemon&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="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;message&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;keyword&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;keyword&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// repeat the keyword a bunch of times&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;keywordStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;keyword&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substr&lt;/span&gt;&lt;span class="p"&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;message&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="c1"&gt;// cut the keyword string so it's the same length as the message&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;encodeWithTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keywordStr&lt;/span&gt; &lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// encode the string using the vigenere table&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;ciphertext&lt;/span&gt; &lt;span class="c1"&gt;//return the cipher text!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And there you have it! Have fun passing secret messages back and forth with your friends...good luck decoding them though...  ;)&lt;/p&gt;

&lt;p&gt;Hope this was enjoyable and helpful. Shoot me an email or &lt;a href="https://twitter.com/_vincecampanale" rel="noopener noreferrer"&gt;tweet at me&lt;/a&gt; with comments, questions, complaints, and suggestions. &lt;/p&gt;

&lt;p&gt;Til next time ☮&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>encryption</category>
      <category>challenge</category>
    </item>
    <item>
      <title>What is Javascript's `new` keyword doing under the hood?</title>
      <dc:creator>Vince Campanale</dc:creator>
      <pubDate>Tue, 20 Jun 2017 02:12:57 +0000</pubDate>
      <link>https://dev.to/vincecampanale/what-is-javascripts-new-keyword-doing-under-the-hood</link>
      <guid>https://dev.to/vincecampanale/what-is-javascripts-new-keyword-doing-under-the-hood</guid>
      <description>&lt;p&gt;Good morning, afternoon, evening, night. I have some things to share with you about the &lt;code&gt;new&lt;/code&gt; keyword in Javascript. Important things. &lt;/p&gt;

&lt;p&gt;I'll start with some context and background about Constructor functions and the &lt;code&gt;class&lt;/code&gt; keyword. Then, I will explain exactly &lt;em&gt;what&lt;/em&gt; the &lt;code&gt;new&lt;/code&gt; keyword is doing under the hood. Next, I will show &lt;em&gt;how&lt;/em&gt; it does what it does by implementing it in code. Finally, I will explain &lt;em&gt;why&lt;/em&gt; it does these things and give a couple arguments for avoiding this approach to Javascript object creation altogether in &lt;em&gt;most&lt;/em&gt; situations. The information presented here comes from &lt;a href="https://www.youtube.com/watch?v=Y3zzCY62NYc"&gt;these&lt;/a&gt; &lt;a href="https://medium.com/javascript-scene/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e"&gt;resources&lt;/a&gt; and several others, processed by my brain. &lt;/p&gt;

&lt;h3&gt;
  
  
  Constructor functions ðŸ›
&lt;/h3&gt;

&lt;p&gt;A Constructor function is a function that builds and returns a new instance of object. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/** Car: {
*    doors: number,
*    color: string,
*    drive: Function
*   }
*
* Car(doors: number, color: string) =&amp;gt; Car
*/&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doors&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&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;drive&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="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;Vroom!&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 capital letter at the beginning of the Constructor name is simply a convention adopted by Javascript programmers to separate &lt;em&gt;Constructor&lt;/em&gt; functions from regular functions. &lt;/p&gt;

&lt;p&gt;The way Constructor functions work under the hood might make for an interesting article, but I'll leave that for another day. Today is about &lt;code&gt;new&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The most important thing to take from this section is that the Constructor function, when invoked with the &lt;code&gt;new&lt;/code&gt; keyword, will return an &lt;em&gt;object&lt;/em&gt; with a &lt;code&gt;doors&lt;/code&gt; property, a &lt;code&gt;color&lt;/code&gt; property, and a &lt;code&gt;drive&lt;/code&gt; method.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;class&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;class&lt;/code&gt; keyword was introduced to Javascript with the ES2015 specification, commonly known as ES6, soon to be known as "just Javascript."&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;class&lt;/code&gt; keyword introduces nothing new (ha) -- it just provides some syntactic sugar for folks who like Java and semantic keywords. Nothing wrong with that. &lt;/p&gt;

&lt;p&gt;Here's how you use it:&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;class&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doors&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;doors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doors&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;drive&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;Vroom!&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="c1"&gt;// or drive = () =&amp;gt; console.log('Vroom!');&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Notice anything familiar?&lt;/p&gt;

&lt;p&gt;I'll give you a hint:&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// Function &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Under the Hood ðŸš—
&lt;/h3&gt;

&lt;p&gt;Whether you are using a vanilla Constructor function or a special keyword to instantiate your object constructing mechanism, you will be using &lt;code&gt;new&lt;/code&gt; to create new instances of the defined object. (There is another not-so-secret and powerful way to generate objects in Javascript called a &lt;a href="https://medium.com/javascript-scene/javascript-factory-functions-vs-constructor-functions-vs-classes-2f22ceddf33e"&gt;factory&lt;/a&gt; function which will have to be covered in a future post).&lt;/p&gt;

&lt;p&gt;So what is the &lt;code&gt;new&lt;/code&gt; keyword doing under the hood (in human words)?&lt;/p&gt;

&lt;p&gt;Three letters, four actions. When you say &lt;code&gt;var myCar = new Car()&lt;/code&gt;, it...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1) Creates a new (empty) object 
2) Gets the prototype of the constructor function (Car) and sets it as the empty object's prototype
3) Calls the constructor function with the new empty object as `this` 
4) Returns the new object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does this process look like in computer words?&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note:&lt;/em&gt; In order to reimplement &lt;code&gt;new&lt;/code&gt; we will have to pass in the constructor and it's arguments separately. &lt;/p&gt;

&lt;p&gt;First, let's do it in ES5 because you only live once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// new(constructor: Function, constructorArgs: Array&amp;lt;any&amp;gt;) =&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;new2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;constructorArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// Step 1: Create an empty object&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;newObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

    &lt;span class="c1"&gt;// Step 2a: Get the prototype of the constructor function&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;constructorPrototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;constructor&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="c1"&gt;// Step 2b: Set the empty object's prototype &lt;/span&gt;
    &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;constructorPrototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Retro technique to turn arguments into an actual array &lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;argsArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Array&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;slice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
    &lt;span class="c1"&gt;// Slice off first argument b/c that's the constructor function itself. &lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;realConstructorArgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;argsArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Step 3: Invoke constructor with newObject as 'this'&lt;/span&gt;
    &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;realConstructorArgs&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Step 4: Return the new object :)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;newObject&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;Now that we have a working implementation, we can clean it up and make use of some new tools from ES6.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// new(constructor: Function, constructorArgs: Array&amp;lt;any&amp;gt;) =&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;new2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;constructorArgs&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;newObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;constructor&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="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;constructorArgs&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;newObject&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;And...&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;myCar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;new2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blue&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;myCar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// { doors: 4, color: 'blue', drive: [Function] }&lt;/span&gt;
&lt;span class="nx"&gt;myCar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;drive&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c1"&gt;// Vroom!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;But wait&lt;/em&gt;, there is an edge case. If the constructor function itself returns a new object, like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;Car&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;doors&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="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;doors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;doors&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;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;color&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;drive&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="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;Vroom!&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="nx"&gt;doors&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we should just return that object directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// new(constructor: Function, constructorArgs: Array&amp;lt;any&amp;gt;) =&amp;gt; Object&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;new2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;constructorArgs&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;newObject&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
    &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setPrototypeOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;constructor&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="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;constructorArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;newObject&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;And we're done.&lt;/p&gt;

&lt;p&gt;Hope this helped! &lt;/p&gt;

&lt;p&gt;Tweet me with feedback &lt;a href="https://twitter.com/_vincecampanale"&gt;@_vincecampanale&lt;/a&gt; if it did or didn't.&lt;/p&gt;

&lt;p&gt;Til next time ðŸ‘‹.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>es6</category>
      <category>class</category>
      <category>constructor</category>
    </item>
  </channel>
</rss>
