<?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: Quinn Lashinsky</title>
    <description>The latest articles on DEV Community by Quinn Lashinsky (@qmaximillian).</description>
    <link>https://dev.to/qmaximillian</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%2F352355%2Fef5f44a7-d550-4f37-b7c9-aeb84153ac4d.jpeg</url>
      <title>DEV Community: Quinn Lashinsky</title>
      <link>https://dev.to/qmaximillian</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/qmaximillian"/>
    <language>en</language>
    <item>
      <title>Pausing Rust By Example</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Mon, 29 Mar 2021 15:36:42 +0000</pubDate>
      <link>https://dev.to/qmaximillian/pausing-rust-by-example-557k</link>
      <guid>https://dev.to/qmaximillian/pausing-rust-by-example-557k</guid>
      <description>&lt;p&gt;I want to thank everyone who has been kind enough to follow this series on learning Rust through Rust by Example. &lt;/p&gt;

&lt;p&gt;The amount of time I want to put into making a quality blog series is at odds with my main goal of getting better at HTML, CSS, and Javascript. I need to dedicate more time to those languages instead of learning a new one. &lt;/p&gt;

&lt;p&gt;I look forward to coming back to this series in the future when I have more time!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Making Change Algorithm</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Thu, 25 Mar 2021 17:04:07 +0000</pubDate>
      <link>https://dev.to/qmaximillian/making-change-algorithm-3m1d</link>
      <guid>https://dev.to/qmaximillian/making-change-algorithm-3m1d</guid>
      <description>&lt;p&gt;This past week I did a coding challenge that I actually enjoyed. The problem wasn't ridiculously abstract or one I would never solve if I were hired as a Junior Engineer. Below I'm gonna give the problem, show my pseudo-code, and then solve it. This may not be the best solution, but it works. I'd love to see other solutions.  Write or link to your solution in the comments and feel free to add possible edge cases I may have missed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;Given a &lt;code&gt;payment&lt;/code&gt; array of US monetary denominations (i.e. &lt;code&gt;10.00&lt;/code&gt; or &lt;code&gt;.05&lt;/code&gt;), and the &lt;code&gt;price&lt;/code&gt; of an item, calculate the least amount of coins a person should receive after paying for their item. If they do not input enough money, return their money in the least amount of coins possible. The coins given back should be formatted like this, &lt;code&gt;[pennies, nickels, dimes, quarters]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Can buy&lt;/span&gt;

&lt;span class="c1"&gt;// input&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.25&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[.&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&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="mf"&gt;1.00&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// output&lt;/span&gt;
&lt;span class="nx"&gt;makeChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[.&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&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="mf"&gt;1.00&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="c1"&gt;// [0, 1, 1, 0]&lt;/span&gt;

&lt;span class="c1"&gt;// 0 Pennies&lt;/span&gt;
&lt;span class="c1"&gt;// 1 Nickel&lt;/span&gt;
&lt;span class="c1"&gt;// 1 Dime&lt;/span&gt;
&lt;span class="c1"&gt;// 0 Quarters&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Cannot buy&lt;/span&gt;

&lt;span class="c1"&gt;// input &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.50&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1.00&lt;/span&gt;&lt;span class="p"&gt;,&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="c1"&gt;// output&lt;/span&gt;
&lt;span class="nx"&gt;makeChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;1.50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;1.00&lt;/span&gt;&lt;span class="p"&gt;,&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="c1"&gt;// [0, 0, 1, 4]&lt;/span&gt;

&lt;span class="c1"&gt;// 0 Pennies&lt;/span&gt;
&lt;span class="c1"&gt;// 0 Nickels&lt;/span&gt;
&lt;span class="c1"&gt;// 1 Dime&lt;/span&gt;
&lt;span class="c1"&gt;// 4 Quarters&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  My Pseudo-Code
&lt;/h2&gt;

&lt;p&gt;Here is the initial pseudo-code I came up with:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert the &lt;code&gt;price&lt;/code&gt; to cents&lt;/li&gt;
&lt;li&gt;Convert each value in the &lt;code&gt;payment&lt;/code&gt; array to cents&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;paymentInCents&lt;/code&gt; equals &lt;code&gt;priceInCents&lt;/code&gt;, return &lt;code&gt;[0,0,0,0]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Determine how much change I owe (If I paid less than the &lt;code&gt;priceInCents&lt;/code&gt;, or more than the &lt;code&gt;priceInCents&lt;/code&gt;) 

&lt;ol&gt;
&lt;li&gt;If &lt;code&gt;paymentInCents&lt;/code&gt; is greater than &lt;code&gt;priceInCents&lt;/code&gt;, buyer is owed &lt;code&gt;paymentInCents&lt;/code&gt; minus &lt;code&gt;priceInCents&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;paymentInCents&lt;/code&gt; is less than &lt;code&gt;priceInCents&lt;/code&gt; , buyer is owed &lt;code&gt;paymentInCents&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt;Create a &lt;code&gt;changeArray&lt;/code&gt; to keep track of the amount of each denomination I am returning as change.&lt;/li&gt;
&lt;li&gt;Calculate change owed to our buyer for each denomination (quarters, dimes, nickels, pennies) in descending order. Put those values in their corresponding position (&lt;code&gt;[pennies, nickels, dimes, quarters]&lt;/code&gt;). After we complete this process, the amount of change owed should be &lt;code&gt;0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;changeArray&lt;/code&gt;, which is the amount of each denomination owed to the buyer.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  My Solution
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;makeChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payment&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;priceInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&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;priceInCents&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&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="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="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="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;changeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;priceInCents&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; 
        &lt;span class="p"&gt;?&lt;/span&gt; 
            &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; 
        &lt;span class="p"&gt;:&lt;/span&gt; 
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paymentInCents&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;priceInCents&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;calculateChangeOwed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeOwed&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;calculateChangeOwed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeOwed&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;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;changeOwed&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeArray&lt;/span&gt; &lt;span class="o"&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;amountOfQuarters&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfQuarters&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfQuarters&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;amountOfDimes&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDimes&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDimes&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;amountOfNickels&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfNickels&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfNickels&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;amountOfPennies&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&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;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfPennies&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;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfPennies&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;changeArray&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Some Explanations
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Why Did I Convert All Monetary Values to Cents?
&lt;/h3&gt;

&lt;p&gt;In Javascript mathematical operations with floating point numbers may lack numeric precision and may not give us the results we expect. For example:&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="mf"&gt;40.90&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// 122.69999999999999&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The output should be &lt;code&gt;122.7&lt;/code&gt;, right? Unfortunately, we can't expect Javascript to give us perfect representations of floats. Integers on the other hand can be represented perfectly, and converting all monetary values to cents will help us avoid floating point number issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is This Code D.R.Y?
&lt;/h3&gt;

&lt;p&gt;I repeat the same code 4 times while calculating how much of each currency is owed to the buyer.&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;// ... code ...&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfQuarters&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfQuarters&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfQuarters&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;amountOfDimes&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDimes&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDimes&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;amountOfNickels&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfNickels&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nx"&gt;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfNickels&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;amountOfPennies&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&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;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfPennies&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;changeArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfPennies&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// ... more code ...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And I think it's entirely possible to clean this code up and make it better. &lt;/p&gt;

&lt;h3&gt;
  
  
  With &lt;code&gt;reduce&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;If I create an array of each denomination I could possibly return in descending order I can use the &lt;code&gt;reduce&lt;/code&gt; function to create an array of amount of each denomination owed to the buyer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;155&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;denominationArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;25&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="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;denominationArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfDenomination&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&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;acc&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;
  
  
  With &lt;code&gt;reduceRight&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Not much different, but I think it's a bit more readable with all denominations in ascending order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;155&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;denominationArray&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;5&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="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;denominationArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduceRight&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfDenomination&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&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;acc&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;What the Difference?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;reduce&lt;/code&gt; will traverse your array left to right&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;reduceRight&lt;/code&gt; will traverse your array right to left&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Full Code With &lt;code&gt;reduceRight&lt;/code&gt;
&lt;/h3&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;makeChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payment&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;priceInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;price&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&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;priceInCents&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&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="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="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="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;changeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;priceInCents&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; 
        &lt;span class="p"&gt;?&lt;/span&gt; 
            &lt;span class="nx"&gt;paymentInCents&lt;/span&gt; 
        &lt;span class="p"&gt;:&lt;/span&gt; 
            &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;paymentInCents&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;priceInCents&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;calculateChangeOwed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeOwed&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;calculateChangeOwed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;changeOwed&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;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;changeOwed&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;denominationArray&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;5&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="mi"&gt;25&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;changeArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;denominationArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduceRight&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;amountOfDenomination&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;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;currentChangeOwed&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;curr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nx"&gt;acc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;amountOfDenomination&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;acc&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;changeArray&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Show your solutions in the comments!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>1.1 Comments </title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Thu, 11 Mar 2021 17:05:17 +0000</pubDate>
      <link>https://dev.to/qmaximillian/rust-by-example-1-1-comments-ik9</link>
      <guid>https://dev.to/qmaximillian/rust-by-example-1-1-comments-ik9</guid>
      <description>&lt;p&gt;This week we're gonna explore comments in Rust. Commenting in Rust serves a couple of purposes. Using &lt;em&gt;regular comments&lt;/em&gt; we can explain how we implemented a solution or why we chose to implement that solution. Using &lt;em&gt;documentation comments&lt;/em&gt; ("doc comments") we can document our code by explicitly describing how to use our code and how our code may behave in certain situations. Doc comments also provide us with some awesome functionality—Rust is able to auto-generate documentation based on the doc comments we made in our code. Whether we use &lt;em&gt;regular comments&lt;/em&gt; or &lt;em&gt;doc comments&lt;/em&gt;, Rust gives us two powerful conventions to explain our code to other humans who write code.&lt;/p&gt;

&lt;p&gt;Let's see how we can take advantage of Rust's comment syntax rules to document our code as we write it. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rust's commenting and documentation conventions &lt;br&gt;
emphasize &lt;a href="https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#use-line-comments" rel="noopener noreferrer"&gt;using line comments over block comments&lt;/a&gt;. Because of this, I'm gonna cover line comments and include some examples of &lt;br&gt;
block comments at the end.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Regular Comments
&lt;/h2&gt;

&lt;p&gt;We can create regular comments in Rust using &lt;code&gt;//&lt;/code&gt; syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// This is a line comment&lt;/span&gt;
&lt;span class="k"&gt;let&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;5&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Documentation Comments
&lt;/h2&gt;

&lt;p&gt;Inner Line Documentation Comments use &lt;code&gt;//!&lt;/code&gt; and are useful for describing high level functionality of an entire &lt;code&gt;crate&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;We can think of a crate in Rust as either a &lt;em&gt;binary&lt;/em&gt; or &lt;em&gt;library&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;A &lt;em&gt;library&lt;/em&gt; crate contains useful utilities we can use within our projects (i.e. &lt;a href="https://docs.rs/rand/0.7.3/rand/" rel="noopener noreferrer"&gt;&lt;code&gt;rand&lt;/code&gt;&lt;/a&gt;, which is a library crate containing utilities for number generation&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;binary crate&lt;/em&gt; which is an executable, a machine code representation of the code we write. (i.e. an app, website, any program we create!)&lt;/p&gt;

&lt;p&gt;Inner Line Documentation Comments use &lt;code&gt;//!&lt;/code&gt; and are mostly used to provide top-level documentation to a crate you create. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/std/primitive.usize.html" rel="noopener noreferrer"&gt;&lt;code&gt;usize&lt;/code&gt;&lt;/a&gt; lets our function know our &lt;code&gt;number&lt;/code&gt; argument has to be number between &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;18446744073709551615&lt;/code&gt;. The &lt;code&gt;usize&lt;/code&gt; after &lt;code&gt;-&amp;gt;&lt;/code&gt; let's Rust know this function returns a number with the type of &lt;code&gt;usize&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// ./src/lib.rs&lt;/span&gt;

    &lt;span class="cd"&gt;//! This inner documentation line comment documents our `./src/lib.rs` file.&lt;/span&gt;
    &lt;span class="cd"&gt;//! We've created a rust library crate which means `./src/lib.rs` &lt;/span&gt;
        &lt;span class="cd"&gt;//! is the entry point of our code.&lt;/span&gt;
    &lt;span class="cd"&gt;//! When we compile our code, it starts with this file!&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&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;Outer Line Documentation Comments use &lt;code&gt;///&lt;/code&gt; and are the preferred way to document any other code you write.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="c1"&gt;// ./src/lib.rs&lt;/span&gt;

    &lt;span class="cd"&gt;//! This inner documentation line comment documents our `lib.rs` file.&lt;/span&gt;
    &lt;span class="cd"&gt;//! We've created a rust library crate which means `lib.rs` is the &lt;/span&gt;
    &lt;span class="cd"&gt;//! entry point of our code.&lt;/span&gt;
    &lt;span class="cd"&gt;//! When we compile our code, it starts with this file!&lt;/span&gt;


    &lt;span class="cd"&gt;/// This function will add the number you pass in by 1&lt;/span&gt;
    &lt;span class="cd"&gt;/// It will then return that number&lt;/span&gt;

    &lt;span class="cd"&gt;/// This is an outer documentation line comment because it exists before&lt;/span&gt;
    &lt;span class="cd"&gt;/// the function `add_five` we have defined below&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_one&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We are using &lt;strong&gt;Inner Line Documentation Comments&lt;/strong&gt; to document the &lt;strong&gt;inside&lt;/strong&gt; of our &lt;code&gt;./src/lib.rs&lt;/code&gt; file and &lt;strong&gt;Outer Line Documentation Comments&lt;/strong&gt; &lt;strong&gt;outside&lt;/strong&gt; of our &lt;code&gt;add_one&lt;/code&gt; function to document our &lt;code&gt;add_one&lt;/code&gt; function. &lt;/p&gt;



&lt;p&gt;Using any form of &lt;em&gt;documentation comment&lt;/em&gt; we can document our code using &lt;a href="https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#using-markdown-1" rel="noopener noreferrer"&gt;Markdown&lt;/a&gt;, specifically the &lt;a href="https://commonmark.org/help/" rel="noopener noreferrer"&gt;CommonMark specification&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;By supporting Markdown, Rust has given us a powerful, uniform convention to help us document our code. To really illustrate this, let's look at the documentation for Rust's standard library, &lt;a href="https://doc.rust-lang.org/stable/std" rel="noopener noreferrer"&gt;&lt;code&gt;std&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We're currently looking at Rust's &lt;code&gt;std&lt;/code&gt; or 'standard' library. Any project we create automatically have access to the entire standard library. &lt;/p&gt;

&lt;p&gt;You should see a page that looks like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99cbgwdt7hagbq0igzt1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F99cbgwdt7hagbq0igzt1.png" alt="std library crate"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Do you see the &lt;code&gt;[src]&lt;/code&gt; in the top right corner? I want you to open that link in a new window and compare the two, maybe like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t2bprhc2l9moddel3zc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6t2bprhc2l9moddel3zc.png" alt="std and std src comparison"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Do you notice anything? &lt;/p&gt;

&lt;p&gt;Do you see how the &lt;code&gt;std&lt;/code&gt; library uses Inner Line Documentation Comments (&lt;code&gt;//!&lt;/code&gt;) to document the top-level of the &lt;code&gt;std&lt;/code&gt; library? &lt;/p&gt;

&lt;p&gt;Do you see how each line with Inner Line Documentation Comments looks similar to the &lt;code&gt;std&lt;/code&gt; library's generated documentation from those comments?&lt;/p&gt;

&lt;p&gt;We can even see some Markdown used in creating an unordered list:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7p5b3i5gs24t3zu88fz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7p5b3i5gs24t3zu88fz.png" alt="unordered list"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Built into Rust we get a powerful convention for documenting the code we write! &lt;/p&gt;



&lt;p&gt;Now that we know the basics of using &lt;em&gt;documentation comments&lt;/em&gt; we'll use &lt;code&gt;cargo&lt;/code&gt; to create a library and some documentation for it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You'll want to have Rust with the &lt;code&gt;cargo&lt;/code&gt; package &lt;br&gt;
manager installed on your local computer to get the &lt;br&gt;
most out of this exercise, but you can also follow &lt;br&gt;
along!&lt;/p&gt;

&lt;p&gt;Check to see if &lt;code&gt;cargo&lt;/code&gt; is installed by running:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;cargo --version&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rust comes with a bunch of tools when you install it, one of them being &lt;code&gt;rustdoc&lt;/code&gt;. &lt;code&gt;rustdoc&lt;/code&gt;'s purpose is to build documentation from our crate's entry point which is called our "crate root". When we build the documentation &lt;code&gt;rustdoc&lt;/code&gt; turns it into HTML, CSS, and Javascript. &lt;code&gt;rustdoc&lt;/code&gt; is a more low-level than &lt;code&gt;cargo&lt;/code&gt;, and luckily &lt;code&gt;cargo&lt;/code&gt; has commands that act as wrappers for &lt;code&gt;rustdoc&lt;/code&gt; to make generating our documentation easier.  If you want to learn more about &lt;code&gt;rustdoc&lt;/code&gt;, you can look at &lt;a href="https://doc.rust-lang.org/rustdoc/index.html" rel="noopener noreferrer"&gt;the &lt;code&gt;rustdoc&lt;/code&gt; book&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now before we generate our documentation website, let's write a simple library with a couple of addition functions. Then we'll document it using all three types of comment!&lt;/p&gt;

&lt;p&gt;Let's run &lt;code&gt;cargo new addition_functions --lib&lt;/code&gt; to create our new Rust library. Then we'll run &lt;code&gt;cd addition_functions&lt;/code&gt;. Open this in your favorite code editor and then open the &lt;code&gt;./src/lib.rs&lt;/code&gt; file and delete the code inside. &lt;/p&gt;

&lt;p&gt;We should see something like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbj8t2iydauox54uhwrq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftbj8t2iydauox54uhwrq.png" alt="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congrats! You've just created a Rust library crate. A Rust library includes functionality that may be used by a binary crate or other library crates. For example, the &lt;code&gt;std&lt;/code&gt; library handles the creation of built-in types for rust. We don't go off on our own and try to build an &lt;code&gt;integer&lt;/code&gt; type, because it would most likely be a waste of time. Rust already provides us with multiple &lt;code&gt;integer&lt;/code&gt; types in it's &lt;code&gt;std&lt;/code&gt; library crate, so let's use them!&lt;/p&gt;

&lt;p&gt;From here let's create a couple of functions. In &lt;code&gt;[./src/lib.rs](http://lib.rs)&lt;/code&gt; we'll add this code:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh61dqtcx9o0ejw5dkiiz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh61dqtcx9o0ejw5dkiiz.png" alt="2"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/std/keyword.pub.html" rel="noopener noreferrer"&gt;&lt;code&gt;pub&lt;/code&gt;&lt;/a&gt; makes our function public so that it will be included in documentation.&lt;/p&gt;

&lt;p&gt;And then we'll run &lt;code&gt;cargo doc --open&lt;/code&gt;. This command will generate our documentation and open it locally in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zovs8tr66vvxi8vdwmf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3zovs8tr66vvxi8vdwmf.png" alt="addition_functions_doc"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;You can also run &lt;code&gt;cargo doc&lt;/code&gt; to build documentation and not open it in a web browser. &lt;/p&gt;

&lt;p&gt;When we use &lt;code&gt;cargo&lt;/code&gt;, our documentation website is built within our &lt;code&gt;./target/doc/[name-of-library]&lt;/code&gt; folder, the entry point being the &lt;code&gt;index.html&lt;/code&gt; file. &lt;/p&gt;

&lt;p&gt;This means an alternative to running &lt;code&gt;cargo doc --open&lt;/code&gt; would be running &lt;code&gt;cargo doc&lt;/code&gt; and then  &lt;code&gt;open target/doc/[name-of-library]/index.html&lt;/code&gt;(in our case &lt;code&gt;[name-of-library]&lt;/code&gt; will be &lt;code&gt;addition_functions&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So we have our two adding functions, now we should write some documentation to describe how they work. Let's use all three types of comments to document the purpose of our library.&lt;/p&gt;

&lt;p&gt;Our Inner Documentation Line Comments to document our entire library: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8g1fepbt6gi13s23sul.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy8g1fepbt6gi13s23sul.png" alt="3"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Our Outer Documentation Line Comments to document our functions:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flotlqc17hbupvjgsxtov.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flotlqc17hbupvjgsxtov.png" alt="4"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;And finally some Regular Comments:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgfo89bvq75llrl5aezd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgfo89bvq75llrl5aezd.png" alt="5"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://gist.github.com/QMaximillian/196c3898835fbcbc4432f2e97f2e98ee" rel="noopener noreferrer"&gt;Here is a full code&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now that we've added some comments, lets re-build our documentation using &lt;code&gt;cargo doc --open&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qjndvh59glnj9rv8eqa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qjndvh59glnj9rv8eqa.png" alt="6"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;We can see our Inner Documentation Line Comments (&lt;code&gt;//!&lt;/code&gt;) which describe the entirety of our library. This is because they are top level within our &lt;code&gt;[./src/lib.rs](http://lib.rs)&lt;/code&gt; file. Rust convention dictates that we only use this type of comment to give an overview of our crate's functionality—a general explanation of what functionality our crate provides.&lt;/p&gt;

&lt;p&gt;Now below that we see our two functions with their Outer Documentation Line Comments (&lt;code&gt;///&lt;/code&gt;). Remember, they are called Outer Documentation Line Comments because they are just before or "outside" of the code they comment on. In this case we're commenting on the &lt;code&gt;add_five&lt;/code&gt; and &lt;code&gt;add_ten&lt;/code&gt; functions respectively. Let's click the &lt;code&gt;add_five&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0og71oenk64h39iaxe97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0og71oenk64h39iaxe97.png" alt="7"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Now we see our function signature in addition to the rest of the outer documentation comments we wrote! &lt;/p&gt;

&lt;p&gt;Finally, we'll check out our source code by clicking &lt;code&gt;[src]&lt;/code&gt; in the top right corner. Now we'll be able to see our regular comments:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvxmmfmfqdb3z96xea2h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxvxmmfmfqdb3z96xea2h.png" alt="8"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Remember, regular comments are useful for other developers working on/developing the library crate, while Outer/Inner Line Documentation is more useful for developers who are using your library crate.&lt;/p&gt;

&lt;p&gt;This is a lot. We haven't even gotten to understanding types in Rust, but it seems very intentional to me that Rust By Example teaches commenting just after "Hello, World!", its first section. Commenting is a way to bridge the communication gap between developers, whether we comment on a line of code or create documentation for an entire library. Comments create documentation in Rust, and documentation can mean the difference between spending 5 minutes on a problem or 5 hours. Rust has excellent documentation. As we keep learning let's take advantage of it. Let's apply the knowledge we've learned today to search documentation for answers and learn Rust as solidly as we can.&lt;/p&gt;
&lt;h1&gt;
  
  
  EXTRA STUFF
&lt;/h1&gt;

&lt;p&gt;We can search for documentation on &lt;a href="https://docs.rs/" rel="noopener noreferrer"&gt;docs.rs&lt;/a&gt;, an open source website that hosts all documentation for crates added to &lt;a href="https://crates.io/" rel="noopener noreferrer"&gt;crates.io&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now let's give our documentation a little flair and learn about some of the ways we can make it more explicit, give it some minimal Markdown styling, and even some alternative syntax.&lt;/p&gt;
&lt;h2&gt;
  
  
  An Alternative Commenting Syntax
&lt;/h2&gt;

&lt;p&gt;Both inner and outer documentation comments are syntactic sugar for the built in &lt;a href="https://doc.rust-lang.org/rustdoc/the-doc-attribute.html" rel="noopener noreferrer"&gt;&lt;code&gt;[doc=""]&lt;/code&gt;&lt;/a&gt; attribute. We can use them interchangeably like so:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type of Comment&lt;/th&gt;
&lt;th&gt;w/o Syntactic Sugar&lt;/th&gt;
&lt;th&gt;w/ Syntactic Sugar&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Inner Line Documentation Comments&lt;/td&gt;
&lt;td&gt;#[doc="This is an inner documentation comment"]&lt;/td&gt;
&lt;td&gt;/// This is an inner documentation comment&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outer Line Documentation Comments&lt;/td&gt;
&lt;td&gt;#![doc"This is an outer documentation comment"]&lt;/td&gt;
&lt;td&gt;//! This is an outer documentation comment&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h2&gt;
  
  
  Markdown Syntax, Code Examples, and Testing
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md" rel="noopener noreferrer"&gt;Some API Conventions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rust crates include some common headings as convention (Errors, Safety, Example) we can use to describe how our code may behave in common use cases. Let's create an Examples header for our &lt;code&gt;add_five&lt;/code&gt;, write a code example, and then test it.&lt;/p&gt;

&lt;p&gt;Using Markdown syntax we can create a heading and called "Examples" above our &lt;code&gt;add_five&lt;/code&gt; function. Let's see what that looks like:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Markdown will default to Rust, but it never hurts to be explicit about the language we are using.&lt;/p&gt;

&lt;p&gt;From here, let's use the &lt;code&gt;assert_eq!&lt;/code&gt; macro to see if our function is working as intended. If the two arguments we pass in are equal, the test will pass. Otherwise, it will fail.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Now if we run &lt;code&gt;cargo test --doc&lt;/code&gt;, we'll only run our documentation test cases. In our console we'll see if the test cases passed or failed. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk57le84pjihpzehryqd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhk57le84pjihpzehryqd.png" alt="9"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Looks like our test cases passed!&lt;/p&gt;

&lt;p&gt;If you're feeling up to it, why don't you write some simple Example documentation for the &lt;code&gt;add_ten&lt;/code&gt; function like we did above, for practice!&lt;/p&gt;




&lt;h2&gt;
  
  
  Block Comment Examples
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Inner Block Documentation Comments
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/*!
        This library has two functions
        that will add an integer to the integer you pass in.
!*/&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_five&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_ten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Outer Block Documentation Comments
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="cd"&gt;/**
        Adds 5 to the integer passed in.
**/&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_five&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
        Adds 10 to the integer passed in.
**/&lt;/span&gt;
&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_ten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Regular Block Comments
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_five&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* 
             I chose to return explicitly here
           for clarity in a blog where we're learning
             how to write comments.
    */&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;add_ten&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;number&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;usize&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="cm"&gt;/* 
             I chose not to return explicitly here
           because we now know we can return explicitly in
       Rust, but we can also not include a ";" (semi-colon)
       and Rust will return that expression.
    */&lt;/span&gt; 
    &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Here's a handy table to remember all three types of comment, whether they are in &lt;em&gt;line or block *&lt;/em&gt;***form.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type of Comment&lt;/th&gt;
&lt;th&gt;Line Comments (Very Common)&lt;/th&gt;
&lt;th&gt;Block Comments (Uncommon)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Regular Comments&lt;/td&gt;
&lt;td&gt;&lt;code&gt;//&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/* ... */&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Inner Line Documentation Comments&lt;/td&gt;
&lt;td&gt;&lt;code&gt;///&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/** ... **/&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outer Line Documentation Comments&lt;/td&gt;
&lt;td&gt;&lt;code&gt;//!&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;&lt;code&gt;/*! ... !*/&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>rust</category>
      <category>tutorial</category>
      <category>motivation</category>
      <category>markdown</category>
    </item>
    <item>
      <title>Type Conversion with String, Number, and Boolean Functions</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Tue, 02 Mar 2021 17:40:05 +0000</pubDate>
      <link>https://dev.to/qmaximillian/type-conversion-with-string-number-and-boolean-functions-2onl</link>
      <guid>https://dev.to/qmaximillian/type-conversion-with-string-number-and-boolean-functions-2onl</guid>
      <description>&lt;p&gt;Whenever we write code, we should make an attempt to organize its meaning in powerful, expressive ways. While code helps us control computers, it is also read by humans and discerning meaning is arguably just as important as how a computer reads it. As we write more and more code, we'll find a need to coerce types, changing one type to a different type. Today we're gonna explore type conversions between &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;boolean&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, and &lt;code&gt;null&lt;/code&gt; types. Javascript gives us ways to make these conversions using the built-in &lt;code&gt;Boolean&lt;/code&gt;, &lt;code&gt;Number&lt;/code&gt;, and &lt;code&gt;String&lt;/code&gt; functions. But how do we use them? And when should you take a different approach when making type conversions? &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;📖 - We'll also throw in &lt;code&gt;NaN&lt;/code&gt; for good measure, &lt;br&gt;
though it is good to note in Javascript &lt;code&gt;NaN&lt;/code&gt; is technically a &lt;code&gt;number&lt;/code&gt; type&lt;/p&gt;

&lt;p&gt;📖 - Before we jump in, it's important to note the &amp;gt; difference between using the  &lt;code&gt;new&lt;/code&gt; operator vs. not using it. &lt;/p&gt;

&lt;p&gt;This applies to three types we are looking at today.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
 * Creates a Primitive Wrapper Object. 
 */&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// String {""}&lt;/span&gt;

&lt;span class="cm"&gt;/* 
 * This Primitive Wrapper Object contains 
 * a set of built-in methods you can call. 
 */&lt;/span&gt;

&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;valueOf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

&lt;span class="c1"&gt;// "Hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* Creates a string */&lt;/span&gt;
&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// "Hello"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  String
&lt;/h2&gt;

&lt;p&gt;First up, we have the &lt;code&gt;String&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  String Conversion
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Boolean (true)&lt;/td&gt;
&lt;td&gt;String(true)&lt;/td&gt;
&lt;td&gt;"true"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boolean (false)&lt;/td&gt;
&lt;td&gt;String(false)&lt;/td&gt;
&lt;td&gt;"false"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number&lt;/td&gt;
&lt;td&gt;String(5)&lt;/td&gt;
&lt;td&gt;"5"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;String("1234")&lt;/td&gt;
&lt;td&gt;"1234"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;String(null)&lt;/td&gt;
&lt;td&gt;"null"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;td&gt;String(undefined)&lt;/td&gt;
&lt;td&gt;"undefined"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NaN&lt;/td&gt;
&lt;td&gt;String(NaN)&lt;/td&gt;
&lt;td&gt;"NaN"&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;no argument passed&lt;/td&gt;
&lt;td&gt;String()&lt;/td&gt;
&lt;td&gt;""&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;String&lt;/code&gt; is probably the most predictable of the three. For primitive data types, the &lt;code&gt;String&lt;/code&gt; function takes in exactly what you type and turns it into a &lt;code&gt;string&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;String(arguments)&lt;/code&gt; is functionally the same as doing &lt;code&gt;value + ""&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Boolean
&lt;/h2&gt;

&lt;p&gt;Now let's see how we can use the &lt;code&gt;Boolean&lt;/code&gt; function!&lt;/p&gt;

&lt;p&gt;Boolean Conversion&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Number (= 0)&lt;/td&gt;
&lt;td&gt;Boolean(0)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number (≠ 0)&lt;/td&gt;
&lt;td&gt;Boolean(1), Boolean(-1000)&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Boolean("Hello")&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String (empty)&lt;/td&gt;
&lt;td&gt;Boolean("")&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;Boolean(null)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;td&gt;Boolean(undefined)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NaN&lt;/td&gt;
&lt;td&gt;Boolean(NaN)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;no argument passed&lt;/td&gt;
&lt;td&gt;Boolean()&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Boolean&lt;/code&gt; will convert falsy values to &lt;code&gt;false&lt;/code&gt; and truthy values to &lt;code&gt;true&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;Boolean&lt;/code&gt; and &lt;code&gt;!!&lt;/code&gt; are interchangeable as they do the same thing. &lt;/p&gt;

&lt;p&gt;For example:&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;differentTypes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;NaN&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1234&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="nx"&gt;differentTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// same as array.filter(x =&amp;gt; Boolean(x))&lt;/span&gt;

&lt;span class="c1"&gt;// [1, true, "1234"]&lt;/span&gt;

&lt;span class="nx"&gt;differentTypes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// [1, true, "1234"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Number
&lt;/h2&gt;

&lt;p&gt;Lastly, let's look at how the &lt;code&gt;Number&lt;/code&gt; function works for common use cases in Javascript.&lt;/p&gt;

&lt;p&gt;Number Conversion&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Example&lt;/th&gt;
&lt;th&gt;Result&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Number (= 0)&lt;/td&gt;
&lt;td&gt;Boolean(0)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Number (≠ 0)&lt;/td&gt;
&lt;td&gt;Boolean(1), Boolean(-1000)&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String&lt;/td&gt;
&lt;td&gt;Boolean("Hello")&lt;/td&gt;
&lt;td&gt;true&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;String (empty)&lt;/td&gt;
&lt;td&gt;Boolean("")&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;null&lt;/td&gt;
&lt;td&gt;Boolean(null)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;undefined&lt;/td&gt;
&lt;td&gt;Boolean(undefined)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NaN&lt;/td&gt;
&lt;td&gt;Boolean(NaN)&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;no argument passed&lt;/td&gt;
&lt;td&gt;Boolean()&lt;/td&gt;
&lt;td&gt;false&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;Number(new Date())&lt;/code&gt; will give us the current date in milliseconds from the &lt;a href="https://en.wikipedia.org/wiki/Unix_time"&gt;epoch&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Number&lt;/code&gt; shines when making conversions from a &lt;code&gt;string&lt;/code&gt; representation of a &lt;code&gt;number&lt;/code&gt;, into a &lt;code&gt;number&lt;/code&gt;. For example, let's look at a common case:&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/quinnlashinsky/embed/OJbZBeX?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;When we increment our value without using the &lt;code&gt;Number&lt;/code&gt; function, because our value is a &lt;code&gt;string&lt;/code&gt; type it will concatenate. &lt;/p&gt;

&lt;p&gt;In Javascript, &lt;code&gt;"0" + 1 === "01"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When we increment our value using the &lt;code&gt;Number&lt;/code&gt; function, because we convert our value to a &lt;code&gt;number&lt;/code&gt; type, we get our expected behavior.&lt;/p&gt;

&lt;p&gt;In Javascript, &lt;code&gt;0 + 1 === 1&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Number vs. parseInt/parseFloat
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Number&lt;/code&gt; is wonderful for simple &lt;code&gt;string&lt;/code&gt; to &lt;code&gt;number&lt;/code&gt; conversions, but &lt;code&gt;parseInt&lt;/code&gt; or &lt;code&gt;parseFloat&lt;/code&gt; may be a more robust option if you are changing values with a unit attached.&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="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 100&lt;/span&gt;
&lt;span class="nb"&gt;parseFloat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100.23&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 100.23&lt;/span&gt;

&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100px&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's important to note that &lt;code&gt;parseInt&lt;/code&gt;/&lt;code&gt;parseFloat&lt;/code&gt; will only parse numbers up until it reaches a non-number, ignoring leading or trailing whitespace.&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;// parseInt and parseFloat yield the same results in this example&lt;/span&gt;

&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1a00&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 1&lt;/span&gt;

&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;a100&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1a00&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// NaN&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This makes it so you can only use hexadecimal, octal, or binary numbers using their full &lt;code&gt;string&lt;/code&gt; representation while using &lt;code&gt;parseInt&lt;/code&gt;'s second argument, &lt;em&gt;radix.&lt;/em&gt; &lt;code&gt;parseFloat&lt;/code&gt; does not take any arguments.&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;// Both function calls should return a binary representation of the number, 4&lt;/span&gt;

&lt;span class="c1"&gt;// Works as expected &lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;100&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;//4&lt;/span&gt;

&lt;span class="c1"&gt;// Does not work as expected&lt;/span&gt;
&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0b100&lt;/span&gt;&lt;span class="dl"&gt;"&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="c1"&gt;// 0 &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;Through learning about the &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Boolean&lt;/code&gt;, and &lt;code&gt;Number&lt;/code&gt; functions, we've learned when it might make sense to use them, and when an alternative is better. Learning code involves adding new information to your toolbox, employing you to write more semantic, expressive, easily readable code. Let these three functions be another piece helping you learn and build with code.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
      <category>programming</category>
    </item>
    <item>
      <title>Introduction and 1.0 - Hello World</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Thu, 25 Feb 2021 18:33:45 +0000</pubDate>
      <link>https://dev.to/qmaximillian/introduction-and-1-0-hello-world-d5c</link>
      <guid>https://dev.to/qmaximillian/introduction-and-1-0-hello-world-d5c</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/stable/rust-by-example/index.html"&gt;Rust By Example - Introduction&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the first page of Rust By Example, we're greeted with this statement:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Rust is a modern systems programming language focusing on safety, speed, and concurrency.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Awesome. But what does this mean?&lt;/p&gt;

&lt;p&gt;Over the course of completing these exercises we'll explore how Rust language prioritizes and implements these qualities.&lt;/p&gt;

&lt;h2&gt;
  
  
  Hello World (Chapter 1.0)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://doc.rust-lang.org/rust-by-example/hello.html"&gt;Rust By Example - Hello World!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PzwcyZ7f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83jt6o211davgnerp1og.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PzwcyZ7f--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/83jt6o211davgnerp1og.png" alt="Screen_Shot_2021-02-24_at_6.47.23_PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It's tradition to create a "Hello World" program whenever you learn a new coding language. But before we do, let's take a look at the top right of our in browser coding environment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--74xrXQqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb2xuamt4e91uoulze1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--74xrXQqc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wb2xuamt4e91uoulze1f.png" alt="Screen_Shot_2021-02-24_at_6.47.42_PM"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We can reset the code to it's original state by clicking the leftmost button&lt;/li&gt;
&lt;li&gt;We can copy all of the code by clicking the middle button&lt;/li&gt;
&lt;li&gt;We can run the code by clicking the rightmost button&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we click run we should see &lt;code&gt;Hello World!&lt;/code&gt; below&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;println!&lt;/code&gt; &lt;code&gt;macro&lt;/code&gt; prints our text to the console. A &lt;code&gt;macro&lt;/code&gt; in Rust isn't a function call although it may look like one. Instead, the contents of the &lt;code&gt;macro&lt;/code&gt; expand into source code. That way the body of the macro explicitly writes code which is eventually executed. This is a form of meta programming. We are writing code (&lt;code&gt;println!&lt;/code&gt;) that generates other code (code we put inside of &lt;code&gt;println!&lt;/code&gt;). We'll look at &lt;code&gt;macros&lt;/code&gt; more in a future post! &lt;/p&gt;

&lt;p&gt;Lastly, we are given instructions on how to create a Rust binary in our local dev environment. Follow the instructions and you will have created an executable binary which will print out "Hello World!". An executable binary is a machine code version of any code we write. In this case, it's our "Hello World!" code! &lt;/p&gt;

&lt;p&gt;We've completed our Hello World example! &lt;/p&gt;

&lt;p&gt;In the next post, we'll cover the next chapter, 1.1 - Comments&lt;/p&gt;

</description>
      <category>rust</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Starting Rust By Example</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Thu, 25 Feb 2021 18:22:03 +0000</pubDate>
      <link>https://dev.to/qmaximillian/starting-rust-by-example-4161</link>
      <guid>https://dev.to/qmaximillian/starting-rust-by-example-4161</guid>
      <description>&lt;p&gt;When I first started to learn how to code I remember being astounded by what I was able to do. Each new concept building on top of the previous, ultimately leading to the ability to build websites and web apps. I picked up Ruby, then Javascript, and while I became more infatuated with Javascript, in the back of my mind I knew I wanted to learn a more low-level language. I had been hearing about Rust for a bit, but wasn't sure. I thought I should probably try to learn Java, C, or C++. There were a couple of reasons that swayed me against this, and here I am making another attempt at learning Rust.&lt;/p&gt;

&lt;p&gt;What were these reasons? &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.rust-lang.org/community"&gt;The Rust Community&lt;/a&gt;:&lt;br&gt;&lt;br&gt;
The Rust community has always felt welcoming and open. I don't feel the same negativity that can pervade communities around other languages.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Tooling Around Rust: &lt;br&gt;&lt;br&gt;
Rust is easy to install, provides useful built-in standardized utilities like testing and crate documentation and setting up Rust on my computer was simple.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Safety And Convenience Of Modern Programming Languages, With The Efficiency And Low Level Control That C and C++ Offer:&lt;br&gt;&lt;br&gt;
Rust has it's own set of programming paradigms that help ensure issues like managing memory are made easier to manage&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://webassembly.org/"&gt;WebAssembly&lt;/a&gt; - The Future of the Web:&lt;br&gt;&lt;br&gt;
WebAssembly looks to be the future of the web, looking to provide us with the ability to use any language to run code in the browser. Rust has already been involved, with tooling being defined for the spec.&lt;br&gt;
&lt;br&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Interoperability Among Types of Programming, specifically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Backend Engineering&lt;/li&gt;
&lt;li&gt;Frontend Engineering&lt;/li&gt;
&lt;li&gt;Systems Engineering&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It really feels like Rust covers almost any area of programming I am or may become interested in!&lt;br&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  How We'll Learn
&lt;/h2&gt;

&lt;p&gt;The Rust website has a few recommended ways to start with Rust. &lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/stable/book/ch00-00-introduction.html"&gt;The Rust Book&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;An awesome resource that gives an overview of the language. I tried learning from the book first, but ultimately the ratio of reading to coding made it hard for me to stay engaged. We'll keep this in mind, and reference the book when necessary!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://github.com/rust-lang/rustlings#getting-started"&gt;The Rustlings Course&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Great for doing Rust based exercises on your machine, but it's less hands on. I found I was either applying or researching knowledge I found in the Rust Book to solve each problem and became stuck regularly. You are thrown into the deep end, and to some degree, must figure out how to solve each problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;a href="https://doc.rust-lang.org/rust-by-example/index.html"&gt;Rust By Example&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;We are going to use Rust By Example. It combines elements of the Rust Book while incorporating coding exercises. You can do exercises in the browser using the &lt;a href="https://play.rust-lang.org/"&gt;Rust Playground&lt;/a&gt;, but I suggest you &lt;a href="https://www.rust-lang.org/tools/install"&gt;install Rust&lt;/a&gt; on your local machine. You can even find editor support &lt;a href="https://www.rust-lang.org/tools"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rust has awesome built-in tooling and we can reference the Rust Book when working through problems.&lt;/p&gt;

&lt;p&gt;We're going to cover each chapter in order, including the linked resources at the bottom in depth. &lt;/p&gt;

&lt;p&gt;Here's to joining me on this journey to learn Rust!&lt;/p&gt;

</description>
      <category>rust</category>
      <category>codenewbie</category>
      <category>tutorial</category>
      <category>motivation</category>
    </item>
    <item>
      <title>Foreign Key Constraints with knex.js to Ensure Database Atomicity</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Tue, 23 Feb 2021 16:25:31 +0000</pubDate>
      <link>https://dev.to/qmaximillian/foreign-key-constraints-with-knex-js-to-ensure-database-atomicity-3j6c</link>
      <guid>https://dev.to/qmaximillian/foreign-key-constraints-with-knex-js-to-ensure-database-atomicity-3j6c</guid>
      <description>&lt;p&gt;Over the course of building Flashcards, my Quizlet-like clone, I wanted to make sure that if I delete a &lt;code&gt;card_set&lt;/code&gt; all of its &lt;code&gt;flashcards&lt;/code&gt; would be deleted as well. I tried to handle this myself, but this became a nightmare of bad logic and edge cases. Eventually, I took a step back and tried to examine the problem I was really trying to solve. My database is my single source of truth for all data in my application. Is it possible for my database to safely handle the above operation? It is! By adding foreign key constraints to my &lt;code&gt;knex&lt;/code&gt; migrations I can take steps to ensure database atomicity within my Postgres database. &lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Database Atomicity?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"An atomic transaction is an indivisible and irreducible series of database operations such that either all occur, or nothing occurs."&lt;/em&gt; - &lt;a href="[https://en.wikipedia.org/wiki/Atomicity_(database_systems)](https://en.wikipedia.org/wiki/Atomicity_(database_systems))"&gt;Wikipedia&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This means my database will guarantee that if I try and delete a &lt;code&gt;card_set&lt;/code&gt; and its &lt;code&gt;flashcards&lt;/code&gt;, either that &lt;code&gt;card_set&lt;/code&gt; and it's &lt;code&gt;flashcards&lt;/code&gt; are both deleted, or neither are deleted.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is A Foreign Key?
&lt;/h2&gt;

&lt;p&gt;A foreign key is a reference to the primary key of another table. Foreign keys make it easy for us to establish relationships between tables. &lt;/p&gt;

&lt;p&gt;Before we jump into the code, let's look at the relationship I have setup for my &lt;code&gt;card_sets&lt;/code&gt; and &lt;code&gt;flashcards&lt;/code&gt;. My &lt;code&gt;card_sets&lt;/code&gt; table is a parent table to my &lt;code&gt;flashcards&lt;/code&gt; child table. &lt;/p&gt;

&lt;p&gt;A parent can have many children but a child can only have one parent, just like a &lt;code&gt;card_set&lt;/code&gt; can have many &lt;code&gt;flashcards&lt;/code&gt;, but a &lt;code&gt;flashcard&lt;/code&gt; can only belong to a singular &lt;code&gt;card_set&lt;/code&gt;. This relationship can also be described as a "has many/belongs" to relationship. A &lt;code&gt;card_set&lt;/code&gt; has many &lt;code&gt;flashcards&lt;/code&gt;, but a &lt;code&gt;flashcard&lt;/code&gt; belongs to only one &lt;code&gt;card_set&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpb14m5dgsgdiirix2q97.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpb14m5dgsgdiirix2q97.png" alt="card_sets and flashcards relationship"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;In order to establish this "parent/child" or "has many/belongs to" relationship, I have to set up foreign key constraints. This is what my foreign key constraint looks like with &lt;code&gt;knex&lt;/code&gt;, the SQL query builder I am using. &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;// Part of My Flashcards Migration File&lt;/span&gt;

  &lt;span class="c1"&gt;// This code creates my "card_set_id" column as a &lt;/span&gt;
  &lt;span class="c1"&gt;// not nullable UUID type&lt;/span&gt;
    &lt;span class="nx"&gt;table&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card_set_id&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="nf"&gt;notNullable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;references&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;id&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="nf"&gt;inTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;card_sets&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="nf"&gt;onDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;CASCADE&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


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

&lt;/div&gt;

&lt;p&gt;These three functions will help us define our foreign keys and their behavior:&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="[https://knexjs.org/#Schema-references](https://knexjs.org/#Schema-references)"&gt;&lt;code&gt;column.references(column)&lt;/code&gt;&lt;/a&gt; function tells us what primary key value to reference as our foreign key. &lt;/p&gt;

&lt;p&gt;In our code above we are saying we are going to reference the &lt;code&gt;id&lt;/code&gt; column in some table (we haven't defined which table yet!) as the primary key to our &lt;code&gt;card_set_id&lt;/code&gt; foreign key. &lt;/p&gt;

&lt;p&gt;Now we have to specify which table's &lt;code&gt;id&lt;/code&gt; column we would like to use the primary key value from.&lt;/p&gt;

&lt;h3&gt;
  
  
  inTable
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="[https://knexjs.org/#Schema-inTable](https://knexjs.org/#Schema-inTable)"&gt;&lt;code&gt;column.inTable(table)&lt;/code&gt;&lt;/a&gt; function lets us define exactly which table we'll get our primary key value to use as a foreign key.&lt;/p&gt;

&lt;p&gt;In our code above we explicitly use the &lt;code&gt;card_sets&lt;/code&gt; table. Now we have bound the two, and formed a relationship between tables&lt;/p&gt;

&lt;p&gt;&lt;code&gt;flashcards.card_set_id&lt;/code&gt; is the foreign key to &lt;code&gt;card_sets.id&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  onDelete
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="[https://knexjs.org/#Schema-onDelete](https://knexjs.org/#Schema-onDelete)"&gt;&lt;code&gt;column.onDelete(command)&lt;/code&gt;&lt;/a&gt; function allows us to define how we'd like all child rows with this foreign key (&lt;code&gt;flashcards.card_set_id&lt;/code&gt;) to behave when their referenced parent row is deleted (&lt;code&gt;card_sets.id&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;I'm working with Postgres, keep in mind not all SQL database implementations will accept these commands:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CASCADE&lt;/strong&gt; - When the parent row is deleted, all children rows referencing the parent are deleted as well&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SET NULL&lt;/strong&gt; - When the parent row is deleted, all children rows with a foreign key referencing the parent row will be set to the value "NULL"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RESTRICT&lt;/strong&gt; - If a child row references a parent row, no parent and or child rows will be deleted&lt;/li&gt;
&lt;li&gt;&lt;a href="[https://www.postgresql.org/message-id/4271.1233022978%40sss.pgh.pa.us](https://www.postgresql.org/message-id/4271.1233022978%40sss.pgh.pa.us)"&gt;NO ACTION&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Guaranteed Database Atomicity
&lt;/h2&gt;

&lt;p&gt;Now the problem is solved! If I delete a &lt;code&gt;card_set&lt;/code&gt; my PostgreSQL database will guarantee that all &lt;code&gt;flashcards&lt;/code&gt; associated with that &lt;code&gt;card_set&lt;/code&gt; are deleted as well. &lt;/p&gt;

&lt;p&gt;Postgres will handle the entire transaction. If the transaction fails, my &lt;code&gt;card_set&lt;/code&gt; and its associated &lt;code&gt;flashcards&lt;/code&gt; will return to the state they were in before the transaction occurred. If the transaction succeeds, then I know both the &lt;code&gt;card_set&lt;/code&gt; and associated &lt;code&gt;flashcards&lt;/code&gt; are deleted!&lt;/p&gt;

</description>
      <category>postgres</category>
      <category>database</category>
      <category>knex</category>
      <category>atomicity</category>
    </item>
    <item>
      <title>Snow Days and Javascript Promises</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Wed, 10 Feb 2021 18:42:05 +0000</pubDate>
      <link>https://dev.to/qmaximillian/snow-days-and-javascript-promises-48id</link>
      <guid>https://dev.to/qmaximillian/snow-days-and-javascript-promises-48id</guid>
      <description>&lt;p&gt;Your eyes are glued to the TV. You watch the news in awe, just waiting for them to get to the weather forecast. You haven't had a snow day all year, and you're hoping that tomorrow will be the first. You think of all the things you'll be able to do—Drink hot chocolate, watch a movie, sleep in, sled, build a snowman with a bunch of friends. It all sounds so amazing.&lt;/p&gt;

&lt;p&gt;Finally, the weather forecast comes on and they are promising snow tomorrow!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;snowPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;// Our Promise function&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all we can do is wait! We don't know whether it will snow or not, and we won't know until tomorrow. We then find out if it snows our school district will announce school closures at 7am tomorrow! It's currently 6pm. We have 13 hours until this prediction proves to be true or false!&lt;/p&gt;

&lt;p&gt;You are elated. So happy, you almost miss the forecaster tell you that there's only 30% chance of snow happening. If it snows, school will be closed.&lt;/p&gt;

&lt;p&gt;You're gonna be absolutely devastated if it doesn't!&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;willItSnow&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;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.3&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;snowPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;willItSnow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School's Closed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School's Open&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="mi"&gt;46800000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 13 hours in milliseconds&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;snowPromise&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Promise {&amp;lt;pending&amp;gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It looks like things are in motion! Our &lt;code&gt;snowPromise&lt;/code&gt; will act as a placeholder, waiting for an asynchronous operation to complete (in our case a &lt;code&gt;setTimeout&lt;/code&gt;), resolving or rejecting with data. In our case, 13 hours later.&lt;/p&gt;

&lt;p&gt;That's a long time to wait...what're we gonna do between now and our predicted snowfall?&lt;/p&gt;

&lt;p&gt;If we didn't use a &lt;code&gt;Promise&lt;/code&gt; we wouldn't be able to do things like perform our snow-bringing bed time rituals. We would be blocked from doing anything else. We'd just sit on the ground waiting to hear if school is closed or not for 13 HOURS. This sounds like a huge waste of time!&lt;/p&gt;

&lt;p&gt;The asynchronous nature of a &lt;code&gt;Promise&lt;/code&gt; lets us run other code while we wait for our &lt;code&gt;Promise&lt;/code&gt; to resolve or reject. While this happens, we can go ahead with leaving a spoon under our pillow and flushing ice cubes down the toilet. This will definitely ensure we get snow tomorrow!&lt;/p&gt;

&lt;p&gt;It's been an exciting day and we still don't know whether it will or won't snow.&lt;/p&gt;

&lt;p&gt;To get ready, we'll turn our PJ's inside out and look forward to our &lt;code&gt;snowPromise&lt;/code&gt; result in the morning!&lt;/p&gt;

&lt;h1&gt;
  
  
  Next Morning
&lt;/h1&gt;

&lt;p&gt;We wake up! We're excited! But we're unsure of whether school is closed or not. We need to hear it from the source. But how do we find our information?! Listening to the radio, tv, or reading information on the internet may help us figure out if school is closed or not. These are conduits for receiving the information, much like &lt;code&gt;Promise&lt;/code&gt; methods we are going to discuss below!&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Promise&lt;/code&gt;'s have a few methods that will allow us to handle our eventual returned result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise Methods
&lt;/h2&gt;

&lt;p&gt;We can handle a &lt;code&gt;Promise&lt;/code&gt; using 3 different types of promise handlers; &lt;code&gt;.then()&lt;/code&gt;, &lt;code&gt;.catch()&lt;/code&gt;, &lt;code&gt;.finally()&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Then
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.then(onFulfilled, onRejected)&lt;/code&gt; - This method will let us handle a success and error cases, which are technically called our &lt;code&gt;onFulfilled&lt;/code&gt; and &lt;code&gt;onRejected&lt;/code&gt; handlers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both of these parameters must be functions.&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;willItSnow&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;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.3&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;snowPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;willItSnow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School's Closed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School's Open&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="c1"&gt;// We'll use 1 second here and going forward so we don't have to wait for&lt;/span&gt;
  &lt;span class="c1"&gt;// 13 hours for our Promise to resolve&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;snowPromise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="c1"&gt;// onFulfilled&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="c1"&gt;// onRejected&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;error&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;// If it snows, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Closed"&lt;/span&gt;

&lt;span class="c1"&gt;// If it doesn't snow, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Open"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If our &lt;code&gt;snowPromise&lt;/code&gt; &lt;code&gt;resolve&lt;/code&gt;'s, it will pass any arguments we passed to our &lt;code&gt;resolve&lt;/code&gt; function to our &lt;code&gt;onFulfilled&lt;/code&gt; handler function.&lt;/p&gt;

&lt;p&gt;If our &lt;code&gt;snowPromise&lt;/code&gt; &lt;code&gt;reject&lt;/code&gt;'s, we'll pass any arguments we passed to our &lt;code&gt;reject&lt;/code&gt; function to our &lt;code&gt;onRejected&lt;/code&gt; handler function.&lt;/p&gt;

&lt;p&gt;Finally, we're able to tell whether school is closed or not!&lt;/p&gt;

&lt;p&gt;Put the above code into your chosen Web Browser's console or a program like &lt;a href="https://runjs.app/"&gt;RunJS&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Is School Closed?! That's amazing! That means our &lt;code&gt;Promise&lt;/code&gt; resolved and our &lt;code&gt;onFulfilled&lt;/code&gt; function ran! Let's go play in the snow!&lt;/p&gt;

&lt;p&gt;Is School Open?! That's unfortunate! That means our &lt;code&gt;Promise&lt;/code&gt; rejected and our &lt;code&gt;onRejected&lt;/code&gt; function ran. Time to get ready for school...&lt;/p&gt;

&lt;p&gt;This may seem a bit cluttered to you though. It may be useful to have both possible paths within our &lt;code&gt;.then()&lt;/code&gt; handler, but we can also use a different method to handle our &lt;code&gt;onRejected&lt;/code&gt; function...&lt;/p&gt;

&lt;h3&gt;
  
  
  Catch
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.catch(onRejected)&lt;/code&gt; - This method will let us handle our error case, which is technically called our &lt;code&gt;onRejected&lt;/code&gt; handler
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;willItSnow&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;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.3&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;snowPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;willItSnow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School Closed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School Open&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;snowPromise&lt;/span&gt;
  &lt;span class="c1"&gt;// onFulfilled&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="c1"&gt;// onRejected&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// If it snows, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Closed"&lt;/span&gt;

&lt;span class="c1"&gt;// If it doesn't snow, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Open"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method makes it easier to break apart our success and failure/error states!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;&lt;em&gt;TIP: We can even chain a bunch of &lt;code&gt;.then()&lt;/code&gt;'s together and add a single &lt;code&gt;.catch()&lt;/code&gt; at the end to handle any error from our &lt;code&gt;Promise&lt;/code&gt; or any previous &lt;code&gt;.then()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;Lastly, we know we'll always want more snow. Multiple snow days in a row? That sounds like heaven!&lt;/p&gt;

&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.finally(onFinally)&lt;/code&gt; - This &lt;code&gt;Promise&lt;/code&gt; method allows us to execute some code whether or not our Promise &lt;code&gt;resolve&lt;/code&gt;'s or &lt;code&gt;reject&lt;/code&gt;'s.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;willItSnow&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;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mf"&gt;0.3&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;snowPromise&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;willItSnow&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School Closed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;School Open&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="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;snowPromise&lt;/span&gt;
  &lt;span class="c1"&gt;// onFulfilled&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&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="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="c1"&gt;// onRejected&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&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="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;finally&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="s2"&gt;🤞🏽⛄️ PLEASE SNOW TOMORROW ⛄️🤞🏽&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// If it snows, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Closed"&lt;/span&gt;
&lt;span class="c1"&gt;// "🤞🏽⛄️ PLEASE SNOW TOMORROW ⛄️🤞🏽"&lt;/span&gt;

&lt;span class="c1"&gt;// If it doesn't snow, below will be logged to the console:&lt;/span&gt;
&lt;span class="c1"&gt;// "School's Open"&lt;/span&gt;
&lt;span class="c1"&gt;// "🤞🏽⛄️ PLEASE SNOW TOMORROW ⛄️🤞🏽"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well...are you going to school today? Or do you have the day off? Either way, we'll always hope for more snow.&lt;/p&gt;

&lt;p&gt;Let's bring this home with some final considerations to remember when using Promises.&lt;/p&gt;

&lt;h3&gt;
  
  
  Final Considerations
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;In Javascript, all asynchronous code will only run if there are no other functions on the &lt;a href="https://blog.usejournal.com/everything-you-need-to-know-about-event-loop-in-javascript-1f14f94e5ab6"&gt;Call Stack&lt;/a&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// 2&lt;/span&gt;
&lt;span class="c1"&gt;// 1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;em&gt;If you want access to a result in multiple chained &lt;code&gt;.then()&lt;/code&gt; methods, you must return the result from each &lt;code&gt;.then()&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No Return -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Resolve Function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`1 - &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`2 - &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;// "1 - Resolve Function"&lt;/span&gt;
&lt;span class="c1"&gt;// "2 - undefined"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Return -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&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;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Resolve Function&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`1 - &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;// "1 - Resolve Function"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❄️ Now let's get back to having a snowball fight! ❄️&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Summing an Array with Nested Arrays</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Tue, 26 Jan 2021 15:04:34 +0000</pubDate>
      <link>https://dev.to/qmaximillian/summing-an-array-with-nested-arrays-4lng</link>
      <guid>https://dev.to/qmaximillian/summing-an-array-with-nested-arrays-4lng</guid>
      <description>&lt;p&gt;(Throughout this article I will be using the terms &lt;code&gt;list&lt;/code&gt; and &lt;code&gt;array&lt;/code&gt; interchangeably. The Python &lt;code&gt;list&lt;/code&gt; type is called an &lt;code&gt;array&lt;/code&gt; in many other programming languages)&lt;/p&gt;

&lt;p&gt;Summing an array can be done in many ways. In Python we can use a &lt;code&gt;for...in&lt;/code&gt; loop. We even have a built in &lt;code&gt;sum&lt;/code&gt; function we can use to sum an array. These solutions work perfectly for a 1D array, but how can we sum a 2D array? Let's break this problem into smaller pieces and solve it.&lt;/p&gt;

&lt;p&gt;Today we are going to solve two problems—summing an array and any of its nested arrays&lt;/p&gt;

&lt;p&gt;First, let's sum an array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;sum&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="c1"&gt;# 15
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well that was easy! This helper function provides an easy way to sum all values, but what is the &lt;code&gt;sum&lt;/code&gt; function actually doing? Something like this maybe?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;number&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumTotal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each &lt;code&gt;number&lt;/code&gt; in the array will be added to the current &lt;code&gt;sumTotal&lt;/code&gt; and then we set that value as the new &lt;code&gt;sumTotal&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;After we finish iterating, we return the &lt;code&gt;sumTotal&lt;/code&gt;. We've accomplished the first step of our goal! Now on to the next step, summing any arrays within our array.&lt;/p&gt;

&lt;p&gt;But wait a second, haven't we already figured out how to sum an array? Let's write out the problem in pseudo-code&lt;/p&gt;

&lt;p&gt;Currently, we have figured out this much:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumArrayofArrays&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# create variable to hold sum of array (`sumTotal`)
&lt;/span&gt;    &lt;span class="c1"&gt;# iterate through array and add each number to the sum
&lt;/span&gt;    &lt;span class="c1"&gt;# return the sum
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know this function will sum an array, so how can we call it on each nested array?&lt;/p&gt;

&lt;h3&gt;
  
  
  Number or Array?
&lt;/h3&gt;

&lt;p&gt;Right now in our loop, we're calling our variable &lt;code&gt;number&lt;/code&gt;, but we know it may not always be a number. For example, if this is our array:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="n"&gt;array&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="c1"&gt;# 1 - Number
&lt;/span&gt;&lt;span class="n"&gt;array&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;# 2 - Number
&lt;/span&gt;&lt;span class="n"&gt;array&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="c1"&gt;# 3 - Number
&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 4 - Array
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 3rd position in our example is a &lt;code&gt;list&lt;/code&gt; type! Let's update our loops variable name in order to be more accurate. Let's use &lt;code&gt;element&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumTotal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We now know an &lt;code&gt;element&lt;/code&gt; can either be a &lt;code&gt;list&lt;/code&gt; or &lt;code&gt;int&lt;/code&gt; type. If an &lt;code&gt;element&lt;/code&gt; is an &lt;code&gt;int&lt;/code&gt; we want to add it to the &lt;code&gt;sumTotal&lt;/code&gt; which we're currently doing, but is there a way to check if an &lt;code&gt;element&lt;/code&gt; is a &lt;code&gt;list&lt;/code&gt;?&lt;/p&gt;

&lt;h3&gt;
  
  
  Type()?
&lt;/h3&gt;

&lt;p&gt;In Python, we can determine the type of anything by using the type function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;class 'int'&amp;gt;
&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;([])&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;class 'list'&amp;gt;
&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# &amp;lt;class 'type'&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type function will allow us to check if an &lt;code&gt;element&lt;/code&gt; is a &lt;code&gt;list&lt;/code&gt;. Let's add that check to our function!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;# do something
&lt;/span&gt;        &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumTotal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Whenever we reach a &lt;code&gt;list&lt;/code&gt; in our array, we know we want to sum all values within that our nested arrays, but how can we do this?&lt;/p&gt;

&lt;p&gt;Well, we've already defined a function that can iterate through a &lt;code&gt;list&lt;/code&gt; and return it's sum, so we can call our &lt;code&gt;sumOfArray&lt;/code&gt; function recursively and solve our problem outright!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;is&lt;/span&gt; &lt;span class="nb"&gt;list&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;list&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="n"&gt;sumTotal&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;element&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;sumTotal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we've defined our function and walked through the process used to create it, let's take a look, step by step and look at what is happening on each line. I know that I find it so much easier to conceptualize and visualize code execution by stepping through the code line by line. Using a debugger or getting a pen and paper tend to be the most helpful for me.&lt;/p&gt;

&lt;p&gt;Let's take a simple example incorporating an array with a number and a nested array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# array = [1, [2]]
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'll use VSCode's built-in Run and Debug tab and take it step by step and explore the space and time complexity of our function.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/MC3MHLV761Q"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;On the left side of the screen we can see two important parts of our code execution. In the top left we can see how our variables change over time and in the bottom left we can see our call stack.&lt;/p&gt;

&lt;p&gt;Ultimately, because we are going to access all values in the array, this means our Time Complexity is O(&lt;em&gt;n&lt;/em&gt;) with &lt;em&gt;n&lt;/em&gt; being the length of the array&lt;/p&gt;

&lt;p&gt;Out Space Complexity is dependent on the max amount of calls we have on the call stack at a given time. &lt;/p&gt;

&lt;p&gt;Every time we call a new function, it is put on the call stack. When the function finishes executing, we remove or "pop" it from the call stack. Each function call takes up space on the call stack. &lt;/p&gt;

&lt;p&gt;This means for each array that has a nested array, we will call our &lt;code&gt;sumOfArray&lt;/code&gt; function and add it to the call stack. This means the depth of the most nested array, which is directly related to the amount of &lt;code&gt;sumOfArray&lt;/code&gt; function calls on the call stack, is our Space complexity. &lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Each nested array pushes another `sumArray` function to onto the stack
&lt;/span&gt;
&lt;span class="n"&gt;firstArray&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]]&lt;/span&gt;

&lt;span class="c1"&gt;# Max Depth - 2
&lt;/span&gt;
&lt;span class="n"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="c1"&gt;# 3
# Time Complexity - O(n)
# Space Complexity - O(d) (Here 'd' is 2)
&lt;/span&gt;
&lt;span class="n"&gt;secondArray&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]]]&lt;/span&gt;

&lt;span class="c1"&gt;# Max Depth - 3
&lt;/span&gt;
&lt;span class="n"&gt;sumOfArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secondArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# 6
# Time Complexity - O(n)
# Space Complexity - O(d) (Here 'd' is 3)
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Feel free to play around with code! Run it in your own local VSCode setup or even write out each step. &lt;/p&gt;

&lt;h3&gt;
  
  
  Recursion
&lt;/h3&gt;

&lt;p&gt;To solve our problem we used a programming technique called recursion. &lt;/p&gt;

&lt;p&gt;Recursion allows us to break our problem down into its smallest pieces, solve those smallest pieces, which will help us find our final result.&lt;/p&gt;

&lt;p&gt;Every time an element is of type &lt;code&gt;list&lt;/code&gt; , a new call to &lt;code&gt;sumOfArray&lt;/code&gt; will be added to the stack&lt;/p&gt;

&lt;p&gt;If a list has no nested arrays it will return it's sum.&lt;/p&gt;

&lt;p&gt;If it has a nested array, when that array is reached during iteration, it will add another call to the stack.&lt;/p&gt;

&lt;p&gt;This will repeat until we reach the most deeply nested array. Then as each call returns it's array's sum, each &lt;code&gt;sumOfArray&lt;/code&gt; function on the stack will be popped off, eventually returning us the sum of all values in the original array and it's nested arrays.&lt;/p&gt;

&lt;p&gt;I highly recommend using pen and paper and a simple input array to see exactly how recursion has helped us solve the our problem here. It can be a highly useful tool to help us solve problems like this, where it seems hard to find an iterative solution!&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>Steps to Creating A Compose Function</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Wed, 08 Jul 2020 17:16:04 +0000</pubDate>
      <link>https://dev.to/qmaximillian/steps-to-creating-a-compose-function-1kf5</link>
      <guid>https://dev.to/qmaximillian/steps-to-creating-a-compose-function-1kf5</guid>
      <description>&lt;p&gt;Functional composition looks something like this.&lt;br&gt;
&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;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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;second&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&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;second&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;

&lt;span class="c1"&gt;// 4&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We work from inner to outer. In the example above, we call the innermost function, first(1), and pass the result of that function, 2, to the invoked second function. When we call the second function it'll look like this: second(2). Finally, when second(2) executes we get our returned value, 4. We've composed a bunch of simple functions to build more complicated ones.&lt;/p&gt;

&lt;p&gt;Using functional composition we can break our code into smaller reusable pieces. We can then use those pieces as building blocks for creating larger functions. Each piece being a set of instructions, clearly indicating exactly how we are manipulating our data. But how can we create a compose function?&lt;/p&gt;

&lt;p&gt;Let's build our model up in pieces. We'll look at the idea of a function as a first class citizen, and what that means in Javascript. &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;MDN says, &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A programming language is said to have First-class functions &lt;br&gt;
when functions in that language are treated like any other&lt;br&gt;
variable. For example, in such a language, a function can be &amp;gt; passed as an argument to other functions, can be returned by &amp;gt; another function and can be assigned as a value to a &lt;br&gt;
variable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;Two takeaways here. In order for a language to have first-class functions, functions must be able to be:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Passed as arguments to other functions&lt;/li&gt;
&lt;li&gt;Returned from another function&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Functions As Arguments
&lt;/h2&gt;

&lt;p&gt;If you've ever used the Array &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map"&gt;map&lt;/a&gt; or  &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach"&gt;forEach&lt;/a&gt; &lt;br&gt;
 function in Javascript you've already seen functions as arguments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&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="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&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;square&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&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;squaredNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;square&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;squaredNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// [1, 4, 9, 16]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The map function will call our square function on every element in the numbers array, and push the return value of our square function into a new array. Once there are no more elements to invoke our square function on, the new array is returned.&lt;/p&gt;

&lt;p&gt;This is a simplified version of what a map function definition might look like:&lt;br&gt;
&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;ourMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fn&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;newArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;newArray&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&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;newArray&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In ourMap, our passed function argument is invoked on each member of the array.&lt;/p&gt;

&lt;h2&gt;
  
  
  Functions As Return Values
&lt;/h2&gt;

&lt;p&gt;We've seen how we use functions as arguments, but what about returning a function from a function?&lt;/p&gt;

&lt;p&gt;It's possible!&lt;br&gt;
&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;multiplier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;f&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;multiplyByTwo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;multiplier&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="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;multiplyByTwo&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="c1"&gt;// 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here the inner function knows about "x", it's within its scope, so when we call multiplier(2) we return a function that looks like this&lt;br&gt;
&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="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;f&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;2&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;f&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now when we invoke multiplyByTwo, we'll invoke the function we return from our "multiplier" function. That means when we call "multiplyByTwo(10)" we get 20.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;multiplyByTwo&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="c1"&gt;// 20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The returned function still has access to all defined variables in the closure it was created in. This is why our "multiplyByTwo" function has access to the number 2 we passed to "multiplier" when creating our "multiplyByTwo" function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compose Function
&lt;/h2&gt;

&lt;p&gt;In order to create our compose function we're gonna want to take in any number of functions and any number of arguments to pass to each function.&lt;/p&gt;

&lt;p&gt;This sounds a bit daunting, but luckily we can take advantage of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments"&gt;arguments&lt;/a&gt; array-like object and the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce"&gt;Array.prototype.reduce&lt;/a&gt; function. &lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;The arguments array-like object is not an array, and does &lt;br&gt;
not have certain functions or properties associated with it that an array might have. That being said, you can convert  it to an array using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from"&gt;Array.from&lt;/a&gt; &lt;br&gt;
function if you need an array.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;I'm gonna write out the entire function, so we can examine and break it down into pieces. By the end, we'll be able to compose our own understanding of a compose function!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;2&lt;/span&gt;   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fns&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="mi"&gt;3&lt;/span&gt;     &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;reducer&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;4&lt;/span&gt;       &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;returnedFunc&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="mi"&gt;5&lt;/span&gt;         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;accumulator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;current&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="mi"&gt;6&lt;/span&gt;      &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;7&lt;/span&gt;     &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="mi"&gt;8&lt;/span&gt;  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="mi"&gt;9&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's break it down line by line.&lt;/p&gt;

&lt;h3&gt;
  
  
  Line 1
&lt;/h3&gt;

&lt;p&gt;We declare our compose function and use the spread operator to copy all of the functions we're receiving as arguments. This is technically the arguments array-like object for our compose function, but we'll call it "fns" because those arguments will only ever be functions. &lt;/p&gt;

&lt;h3&gt;
  
  
  Line 2
&lt;/h3&gt;

&lt;p&gt;Here we're gonna run reduce on this arguments array.&lt;/p&gt;

&lt;h3&gt;
  
  
  Line 3
&lt;/h3&gt;

&lt;p&gt;The reduce functions takes a reducer function. Here, the "accumulator" will start at the first element in the "fns" arguments array, and the "current" will be the second.&lt;/p&gt;

&lt;h3&gt;
  
  
  Line 4
&lt;/h3&gt;

&lt;p&gt;Here comes our returned function! The function will be returned when we invoke compose. &lt;/p&gt;

&lt;p&gt;At this point, I think it would be helpful to see this in action.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;addAndMultiplyItself&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;compose&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;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;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;addAndMultiplyItself&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// [Function: returnedFunc]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We've now saved our returned function into a variable and it has access to the environment in which it was defined. This means it has access to functions we passed in on line 1. &lt;/p&gt;

&lt;h3&gt;
  
  
  Line 5
&lt;/h3&gt;

&lt;p&gt;When we call addAndMultiplyByItself, and pass in our argument(s), the reduce function will execute from innermost to outermost. &lt;/p&gt;

&lt;p&gt;Here's the function call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;addAndMultiplyItself&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;compose&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;multiply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="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;x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;x&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;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nx"&gt;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;addTogetherAndMultiply&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here's what happens as the reducer executes:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;iteration&lt;/th&gt;
&lt;th&gt;accumulator&lt;/th&gt;
&lt;th&gt;current&lt;/th&gt;
&lt;th&gt;args&lt;/th&gt;
&lt;th&gt;returned value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;multiply&lt;/td&gt;
&lt;td&gt;add&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;400&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;p&gt;When we invoke the function returned from compose with the argument 10, addTogetherAndMultiply(10), we run every single function compose takes as an argument on the number 10, innermost to outermost as we reduce.&lt;/p&gt;

&lt;p&gt;Composing our functions gives us more control over adding and removing functions that may not suit a particular use case.&lt;/p&gt;

&lt;p&gt;We can build many reusable, modular functions by following a functional composition model.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>functional</category>
    </item>
    <item>
      <title>Making My First Open Source Contribution</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Fri, 12 Jun 2020 20:11:58 +0000</pubDate>
      <link>https://dev.to/qmaximillian/my-experience-making-my-first-open-source-contribution-258e</link>
      <guid>https://dev.to/qmaximillian/my-experience-making-my-first-open-source-contribution-258e</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mr_30bB5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/rebuild-black-business/image/upload/c_scale%2Cf_auto%2Ch_0.6%2Cq_auto/v1/assets/home-header-bg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mr_30bB5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://res.cloudinary.com/rebuild-black-business/image/upload/c_scale%2Cf_auto%2Ch_0.6%2Cq_auto/v1/assets/home-header-bg" alt="Protesters gathering and raising their fists in a black power salute"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the past two days I had been checking Trello for a ticket that I thought I could complete. Finally, I found one! ...and I spent 10 minutes reasoning back and forth whether I could do it. I was paralyzed with fear. Could I accomplish this? Did I have the skills? Would my work be worthy enough to put into production?&lt;/p&gt;

&lt;p&gt;I was extremely unsure if I could pull through. I mustered up the courage to ask the Team Lead if I could take the ticket. They gave me the go ahead. &lt;/p&gt;

&lt;p&gt;I took a deep breath and I took the ticket. &lt;/p&gt;

&lt;p&gt;The ticket involved adding clickable arrows to a pagination component. I had to make it so a user could click to go to the next or previous page. I had to take into account mobile and desktop styling, and I had to work with Chakra UI, a library I had no experience using. &lt;/p&gt;

&lt;p&gt;The majority of the component was already built, but looking through the file only made me more confused than I had initially been.&lt;/p&gt;

&lt;p&gt;After taking another deep breath I formulated a plan based on advice I had been given from the coding bootcamp I had attended, from a summer internship I had done, and hours of searching stack overflow. This was the process that helped me from beginning to end.&lt;/p&gt;

&lt;h2&gt;
  
  
  Write Down Ticket Requirements
&lt;/h2&gt;

&lt;p&gt;I wrote down a list of every requirement I had to fulfill to complete the ticket. This let me see the exact amount of work I had to do, making it less cloudy what the end goal would look like.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understand the File(s)
&lt;/h2&gt;

&lt;p&gt;I took my time reading and examining the files. I attempted to get a high level overview of how the logic was affecting the view. Piecing together how each piece functioned to create the whole component gave me a quick look into how the component worked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Change, Reload, Change, Reload
&lt;/h2&gt;

&lt;p&gt;I took my time adjusting variables I thought might be related to fulfilling the requirements of the ticket. This helped me to get a deeper understanding of how the state, functions, and variables in the component work together.&lt;/p&gt;

&lt;p&gt;Some tools I used beside directly changing code were&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;javascript console.log&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 and&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;javascript debugger&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
. These two were able to give me a good idea if I was getting back the values I expected and if my code was executing how I expected.&lt;/p&gt;

&lt;p&gt;Note: If you become confused and want to clean the slate and remove all changes you made on unstaged files (files are not added or committed) run&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git git checkout .&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 in your terminal and all the changes you made will be discarded. You can also discard changes in individual files using&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git git checkout [FILE PATH HERE]&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;i.e.&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;git git checkout src/components/Pagination.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  Write Down Steps to Complete Ticket
&lt;/h2&gt;

&lt;p&gt;After I had decent understanding of what the pagination component was doing, I decided to write out how I could possibly complete each task step by step.&lt;/p&gt;

&lt;p&gt;All in all, it took me about 6 hours. I tried new things, combed at Chakra UI documentation and asked many questions. The thing I most struggled with were conventions used within the project. It's tempting to copy what other people are doing, but I tried to focus on why these decisions were made. I didn't want to emulate code as a default, because it could yield unintended side effects that could confuse me.&lt;/p&gt;

&lt;p&gt;This was where having such a great team of volunteers and Team Leads helped. I started out unsure and wary of whether I could accomplish contributing to open source, but the above steps helped me to focus on the task at hand. I submitted my PR confident that the work I had done was worthy. &lt;/p&gt;

&lt;p&gt;It was merged the next day and I made my first contribution to open source! 🥳&lt;/p&gt;

&lt;p&gt;Some open source projects will list issues with labels denoting them as good first source contributions, and I think if you feel comfortable enough with the technologies used or even a bit challenged, it may be worth it to take on the issue. I'm definitely still hesitant, but I know I'm gonna fight this feeling and make more contributions. Worst that happens is it doesn't get merged. But there will always be more open source to contribute to!&lt;/p&gt;

&lt;p&gt;The project I was able to contribute to is &lt;a href="https://www.rebuildblackbusiness.com/"&gt;Rebuild Black Business&lt;/a&gt; which just launched today!&lt;/p&gt;

&lt;p&gt;It is an open source project to help black businesses survive as they navigate the age of COVID-19 and dealing with the aftermath of looting. Ultimately, it's an awesome site that provides resources to black business owners who need aid. You can even list yourself as an ally-- someone who is willing to be contacted and help black business owners!&lt;/p&gt;

&lt;p&gt;I encourage those reading this post to take a look and contribute if they can. The entire team is volunteers and they put this together in about 2 weeks. &lt;/p&gt;

</description>
      <category>opensource</category>
      <category>listings</category>
      <category>showdev</category>
    </item>
    <item>
      <title>Using Template Repositories w/ GitHub</title>
      <dc:creator>Quinn Lashinsky</dc:creator>
      <pubDate>Wed, 06 May 2020 20:17:03 +0000</pubDate>
      <link>https://dev.to/qmaximillian/using-template-repositories-w-github-141i</link>
      <guid>https://dev.to/qmaximillian/using-template-repositories-w-github-141i</guid>
      <description>&lt;p&gt;Abstractions are a key part of writing concise, useful code. When we create abstractions we write less code, reuse more code, and make our programs more clear and understandable. While working on building yet  another server for a project idea I had, I realized I was coding the same boilerplate I had coded multiple times before. I was not abstracting this process effectively. How could I create a template repository (repo) with an already made repo?&lt;/p&gt;

&lt;h2&gt;
  
  
  The Do-It-Yourself Option
&lt;/h2&gt;

&lt;p&gt;We can choose a repo to treat as a template. From there we have to decouple the template from its remote origin, and add a new remote origin repo to push to, creating our new project.&lt;/p&gt;

&lt;p&gt;Let's walk through the steps:&lt;/p&gt;

&lt;p&gt;First we'll go to the repo on GitHub you're going to treat as a template and copy the repo's git clone command.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--i7d-G5sr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/be03a6r90g2nrzcujw6r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--i7d-G5sr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/be03a6r90g2nrzcujw6r.png" alt='The window that appears when you click the "Clone or download" button in the "Code" tab in your repository'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In your command line, paste the command you copied. It should look something like this without the angle brackets ("&amp;lt;" and "&amp;gt;") :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git clone git@github.com:&amp;lt;your-username&amp;gt;/&amp;lt;your-template-repo-name&amp;gt;.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;From here, we can add the name of the folder we want this repo to clone into as the last argument of this command.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone git@github.com:&amp;lt;your-username&amp;gt;/&amp;lt;your-template-repo-name&amp;gt;.git &amp;lt;file-name&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If the file already exists, the repo will clone into that file. If it does not exist, the file will be created and then the repo will be cloned into it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;*Helpful Tip:&lt;/em&gt; &lt;/p&gt;

&lt;p&gt;&lt;em&gt;git remote -v&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This command will tell you where your current repo points to as its origin&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then, we can change the origin url by running the below command. The origin url is the alias for a remote repo, which for our purposes, is the version of this repo stored on GitHub's servers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git remote set-url origin git@github.com:&amp;lt;your-username&amp;gt;/&amp;lt;target-repo-for-new-project&amp;gt;.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we're set! We've used a de facto template repo to create a new project repo structured just how we like.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Automatic Way
&lt;/h2&gt;

&lt;p&gt;We've just manually created a template repo. But is there a reason to manually handle this process each time you create a new template/project?&lt;/p&gt;

&lt;p&gt;Luckily websites like GitLab and GitHub have added the ability to let you use any of your repo's as a template repo. Let's see how we can create a template repo on GitHub.&lt;/p&gt;

&lt;p&gt;Go to Settings tab on the repo page you want to make a template repo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--wr2khq5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7vs2kbkkt8q8ecwifgg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--wr2khq5c--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/v7vs2kbkkt8q8ecwifgg.png" alt="GitHub repo on the Settings tab, indicating the ability to click a checkbox to enable a repository to become a template repository"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Right below the name of the repo, we can click a checkbox to turn the current repo into a template repo.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--UPHSEYwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/me1fucusflxw9aw54gjf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--UPHSEYwA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/me1fucusflxw9aw54gjf.png" alt="GitHub repo on the Settings tab, with checkbox to enable a repository to become a template repository checked"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's all it takes!&lt;/p&gt;

&lt;p&gt;Now whenever we create a new repo, we'll be given the option to use a template repo at the top of the page. This will create our repo with the base file structure defined in our template. From here we can clone the repo, and get to work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--huxpfEDD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/efsvxerd10zhltwnak02.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--huxpfEDD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/efsvxerd10zhltwnak02.png" alt="Creating a new repo on GitHub and selecting the instant dark mode template for my new Vantablack website project"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We'll even see a reference to the template repo below the heading that includes our GitHub username and repo name.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cIK8shB8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/57hgx94n3fhu5i88ijtz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cIK8shB8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/57hgx94n3fhu5i88ijtz.png" alt='GitHub username, repo name and just below the text, "generated from QMaximillian/instant-dark-mode"'&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Creating a template repo can help you think about just how to to design the base architecture of any project you create. Reasoning about design decisions like using an ORM, SQL or NoSQL, or including authentication are all up to you. Once made, you can continually update the code to suit your needs. By creating a template repo we can abstract the work that is common across projects, and confidently create projects with shorter development time.&lt;/p&gt;

</description>
      <category>github</category>
      <category>template</category>
      <category>architecture</category>
      <category>git</category>
    </item>
  </channel>
</rss>
