<?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: A.M. Hernandez</title>
    <description>The latest articles on DEV Community by A.M. Hernandez (@amhernandez).</description>
    <link>https://dev.to/amhernandez</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%2F813685%2Fed365853-27fe-45b9-a756-ea60a0b02f7a.png</url>
      <title>DEV Community: A.M. Hernandez</title>
      <link>https://dev.to/amhernandez</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amhernandez"/>
    <language>en</language>
    <item>
      <title>How I REACTO to Algos: Sum Intervals</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Sun, 17 Apr 2022 03:52:48 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-sum-intervals-303i</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-sum-intervals-303i</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;More time planning could mean less time correcting. That's why REACTO is so useful. It helps us focus on the plan before we act. Sort of like measuring twice and cutting once. Today, we will work with nested arrays and plan our approach to the solution using REACTO.&lt;/p&gt;

&lt;p&gt;Before we move any further, I'd like to poit out the previous 4 posts in the series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-decipher-33a8"&gt;Caesar Decipher&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;Caesar Cipher&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-tournament-winner-20hk"&gt;Tournament Winner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-17l"&gt;Pig Latin&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  This is REACTO
&lt;/h2&gt;

&lt;p&gt;REACTO is an acronym that represents the method we will use in solving this problem. As a reminder, these are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's stick to this order and get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Write a function called &lt;code&gt;sum_intervals()&lt;/code&gt; that accepts an array of intervals, and returns the sum of all the interval lengths. Overlapping intervals should only be counted once.&lt;/p&gt;

&lt;p&gt;Intervals are represented by a pair of integers in the form of an array. The first value of the interval will always be less than the second value. Interval example: [1, 5] is an interval from 1 to 5. The length of this interval is 4.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overlapping Intervals&lt;/strong&gt;&lt;br&gt;
List containing overlapping intervals:&lt;br&gt;
[&lt;br&gt;
[1,4],&lt;br&gt;
[7, 10],&lt;br&gt;
[3, 5]&lt;br&gt;
]&lt;/p&gt;

&lt;p&gt;The sum of the lengths of these intervals is 7. Since [1, 4] and [3, 5] overlap, we can treat the interval as [1, 5], which has a length of 4.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;

&lt;p&gt;We need to restate the prompt so it could help us plan our approach later. When we restate the prompt we should try to keep our description brief. Brief are easier to scan read while working out the solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate
*/&lt;/span&gt;

&lt;span class="c1"&gt;// create function sumIntervals&lt;/span&gt;
&lt;span class="c1"&gt;// args: array of arrays containing two integers =&amp;gt; [[int, int], [int, int],  [int, int], ...]&lt;/span&gt;
&lt;span class="c1"&gt;// overlapping intervals are counted once&lt;/span&gt;
&lt;span class="c1"&gt;// return value is integer, sum of all intervals of input array&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clarifying questions:&lt;/p&gt;

&lt;p&gt;If there are any lingering questions we should ask them here but we can ask about them later as well. I have none so let's move on.&lt;/p&gt;

&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;Let's get some examples listed so we can use it to guide our approach.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sample input&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sumIntervals&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="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="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;10&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&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="mi"&gt;9&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sumIntervals&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="mi"&gt;1&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="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;10&lt;/span&gt;&lt;span class="p"&gt;],&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;5&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="mi"&gt;7&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;sumIntervals&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="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="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;20&lt;/span&gt;&lt;span class="p"&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;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
   &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&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="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&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="mi"&gt;19&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;This is the heartiest part of our journey to the solution. We need a plan. I like to build the approach as a comment so I could easily reorder them when necessary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function function()
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First step is to create the function. Now we need to handle the input. Let's think about why our example says that an interval &lt;code&gt;[1, 5]&lt;/code&gt; is &lt;code&gt;4&lt;/code&gt;. When I think about it, &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;5&lt;/code&gt; contain &lt;code&gt;5&lt;/code&gt; numbers. So why is the length &lt;code&gt;4&lt;/code&gt;? Well, if we are dealing with time we don't count every integer but the space between them. So to get from &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt; we will need &lt;code&gt;1&lt;/code&gt; unit. Same for &lt;code&gt;1&lt;/code&gt; to &lt;code&gt;5&lt;/code&gt;. Look at it like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; to &lt;code&gt;2&lt;/code&gt; = &lt;code&gt;1&lt;/code&gt; unit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;2&lt;/code&gt; to &lt;code&gt;3&lt;/code&gt; = &lt;code&gt;1&lt;/code&gt; unit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3&lt;/code&gt; to &lt;code&gt;4&lt;/code&gt; = &lt;code&gt;1&lt;/code&gt; unit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;4&lt;/code&gt; to &lt;code&gt;5&lt;/code&gt; = &lt;code&gt;1&lt;/code&gt; unit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's &lt;code&gt;4&lt;/code&gt; units total.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function function()
- init accumulator for sum of intervals, type int
- init accumulator for visited indices, arr type int
- iter over input intervals array, index i
-- declare const for current interval
-- iter over current interval, index j
--- if the arr accumulator for visited indices does not include this index j
---- increment accumulator for sum intervals by 1
---- push current index j to visited indices array accumulator
- return the sum of intervals accumulator
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comments above lay out the approach. We know that we need to visit every element of the input array. Then we want to take account of the interval numbers we have visited in case we come across them again in another element. Another initial step is to create a variable to hold onto the return value that we can increment as we loop over the input array. We set up an empty array to keep tabs on the visited indices.&lt;/p&gt;

&lt;p&gt;Next, we need to iterate over the input array. At each iteration we need to set a constant to hold onto the current interval of the input array. Then we loop over the interval, setting the first value of the interval as the starting index for this loop, and the second value of the interval as the end index. This way we are counting from starting integer to end integer of the interval pair (see below).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interval --&amp;gt; [1, 5]
for(let j=interval[0]; j&amp;lt;interval[1]; j++){ ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While in the inner loop, we check if the visited indices array does not include the current index. If the array contains the index we don't want to count it. If the array does not contain the index we need to count it. We do that by incrementing the accumulator for interval sum by &lt;code&gt;1&lt;/code&gt;. Then, while still in the conditional block, we will push the index number of this inner loop to the accumulator of visited indices.&lt;/p&gt;

&lt;p&gt;When the outer loop is done we can then return the interval sum accumulator value.&lt;/p&gt;

&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;Time to code! 🧑‍💻&lt;br&gt;
Let's set up the function and then copy the approach as comments into the function to use as a guide.&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;// create function function()&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;intervalSum&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// init accumulator for sum of intervals, type int&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;intervalSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// init accumulator for visited indices, arr type int&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;intArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="c1"&gt;// iter over input intervals array, index i&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// declare const for current interval&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// iter over current interval, index j&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;interval&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;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// if the arr accumulator for visited indices does not include this index j&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;intArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// increment accumulator for sum intervals by 1&lt;/span&gt;
        &lt;span class="nx"&gt;intervalSum&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// push current index j to visited indices array accumulator&lt;/span&gt;
        &lt;span class="nx"&gt;intArr&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;j&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="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;// return the sum of intervals accumulator&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;intervalSum&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;That should be everything! Now I am going to remove the comments for readability, but I would usually keep comments in if I am saving this to my local machine so that I may review the thought process in the future. Here's the code without the comments:&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;sumIntervals&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intervals&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;intervalSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;intArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;intervals&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;interval&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;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;intArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;intervalSum&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;intArr&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;j&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="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;intervalSum&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;Doesn't that look great? We could have actually tested the code before removing the comments. Let's continue.&lt;/p&gt;

&lt;h3&gt;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;Let's test! Here is a Codepen with the function in the JS tab on the left and the results on the right. Feel free to play around with the code and explore.&lt;/p&gt;

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

&lt;p&gt;All of our tests passed! Feel free to play around with the function and the tests to experiment.&lt;/p&gt;

&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;Can you find a more optimal solution than using two &lt;code&gt;for&lt;/code&gt; loops? I bet you can find a more efficient solution. That is the task of our next step. At this point we have found the solution so it's worth celebrating. It is good practice to then look for a more optimal solution if one exists.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You
&lt;/h2&gt;

&lt;p&gt;Thank you for taking time out of your day to read this post. Follow me here on &lt;a href="https://dev.to/amhernandez"&gt;DEV&lt;/a&gt; if you'd like to see more content like this and follow my explorations into the world of web development. I'll see you around!&lt;/p&gt;

</description>
      <category>java</category>
      <category>algorithms</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How I REACTO to Algos: Caesar Decipher</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Sun, 27 Mar 2022 05:06:44 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-decipher-33a8</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-decipher-33a8</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;It is finally time to decipher the Caesar Cipher.&lt;br&gt;
First, here's a quick reminder of what the Caesar Cipher is, from &lt;a href="https://en.wikipedia.org/wiki/Caesar_cipher"&gt;Wikipedia&lt;/a&gt; do some explaining on that matter:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In cryptography, a Caesar cipher ... is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;last article&lt;/a&gt; we explored the Caesar Cipher and created a function to encrypt messages suing a secret key. In this article we will decipher an encoded message. After this we will be able to encrypt and decrypt using the Caesar Cipher, opening up some more avenues of exploration. Before we go any further, I'd like to point out my previous articles in this REACTO series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;Caesar Cipher&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-tournament-winner-20hk"&gt;Tournament Winner&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-17l"&gt;Pig Latin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-33ag"&gt;First Double&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;Now, a quick recap on how we will get to our solution using REACTO.&lt;/p&gt;
&lt;h2&gt;
  
  
  This is REACTO
&lt;/h2&gt;

&lt;p&gt;REACTO is an acronym that represents the method we will use in solving this problem. As a reminder, these are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay, you know what to do now.&lt;/p&gt;
&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a function that will receive a string secret and a key number as input and return a decoded version of the secret. A secret is decoded by replacing each letter in the secret with the letter that is a specified number (the key argument) of letters ahead of it in the alphabet. When shifting, if the selection goes beyond the last letter of the alphabet you should continue counting from the first letter of the alphabet. Decoded messages should be returned in all upper case.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;

&lt;p&gt;I worded the prompt similarly to the previous prompt for Caesar Cipher because the goal is nearly identical. The solution should be very similar so let's just jump right into it. First, I am restating this prompt, again reflecting the restatement of the last prompt. Except this time we know that the function may receive a string containing non-letter characters which must remain unaltered, and the input string may be in either lower or upper case.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

Create a function that takes two args: a string and a number.
Return an decoded version of the string in all upper case.
In order to decode the string, each letter needs to be unshifted by the number argument.
While unshifting, if we need to go right of the last letter in the alphabet we should wrap to the first letter of the alphabet.
Non-letter characters should not be altered.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;To test this function we are going to use the secrets generated in the previous article, &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;Caesar Cipher&lt;/a&gt;. Those secrets will now be our test inputs and the decoded messages will be the expected outputs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sample input&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// example 1&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;QEB NRFZH YOLTK CLU GRJMBA LSBO QEB IXWV ALD&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key&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="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;THE&lt;/span&gt; &lt;span class="nx"&gt;QUICK&lt;/span&gt; &lt;span class="nx"&gt;BROWN&lt;/span&gt; &lt;span class="nx"&gt;FOX&lt;/span&gt; &lt;span class="nx"&gt;JUMPED&lt;/span&gt; &lt;span class="nx"&gt;OVER&lt;/span&gt; &lt;span class="nx"&gt;THE&lt;/span&gt; &lt;span class="nx"&gt;LAZY&lt;/span&gt; &lt;span class="nx"&gt;DOG&lt;/span&gt;


&lt;span class="c1"&gt;// example 2&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;OHCL FVB LCLY OLHYK VM AOL IFGHUAPUL NLULYHSZ WYVISLT?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&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;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;Have&lt;/span&gt; &lt;span class="nx"&gt;you&lt;/span&gt; &lt;span class="nx"&gt;ever&lt;/span&gt; &lt;span class="nx"&gt;heard&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="nx"&gt;The&lt;/span&gt; &lt;span class="nx"&gt;Byzantine&lt;/span&gt; &lt;span class="nx"&gt;Generals&lt;/span&gt; &lt;span class="nx"&gt;problem&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;


&lt;span class="c1"&gt;// example 3&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;YMJD XFB FGTZY 5 BTQAJX HNWHQNSL YMJ KNJQI!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&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;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;They&lt;/span&gt; &lt;span class="nx"&gt;saw&lt;/span&gt; &lt;span class="nx"&gt;about&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="nx"&gt;wolves&lt;/span&gt; &lt;span class="nx"&gt;circling&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;field&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;Time to plan an approach to solving the prompt. This is going to very similar to our approach to the &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;caesarCipher&lt;/a&gt; problem, except we traverse the alphabet in the opposite direction using the key.&lt;/p&gt;

&lt;p&gt;When we previously encoded the message &lt;code&gt;THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG&lt;/code&gt; with a key &lt;code&gt;3&lt;/code&gt; we got the secret string &lt;code&gt;QEB NRFZH YOLTK CLU GRJMBA LSBO QEB IXWV ALD&lt;/code&gt;. Thus, we should now be able to decode the secret &lt;code&gt;QEB NRFZH YOLTK CLU GRJMBA LSBO QEB IXWV ALD&lt;/code&gt; with the same key of &lt;code&gt;9&lt;/code&gt; and get back the string message &lt;code&gt;THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG&lt;/code&gt;. This is our goal.&lt;/p&gt;

&lt;p&gt;First, we can lay out the first steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarDecipher(secret, key)
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know that we should return a string in all upper case, and that the input string may be in any case. I would want to convert the input string to upper case before iterating over it. We are also going to use a string constant to hold all of the letter of the alphabet in order. This string should already be in all upper case to make comparisons simpler. Before iterating over the secret string we should set up an accumulator to build up the decoded message.&lt;/p&gt;

&lt;p&gt;When iterating over the string the algorithm needs to visit each character of the secret in order, starting from the &lt;code&gt;0&lt;/code&gt; index. We can use a for loop limited to the length of the string, just as we did for the cipher function. In each loop, we should declare a constant for that current character of the string secret and check if it is a letter. If it is not a letter it will be added to the decoded message string, and if it is a letter it will need to be decoded first and then added to the message string.&lt;/p&gt;

&lt;p&gt;If the current character is a letter get the index of it in the constant string alphabet. This letter will then be replaced with another letter of the alphabet that is &lt;code&gt;key&lt;/code&gt; letters ahead of it. We will also wrap from letter &lt;code&gt;Z&lt;/code&gt; to letter &lt;code&gt;A&lt;/code&gt; when the key requires we go beyond the index of &lt;code&gt;25&lt;/code&gt;, the last index of the alphabet. Wrapping can be acheived by getting the remainder of the sum of the current index divided by 26. 26 because that is the number of letters in the alphabet, and will eb the length of the alphabet string.&lt;/p&gt;

&lt;p&gt;To clarify the point on wrapping; the position of the letter &lt;code&gt;X&lt;/code&gt; is &lt;code&gt;23&lt;/code&gt; and, for example, the key is &lt;code&gt;5&lt;/code&gt; then the position of the decoded would be 28. There is no index past &lt;code&gt;25&lt;/code&gt;, though. That's why we need to wrap, and we can do that by adding the index of the current character to the key number. That sum may be divided by 26, the total length of the alphabet, to give us a remainder. Whatever that remainder number is will be the location of the decoded letter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;letter &lt;code&gt;X&lt;/code&gt; is at index &lt;code&gt;23&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;index &lt;code&gt;23&lt;/code&gt; + key &lt;code&gt;5&lt;/code&gt; = &lt;code&gt;28&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;and the remainder of &lt;code&gt;28 / 26&lt;/code&gt; = &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;the letter at index &lt;code&gt;2&lt;/code&gt; is &lt;code&gt;C&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once the replacement letter, or the decoded letter, is found it will be added to the decoded message string. After every character of the input string has been visited the decoded message string can be returned. The end!&lt;/p&gt;

&lt;p&gt;Now it can be added to a comment and then we can add that comment into our fucntion!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarDecipher(message, key)

- create constant for alphabet characters, all caps
- create variable for the return string value (encoded message)
- convert input string to upper case
- iterate over input string
-- create constant for the current character
-- check if current character is a letter and get the index of that letter in the alphabet
-- IF character is a letter:
--- add the key value to the current character's index to get the index of the substitute character (decoded character)
--- IF the index for the substitute character is greater than 26:
---- the value for the substitute's index should be updated to be the remainder of this index and 26
--- get the substitute character at this new index from the alphabet constant and add it to the decoded message
-- ELSE if character is not a letter, add it to the decoded message without change
- return the decoded message
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;I'm going to drop the approach comments into the function and use that as my guide.&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;// - create function caesarDecipher(message, key)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;caesarDecipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// - create constant for alphabet characters, all caps&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// - create variable for the return string value (encoded message)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// - convert input string to upper case&lt;/span&gt;
  &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// - iterate over input string&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// -- create constant for the current character&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// -- check if current character is a letter and get the index of that letter in the alphabet&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// -- IF character is a letter:&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;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// --- add the key value to the current character's index to get the index of the substitute character (decoded character)&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// --- IF the index for the substitute character is greater than 26:&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;newPos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// ---- the value for the substitute's index should be updated to be the remainder of this index and 26&lt;/span&gt;
        &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// --- get the substitute character at this new index from the alphabet constant and add it to the decoded message&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// -- ELSE if character is not a letter, add it to the decoded message without change&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;char&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;// - return the decoded message&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&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;Let's note the use of &lt;code&gt;indexOf()&lt;/code&gt; which returns &lt;code&gt;-1&lt;/code&gt; if the character is not found in the target string. Also, we are checking if the index is over 26, but we don't need to do that. Even if the number is below 25 and we use the modulo operator to get the remainder with 26 it will simply return the same index. For example, if the new index position is &lt;code&gt;5&lt;/code&gt;, the result fo &lt;code&gt;5 % 26&lt;/code&gt; will be &lt;code&gt;5&lt;/code&gt;. Therefore, the conditional checking if the new index is over &lt;code&gt;26&lt;/code&gt; is unnecessary. I will include that change below. Here's the code without the comments:&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;caesarDecipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&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;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;shift&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;char&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;message&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;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;Now for the tests!&lt;/p&gt;

&lt;p&gt;Here is a Codepen with the function in the JS tab on the left and the results on the right. Feel free to play around with the code and explore.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;Nothing more to add here that wasn't covered in &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i"&gt;caesarCipher&lt;/a&gt; algo. Since we need to visit each character in the input string the time complexity will remain O(n) and so will the space.&lt;/p&gt;

&lt;p&gt;And that is the last step! If you have any questions or suggestions, please leave a comment below!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;Well, now that we can encode and a decode a message we should build an app that can do this for others!! Well, luckily I have already put together this app last summer when I first came across the Caesar Cipher. Back then I went about it the opposite of how we did in this series, so I will be updating the functions there to reflect these updates. By the time you visit this link, it should already be updated. And yes, I did spell cipher with a &lt;code&gt;y&lt;/code&gt; a bunch of times. I think I'll stick with &lt;code&gt;cipher&lt;/code&gt; though!&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--O2mproaR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9jnt3fjqq51fhh9aujfx.png" alt="screenshot of CaesarCipher app, _am-hernandez.github.io/caesarCipher_" width="880" height="654"&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;em&gt;am-hernandez.github.io/caesarCipher&lt;/em&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;a href="https://am-hernandez.github.io/caesarCipher/"&gt;Visit the CaesarCipher app here&lt;/a&gt; to share secret messages with friends!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Look out for a follow-up where I will walk you through creating this app from these last two algos&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You
&lt;/h2&gt;

&lt;p&gt;Once again. I would like to thank you for taking time out of your day to read this post. Follow me here on &lt;strong&gt;DEV&lt;/strong&gt; if you'd like to see more content like this as I post about my explorations into the world of web development. I'll see you around!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>cryptography</category>
    </item>
    <item>
      <title>How I REACTO to Algos: Caesar Cipher</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Sun, 20 Mar 2022 03:45:12 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-cipher-3j0i</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;Today we are going to go over the Caesar Cipher. Or cypher... Or cipher? 🤔&lt;br&gt;
What is the Caesar Cipher anyway? Well, I'll let &lt;a href="https://en.wikipedia.org/wiki/Caesar_cipher"&gt;Wikipedia&lt;/a&gt; do some explaining on that matter:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In cryptography, a Caesar cipher ... is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Our goal will be to encode a message using this cipher! Before we go any further on this algo, I'd like to point out my previous articles in this series:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-33ag"&gt;First Double&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-17l"&gt;Pig Latin&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-tournament-winner-20hk"&gt;Tournament Winner&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;



&lt;p&gt;Now, a quick recap on how we will get to our solution using REACTO.&lt;/p&gt;
&lt;h2&gt;
  
  
  This is REACTO
&lt;/h2&gt;

&lt;p&gt;REACTO is an acronym that represents the method we will use in solving this problem. As a reminder, these are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;🏁 Let's get started!&lt;/p&gt;
&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a function that will receive a string message and a key number as input and return an encoded version of the message. A message is encoded by replacing each letter in the message with the letter that is a specified number (the key argument) of letters before it in the alphabet. When shifting, if the selection goes beyond the first letter of the alphabet you should continue counting from the last letter of the alphabet. Encoded messages should be returned in all upper case.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;

&lt;p&gt;Here's where we can make note of the prompt and restate it in our own own words. I actually worded the prompt above so I will just word it differently below in the way I normally would.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

Create a function that takes two args: a string and a number.
Return an encoded version of the string in all upper case.
In order to encode the string, each letter needs to be shifted by the number argument.
While shifting, if we need to go left of the first letter in the alphabet we should wrap to the last letter of the alphabet.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clarifying questions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: Might the function receive non-letter characters?&lt;br&gt;
A: Yes, the function may receive non-letter characters. Only letters will be shifted, keep other character in places.&lt;/p&gt;

&lt;p&gt;Q: Will the input string always be all caps?&lt;br&gt;
A: The input string may be in lower or upper case or a mix of both.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Okay, so that's cleared up and will be added to the restatement of the prompt in our notes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

Create a function that takes two args: a string and a number.
Return an encoded version of the string in all upper case.
In order to encode the string, each letter needs to be shifted by the number argument.
While shifting, if we need to go left of the first letter in the alphabet we should wrap to the last letter of the alphabet.
Non-letter characters should not be altered.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;In this section we will need to create some examples of the expected return values. Examples should be provided and we can always add more for clarification. We'll pull the first example from that same &lt;a href="https://en.wikipedia.org/wiki/Caesar_cipher"&gt;Wikipedia article on Caesar Cipher&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// example 1&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key&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="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;QEB&lt;/span&gt; &lt;span class="nx"&gt;NRFZH&lt;/span&gt; &lt;span class="nx"&gt;YOLTK&lt;/span&gt; &lt;span class="nx"&gt;CLU&lt;/span&gt; &lt;span class="nx"&gt;GRJMBA&lt;/span&gt; &lt;span class="nx"&gt;LSBO&lt;/span&gt; &lt;span class="nx"&gt;QEB&lt;/span&gt; &lt;span class="nx"&gt;IXWV&lt;/span&gt; &lt;span class="nx"&gt;ALD&lt;/span&gt;

&lt;span class="c1"&gt;// example 2&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Have you ever heard of The Byzantine Generals problem?&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;19&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;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;OHCL&lt;/span&gt; &lt;span class="nx"&gt;FVB&lt;/span&gt; &lt;span class="nx"&gt;LCLY&lt;/span&gt; &lt;span class="nx"&gt;OLHYK&lt;/span&gt; &lt;span class="nx"&gt;VM&lt;/span&gt; &lt;span class="nx"&gt;AOL&lt;/span&gt; &lt;span class="nx"&gt;IFGHUAPUL&lt;/span&gt; &lt;span class="nx"&gt;NLULYHSZ&lt;/span&gt; &lt;span class="nx"&gt;WYVISLT&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt;

&lt;span class="c1"&gt;// example 3&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;message3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;They saw about 5 wolves circling the field!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;key3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;99&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;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;YMJD&lt;/span&gt; &lt;span class="nx"&gt;XFB&lt;/span&gt; &lt;span class="nx"&gt;FGTZY&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="nx"&gt;BTQAJX&lt;/span&gt; &lt;span class="nx"&gt;HNWHQNSL&lt;/span&gt; &lt;span class="nx"&gt;YMJ&lt;/span&gt; &lt;span class="nx"&gt;KNJQI&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;We could come up with many examples, but this should be good for now. We can see that the spacing and punctuation are preserved.&lt;/p&gt;

&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;Before coding it will be best to think through an approach to finding a resolution. First step is definitely going to be creating the function that takes two arguments. What else?&lt;br&gt;
I will write out the approach in a comment below the restatement of the prompt. You might find that you write your approach and then edit it a few times before moving on to the next step of coding your solution.&lt;/p&gt;

&lt;p&gt;In the function we will want to create a string that holds every letter of the alphabet in order and in all caps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarCipher(message, key)

- create constant for alphabet characters, all caps
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Making the alphabet upper case will make it easier to match the letters in the message. Even if the letters in the message were lower case, we will end up converting these letters to upper case before iterating over the message string. We should also set up an accumulator to form the encoded message as we iterate over the input string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarCipher(message, key)

- create constant for alphabet characters, all caps
- create variable for the return string value (encoded message)
- convert input string to upper case
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This brings us the what I was alluding to earlier, we need to iterate over the input string. With each iteration we should get the current character of the input string and check if it is a letter by comparing it to the alphabet constant. If the character is in the character constant then it is a letter. If the character is not a letter we should just add it to the encoded message and move on to the next character in the input string. If the character is a letter we will need to do more work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarCipher(message, key)

- create constant for alphabet characters, all caps
- create variable for the return string value (encoded message)
- convert input string to upper case
- iterate over input string
-- create constant for the current character
-- check if current character is a letter
--- if character is not a letter, add it to the encoded message without change
--- else if char is a letter ....?
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What should you do if a character is a letter? You should get the index of that letter in the alphabet and then combine it with the input key number. Ok, so we get the index of the current letter, but how do we use the key value that was the second argument to the function?&lt;/p&gt;

&lt;p&gt;The key is the shift number, and the prompt states we move down the alphabet &lt;code&gt;key&lt;/code&gt; number of times. If we have the key &lt;code&gt;3&lt;/code&gt; and the current character is &lt;code&gt;D&lt;/code&gt;, then the encoded letter should be &lt;code&gt;A&lt;/code&gt;. The character &lt;code&gt;D&lt;/code&gt; is the fourth letter of the alphabet and so is at its index 3. With a key of &lt;code&gt;3&lt;/code&gt;, we can see that &lt;code&gt;3 - 3 = 0&lt;/code&gt;, and that the letter at index 0 is &lt;code&gt;A&lt;/code&gt;. So &lt;code&gt;D&lt;/code&gt; would be &lt;code&gt;A&lt;/code&gt; if the key is 3.&lt;/p&gt;

&lt;p&gt;Below, you can see that if you rotate the cipher string left by 3 you will end up with the plain alphabet. That's like calling &lt;code&gt;.shift()&lt;/code&gt; three times on cipher if it was an array, and adding the shifted letters to the end of the same array as they come off the front.&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="err"&gt;┌────────┬─────────────────────────────────────────────────────┐&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt; &lt;span class="nx"&gt;plain&lt;/span&gt;  &lt;span class="err"&gt;│&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="nx"&gt;D&lt;/span&gt; &lt;span class="nx"&gt;E&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt; &lt;span class="nx"&gt;G&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;J&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="nx"&gt;M&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt; &lt;span class="nx"&gt;O&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="nx"&gt;Q&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nx"&gt;U&lt;/span&gt; &lt;span class="nx"&gt;V&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt; &lt;span class="nx"&gt;Z&lt;/span&gt; &lt;span class="err"&gt;│&lt;/span&gt;
&lt;span class="err"&gt;├────────┼─────────────────────────────────────────────────────┤&lt;/span&gt;
&lt;span class="err"&gt;│&lt;/span&gt; &lt;span class="nx"&gt;cipher&lt;/span&gt; &lt;span class="err"&gt;│&lt;/span&gt; &lt;span class="nx"&gt;X&lt;/span&gt; &lt;span class="nx"&gt;Y&lt;/span&gt; &lt;span class="nx"&gt;Z&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="nx"&gt;D&lt;/span&gt; &lt;span class="nx"&gt;E&lt;/span&gt; &lt;span class="nx"&gt;F&lt;/span&gt; &lt;span class="nx"&gt;G&lt;/span&gt; &lt;span class="nx"&gt;H&lt;/span&gt; &lt;span class="nx"&gt;I&lt;/span&gt; &lt;span class="nx"&gt;J&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="nx"&gt;M&lt;/span&gt; &lt;span class="nx"&gt;N&lt;/span&gt; &lt;span class="nx"&gt;O&lt;/span&gt; &lt;span class="nx"&gt;P&lt;/span&gt; &lt;span class="nx"&gt;Q&lt;/span&gt; &lt;span class="nx"&gt;R&lt;/span&gt; &lt;span class="nx"&gt;S&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nx"&gt;U&lt;/span&gt; &lt;span class="nx"&gt;V&lt;/span&gt; &lt;span class="nx"&gt;W&lt;/span&gt; &lt;span class="err"&gt;│&lt;/span&gt;
&lt;span class="err"&gt;└────────┴─────────────────────────────────────────────────────┘&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The cipher is created by unshifting the plain alphabet and the alphabet is recreated by shifting the cipher text.&lt;/p&gt;

&lt;p&gt;Let's get back to the code! We know we need to subtract the key from the current index of the current character but what if we generate a negative number for the new index? In order to handle these cases we need to consider the amount of letters in the alphabet. There are 26 letters in the alphabet, so the indices range from 0-25. If we go below 0 we will need to make sure we can wrap back around the other end of the alphabet. If our starting position is the &lt;code&gt;0&lt;/code&gt; index and the key is &lt;code&gt;3&lt;/code&gt;, our new position will be at &lt;code&gt;-3&lt;/code&gt;. Since the new position is below &lt;code&gt;0&lt;/code&gt; we know that we must count back from the last index, &lt;code&gt;25&lt;/code&gt;, three times. If we do that it will make the new position &lt;code&gt;22&lt;/code&gt;, or letter &lt;code&gt;W&lt;/code&gt;, which is one less index than we intend. That's because there are 26 letters but 25 indices, since we start counting indices at 0. So we should add a &lt;code&gt;1&lt;/code&gt; to the new position if it is below zero, then get the remainder from dividing this new position by 26. The remainder number will be negative so we can add that to the number of the last index, &lt;code&gt;25&lt;/code&gt;, to get to the updated new position of &lt;code&gt;23&lt;/code&gt;, or the letter &lt;code&gt;X&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarCipher(message, key)

- create constant for alphabet characters, all caps
- create variable for the return string value (encoded message)
- convert input string to upper case
- iterate over input string
-- create constant for the current character
-- check if current character is a letter and get the index of that letter in the alphabet
--- if character is not a letter, add it to the encoded message without change
--- else if character is a letter, subtract the key value from its alphabet index to get the index of the substitute character (encoded character)
---- if the new index is less than 0, the value should instead be the value of the remainder from new index +1 divided by 26 plus 25
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last step in our approach would leave us with a negative number if the new index was below 0 and performed a modulo operation for the remainder. So if we add that negative remainder to 25 (number of indices in alphabet), we will get the appropriate letter by counting backward from the last index. This way, no matter how large the key is we will still get to our letter. In programming we won't actually have a letter wheel to rotate so we need to consider the 0th index and wrapping!&lt;/p&gt;

&lt;p&gt;Once we have this new index position we can grab the corresponding letter from the alphabet and add it to the encoded message we will be returning at the end of the function. Then we may return the encoded message and be done!&lt;/p&gt;

&lt;p&gt;This is the updated approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function caesarCipher(message, key)

- create constant for alphabet characters, all caps
- create variable for the return string value (encoded message)
- convert input string to upper case
- iterate over input string
-- create constant for the current character
-- check if current character is a letter and get the index of that letter in the alphabet
-- IF character is a letter:
--- subtract the key value from current character's index to get the index of the substitute character (encoded character)
--- IF the index for the substitute character is less than 0:
---- the value for the substitute's index should instead be 25 plus the remainder of this index+1 and 26
--- get the substitute character at this new index from the alphabet constant and add it to the encoded message
-- ELSE if character is not a letter, add it to the encoded message without change
- return the encoded message
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;Time to code! 🧑‍💻&lt;br&gt;
This has been a very long article yet our approach seems so simple! Let's put the plan to action by pasting the approach comments into the function to serve as a guide.&lt;/p&gt;

&lt;p&gt;if you would like to take some time to figure this out do not scroll any further! Otherwise, keep scrolling when you are ready and prepare for spoilers!&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;// - create function caesarCipher(message, key)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// - create constant for alphabet characters, all caps&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// - create variable for the return string value (encoded message)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// - convert input string to upper case&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

  &lt;span class="c1"&gt;// - iterate over input string&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// -- create constant for the current character&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// -- check if current character is a letter and get the index of that letter in the alphabet&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// -- IF character is a letter:&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;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// --- subtract the key value from current character's index to get the index of the substitute character (encoded character)&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// --- IF the index for the substitute character is less than 0:&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;newPos&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&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="c1"&gt;// ---- the value for the substitute's index should instead be 25 plus the remainder of this index+1 and 26&lt;/span&gt;
        &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// --- get the substitute character at this new index from the alphabet constant and add it to the encoded message&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// -- ELSE if character is not a letter, add it to the encoded message without change&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;char&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;// - return the encoded message&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here is the function with no comments:&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;caesarCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&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;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ABCDEFGHIJKLMNOPQRSTUVWXYZ&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&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;pos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;char&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;pos&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;pos&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;key&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;newPos&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;26&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;newChar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;newPos&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;newChar&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;secret&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;char&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;secret&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;Let me take a moment to point out the use of the method &lt;code&gt;indexOf()&lt;/code&gt;. It returns the value of the index where the character provided in the argument is found in the target string or array. If the character is not in the string the method will return &lt;code&gt;-1&lt;/code&gt;. So if the the method returns a number greater than &lt;code&gt;-1&lt;/code&gt; we can assume it is a letter.&lt;/p&gt;

&lt;h3&gt;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;The exciting part is translating the approach into code. The fun part is testing the code! Let's take a look at the codepen provided below where I laid out some tests.&lt;/p&gt;

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

&lt;p&gt;🎉! We passed our own tests again! Nice! We should now give some thought to optimizing this function.&lt;/p&gt;

&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;Going over the alphabet is always going to be constant no matter the size of the input string, so not worth optimizing that. We do, though, create a new string the same size as the input string but in upper case when we use &lt;code&gt;message = message.toUpperCase()&lt;/code&gt;. I can imagine for a very large input string this would be a problem. Maybe we should only check if the upper case version matches without turning the entire string to upper case? I did time the differences for this kind of change and it seemed to go even slower. I will have to look into that some more and talk about it in the follow up to this article or else update this section. We do loop over the entire input string and that will always happen because we need to visit every character in the message. With that, we know the time complexity is going to remain O(n). The space complexity will be the same. So, at the moment there is no optimization obvious to me right now, except making the alphabet constant an object. If you have any input on this please comment below!&lt;/p&gt;

&lt;h2&gt;
  
  
  What's Next?
&lt;/h2&gt;

&lt;p&gt;Next, we are going to &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-caesar-decipher-33a8"&gt;decipher a coded message!&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You
&lt;/h2&gt;

&lt;p&gt;Once again, I would like to thank you for taking time out of your day to read this post. Follow me here on &lt;code&gt;dev.to&lt;/code&gt; if you'd like to see more content like this. I'll see you around!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>cryptography</category>
    </item>
    <item>
      <title>How I REACTO to Algos: Tournament Winner</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Sat, 05 Mar 2022 00:01:17 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-tournament-winner-20hk</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-tournament-winner-20hk</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;This is the third article in a series aiming to familiarize you with REACTO as a method of solving any algorithm problem. Today we are working on an algo found on &lt;a href="https://www.algoexpert.io/"&gt;AlgoExpert&lt;/a&gt; called &lt;code&gt;tournamentWinner&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Check out the previous article in the series, &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-17l"&gt;Pig Latin&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  This is REACTO
&lt;/h2&gt;

&lt;p&gt;REACTO is an acronym that represents the method we will use in solving this problem. As a reminder, these are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's stick to this order and get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;A tournament just ended and you need to find the winner. Each round has a pair of teams facing off and only one team wins. The winning team is awarded 3 points. Create a function called &lt;code&gt;tournamentWinner&lt;/code&gt; that takes two arrays as input and return the winner of the tournament. The first array, called &lt;code&gt;competitions&lt;/code&gt;, consists of arrays of two team names that faced each other in the round and will be in the form &lt;code&gt;["homeTeam", "awayTeam"]&lt;/code&gt;. The second argument is an array called &lt;code&gt;results&lt;/code&gt; whose elements will be either a &lt;code&gt;1&lt;/code&gt; or a &lt;code&gt;0&lt;/code&gt;. The same index at both arrays shows the teams that faced off and which of the teams won for that round. For example, at index &lt;code&gt;i&lt;/code&gt;, the teams at &lt;code&gt;competitions[i]&lt;/code&gt; competed against each other and the team represented by &lt;code&gt;results[i]&lt;/code&gt; is the winner from that pair. Note that &lt;code&gt;results[i]&lt;/code&gt; will be either a &lt;code&gt;1&lt;/code&gt; or a &lt;code&gt;0&lt;/code&gt;, with &lt;code&gt;1&lt;/code&gt; representing a win for the &lt;code&gt;homeTeam&lt;/code&gt;, and a &lt;code&gt;0&lt;/code&gt; representing a win for the &lt;code&gt;awayTeam&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;

&lt;p&gt;Now we will restate the prompt, which is important especially if you were given the prompt verbally.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

Given two arrays as input, one an array of team pairs
(each element is an array of two strings), and the other
an array of integers stating the winner of the pair, return
the team with the most points. The winning team in a pairing
is awarded 3 points while the other team in the pair is
awarded 0 points. The team with the most points after all
pairs are considered is the winner of the tournament. Each
team pairing from the first array has a home and an away
team. The second input array is the results array, contains
numbers of either 0 or 1, where 0 means the away team wins in
the pair and 1 means the home team wins for the pair.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now's a good time for questions and I happen to have one:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: Will a team face the same team twice?\&lt;br&gt;
A: No, each pairing is unique.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That was probably a given, but better to not assume!&lt;/p&gt;

&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;E is for examples and will usually be provided. If they are discussed verbally don't forget to write them down for reference because it could help guide in testing later.&lt;/p&gt;

&lt;p&gt;Below we will see the two input arrays labeled &lt;code&gt;competitions&lt;/code&gt; and &lt;code&gt;results&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;sample input&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;competitions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pandas&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pandas&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pythons&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pythons&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mice&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;results&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;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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;sample output&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Mice&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;We can see why Mice is the winning team. Mice beats Pandas, Pythons beat Pandas, Mice beat Pythons. The score would look like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Mice - 6 points\&lt;br&gt;
Pandas - 0 points\&lt;br&gt;
Pythons - 3 points&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So team Mice wins in this example!&lt;/p&gt;

&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;Here is where we plan our approach. We should keep the code to a minimum and really think through the steps we will take to reach a solution.&lt;/p&gt;

&lt;p&gt;On my first attempt this was my thought process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;create an object to act as a score keeper.&lt;/li&gt;
&lt;li&gt;declare a variable to hold the index so it can be used on both input arrays simultaneously&lt;/li&gt;
&lt;li&gt;declare another variable to hold the current high score until iteration over the arrays ends, can be initiated with value of 0&lt;/li&gt;
&lt;li&gt;use a while loop to iterate over both the arrays&lt;/li&gt;
&lt;li&gt;declare a variable to hold the string name of the tournament winner, and update it while looping&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's a general view of how I will approach this problem. It will be more of a brute force tactic. Usually I want to get to a solution first before I think of optimization. Remember optimization is the last step of REACTO anyway. As skills develop we may start thinking about more efficient methods first, but I expect any beginner to feel more comofortable with this approach as I am.&lt;/p&gt;

&lt;p&gt;So, if we are using a while loop what should we include inside of it?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loop only while the index value is less than the length of either of the input arrays (the arrays have the same amount of elements)&lt;/li&gt;
&lt;li&gt;declare a home and away variable and assign to them the values from the competitions array (&lt;code&gt;const [home, away] = competitions[indexValue]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;declare variable for winner of the pair and assign it the integer value from the results array at the given index value&lt;/li&gt;
&lt;li&gt;create conditional statements:

&lt;ul&gt;
&lt;li&gt;if winner is 0 (0 is the away team), then add entry to score keeper object with team name and value of 3... but if the already exists we just set value to += 3&lt;/li&gt;
&lt;li&gt;repeat for winner being 1&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;increase the index value at the end of the loop&lt;/li&gt;
&lt;li&gt;after the while loop we can iterate over the score keeping object

&lt;ul&gt;
&lt;li&gt;start with a conditional: if the value of current key is greater than the value of the high score variable, set the value of high score to the current key's value AND set the value of the tournament winner variable to the current key&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;lastly, return the string value from the tournament winner variable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay!! That was quite verbose, but it helps to be detailed in this step. My preferred method of writing an approach is to write them in as a comment inside of the function and use them as a guide for the code. Let's add the above as a comment first and then we will copy it off and paste it into our function when we are ready to code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function tournamentWinner() that takes two arrays as args; competitions and results. &amp;lt;&amp;lt;-- forgot to add this
Inside of the function:
- create an object to act as a score keeper.
- declare a variable to hold the index so it can be used on both input arrays simultaneously, set initial val to 0
- declare another variable to hold the current high score until iteration over the arrays ends, can be initiated with value of 0
- declare a variable to hold the string name of the tournament winner, and update it while looping
- use a while loop to iterate over both the arrays
- loop only while the index value is less than the length of either of the input arrays (the arrays have the same amount of elements)
- declare a home and away variable and assign to them the values from the competitions array (`const [home, away] = competitions[indexValue]`)
- declare variable for winner of the pair and assign it the integer value from the results array at the given index value
- create conditional statements:
  - if winner is 0 (0 is the away team), then add entry to score keeper object with team name and value of 3... but if the already exists we just set value to += 3
  - repeat for winner being 1
- increase the index value at the end of the loop
- after the while loop we can iterate over the score keeping object
  - start with a conditional: if the value of current key is greater than the value of the high score variable, set the value of high score to the current key's value AND set the value of the tournament winner variable to the current key
- lastly, return the string value from the tournament winner variable
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I added some more to those comments for clarity. And yeah, it looks rather clunky but we will get tp tidying later. Now it is time to code.&lt;/p&gt;

&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;Time to code! 🧑‍💻&lt;br&gt;
If you've read my other articles in this series you'll know that I like to copy my Approach comments and paste them into my code as a guide.&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;// create function tournamentWinner() that takes two arrays as args; competitions and results. &amp;lt;&amp;lt;-- forgot to add this&lt;/span&gt;
&lt;span class="cm"&gt;/* Inside of the function: */&lt;/span&gt;
&lt;span class="c1"&gt;// create an object to act as a score keeper.&lt;/span&gt;
&lt;span class="c1"&gt;// declare a variable to hold the index so it can be used on both input arrays simultaneously, set initial val to 0&lt;/span&gt;
&lt;span class="c1"&gt;// declare another variable to hold the current high score until iteration over the arrays ends, can be initiated with value of 0&lt;/span&gt;
&lt;span class="c1"&gt;// declare a variable to hold the string name of the tournament winner, and update it while looping&lt;/span&gt;
&lt;span class="c1"&gt;// use a while loop to iterate over both the arrays&lt;/span&gt;
&lt;span class="c1"&gt;// loop only while the index value is less than the length of either of the input arrays (the arrays have the same amount of elements)&lt;/span&gt;
&lt;span class="c1"&gt;// declare a home and away variable and assign to them the values from the competitions array (`const [home, away] = competitions[indexValue]`)&lt;/span&gt;
&lt;span class="c1"&gt;// declare variable for winner of the pair and assign it the integer value from the results array at the given index value&lt;/span&gt;
&lt;span class="c1"&gt;// create conditional statements:&lt;/span&gt;
&lt;span class="c1"&gt;//// if winner is 0 (0 is the away team), then add entry to score keeper object with team name and value of 3... but if the already exists we just set value to += 3&lt;/span&gt;
&lt;span class="c1"&gt;//// repeat for winner being 1&lt;/span&gt;
&lt;span class="c1"&gt;// increase the index value at the end of the loop&lt;/span&gt;
&lt;span class="c1"&gt;// after the while loop we can iterate over the score keeping object&lt;/span&gt;
&lt;span class="c1"&gt;//// start with a conditional: if the value of current key is greater than the value of the high score variable, set the value of high score to the current key's value AND set the value of the tournament winner variable to the current key&lt;/span&gt;
&lt;span class="c1"&gt;// lastly, return the string value from the tournament winner variable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comments have been reformatted into single line comments so that they may be moved around easily. Now that the approach is laid out in the coding environment of choice we can start writing JavaScript (or your language of choice). What you will see next are the comments and their translation into JavaScript.&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;// create function tournamentWinner() that takes two arrays as args; competitions and results.&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;tournamentWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/* Inside of the function: */&lt;/span&gt;
  &lt;span class="c1"&gt;// create an object to act as a score keeper.&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

  &lt;span class="c1"&gt;// declare a variable to hold the index so it can be used on both input arrays simultaneously, set initial val to 0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tournamentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// declare another variable to hold the current high score until iteration over the arrays ends, can be initiated with value of 0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;highScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// declare a variable to hold the string name of the tournament winner, and update it while looping&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;champ&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// use a while loop to iterate over both the arrays&lt;/span&gt;
  &lt;span class="c1"&gt;// loop only while the index value is less than the length of either of the input arrays (the arrays have the same amount of elements)&lt;/span&gt;
  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// declare a home and away variable and assign to them the values from the competitions array (`const [home, away] = competitions[indexValue]`)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// declare variable for winner of the pair and assign it the integer value from the results array at the given index value&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// create conditional statements:&lt;/span&gt;
    &lt;span class="c1"&gt;// if winner is 0 (0 is the away team), then add entry to score keeper object with team name and value of 3... but if the already exists we just set value to += 3&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// repeat for winner being 1&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// increase the index value at the end of the loop&lt;/span&gt;
    &lt;span class="nx"&gt;tournamentIdx&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;// after the while loop we can iterate over the score keeping object&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// start with a conditional: if the value of current key is greater than the value of the high score variable, set the value of high score to the current key's value AND set the value of the tournament winner variable to the current key&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;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;highScore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;highScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;champ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;key&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;// lastly, return the string value from the tournament winner variable&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;champ&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;That should be everything! Now I am going to remove the comments for readability, but I would usually keep comments in if I am saving this to my local machine so that I may review the thought process in the future. Here's the code without the comments:&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;tournamentWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;results&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;leaderboard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;tournamentIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;highScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;champ&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&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;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;tournamentIdx&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&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="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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;winner&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt;

    &lt;span class="nx"&gt;tournamentIdx&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;highScore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;highScore&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="nx"&gt;champ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;key&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;champ&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;Looking better! Let's test it out.&lt;/p&gt;

&lt;h3&gt;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;Testing time is here again! Here is a Codepen with the function in the JS tab on the left and the results on the right. Feel free to play around with the code and explore.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;We passed our own tests! 🎉 Oh yeah! Now let's optimize it because you probably noticed we have two loops in the function. That means we loop over the arrays once, in one loop, and then we loop over the score keeping object. We don't need to do the latter, so let's look at a more optimized version below:&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;tournamentWinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;results&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;champ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="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;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;results&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;home&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;competitions&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;winner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;result&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;home&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;away&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;updateLeaderboard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&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;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;winner&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;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;champ&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;champ&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;winner&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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;champ&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;updateLeaderboard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;points&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;]&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="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;leaderboard&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;team&lt;/span&gt;&lt;span class="p"&gt;]&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="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;You can see how we made use of a helper function (thanks to &lt;a href="https://www.algoexpert.io/"&gt;AlgoExpert&lt;/a&gt; for the helpful guidance) and only make one loop. Let that optimized code sink in! You can see how some things done in the first attempt were unnecessary but did not hinder our progress to a valid solution. If you have any questions or suggestions, please leave a comment below!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You
&lt;/h2&gt;

&lt;p&gt;Once again. I would like to thank you for taking time out of your day to read this post. Follow me here on dev.to if you'd like to see more content like this as I post about my explorations into the world of web development. I'll see you around!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How I REACTO To Algos: Pig Latin</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Mon, 21 Feb 2022 16:00:11 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-17l</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-17l</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;This is the second article in a series aiming to familiarize you with REACTO. Today we will tackle a fun one I found on Codewars to translate text to Pig Latin. 🐽&lt;/p&gt;

&lt;p&gt;Check out the first article in the series, &lt;a href="https://dev.to/amhernandez/how-i-reacto-to-algos-33ag"&gt;First Double&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Remember REACTO?
&lt;/h2&gt;

&lt;p&gt;REACTO is an acronym that represents the method we will use in solving this problem. As a reminder, these are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's the order and we're sticking to it. Let's get started!&lt;/p&gt;

&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;You'll be given a string of words. Create a function called &lt;code&gt;toPigLatin&lt;/code&gt; that moves the first letter of each word in a string to the end of the word, then adds &lt;strong&gt;"ay"&lt;/strong&gt; to the end of the word before returning the newly formed string. Leave punctuation marks untouched.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That, right there, is how Pig Latin is made. 🐷💖&lt;/p&gt;

&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;

&lt;p&gt;Here we are in the first step, so let's do what it recommends!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

given a string, create function that takes each word of the
string, moves its first letter to the end and adds "ay"
before returning the string.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This time I am going to ask the imaginary instructor some clarifying questions:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Q: Should the return value preserve letter case?&lt;br&gt;
A: No, the string returned should be in all lower case.&lt;/p&gt;

&lt;p&gt;Q: Will the function receive a string with numbers in it?&lt;br&gt;
A: No.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In light of this new information, the &lt;strong&gt;R: Restate&lt;/strong&gt; should be modified:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/* 
R: Restate

- given a string, create function that takes each word of the string, moves its first letter to the end and adds "ay" before returning the string. 
- The return value should be in lower case.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;You'll always get examples, but if none are supplied you can always ask! Here are some examples of inputs and their expected outputs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
E: Examples

toPigLatin('Pig latin is cool.'); // igpay atinlay siay oolcay.
toPigLatin('Hello world!');     // ellohay orldway!
toPigLatin('I know this is a great place'); // iay nowkay histay siay aay reatgay lacepay
toPigLatin("We can't do this."); // eway an'tcay oday histay.
toPigLatin('Is this the way?'); // siay histay hetay ayway?
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;Now it is time to write out the approach to take before writing any actual code. Pseudocode is great here.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
A: Approach

- create function toPigLatin that takes a string argument
- assign letters of the alphabet to variable with all letters in lower case, call it alphabet
- declare a variable to hold the final string, initialized to empty string, call it pigStr
- split the argument string where there are spaces, " ", which will create an array of words
- iterate over this array of words
- - declare variable to hold current word in loop and make it lower case
- - check if the last character of this word is a letter (is it in the alphabet string?)
- - - if the character is not a letter:
- - - - take characters from word, skipping the first and last, and add to pigStr followed by first character, then "ay", then the last character of the word.(pig! &amp;gt;&amp;gt; ig + p + ay + ! &amp;gt;&amp;gt; igpay!)
- - - else take the word but skip the first letter and add it to pigStr followed by the first letter of the word and then "ay". (cat &amp;gt;&amp;gt; at + c + ay &amp;gt;&amp;gt; atcay)
- - at the end of every loop we should add a space, " ", to pigStr unless it is the last loop.
- return pigStr
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are many ways to get to the solution and the path I have laid out is going to be more verbose than what you might come up with if you aren't a beginner. Let's complete this challenge first and then we can tidy up. 🧹&lt;/p&gt;

&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;Time to code! 🧑‍💻&lt;br&gt;
If you've read the previous article in this series you'll know that I like to copy my Approach comments and paste them into my code as a guide.&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;// create function toPigLatin that takes a string argument&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;toPigLatin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// assign letters of the alphabet to variable with all letters in lower case, call it alphabet&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// declare a variable to hold the final string, initialized to empty string, call it pigStr&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;// split the argument string where there are spaces, " ", which will create an array of words&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// iterate over this array of words&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// declare variable to hold current word in loop and make it lower case&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// check if the last character of this word is a letter (is it in the alphabet string?)&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;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&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 the character is not a letter:&lt;/span&gt;
      &lt;span class="c1"&gt;// take characters from word, skipping the first and last, and add to pigStr followed by first character, then "ay", then the last character of the word.(pig! &amp;gt;&amp;gt; ig + p + ay + ! &amp;gt;&amp;gt; igpay!)&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// else take the word but skip the first letter and add it to pigStr followed by the first letter of the word and then "ay". (cat &amp;gt;&amp;gt; at + c + ay &amp;gt;&amp;gt; atcay)&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// at the end of every loop we should add a space, " ", to pigStr unless it is the last loop.&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;i&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &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="c1"&gt;// return pigStr&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pigStr&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;That's everything for the code! We are just going to clean up the comments now and add it to a Codepen along with some &lt;code&gt;console.log&lt;/code&gt; statements serving as tests.&lt;br&gt;
Here's the function without comments:&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;toPigLatin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&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;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;wordsArr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;pigStr&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &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="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pigStr&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's test this!&lt;/p&gt;

&lt;h3&gt;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;Testing time! Here is a Codepen with the function in the JS tab and the results. Feel free to play around with the code and explore.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;We passed our own tests! 🎉 Big celebration! Now let's optimize, if possible.&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;toPigLatin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;alphabet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;abcdefghijklmnopqrstuvwxyz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;pigArr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;pigArr&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;word&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;word&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toLowerCase&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;alphabet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;false&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;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;ay&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;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Can you tell what changes were made? Take a look and don't forget to copy and paste this into the Codepen above to see if you get the same result. Or &lt;a href="https://codepen.io/am-hernandez/pen/oNoEMGR?editors=0011"&gt;visit the Codepen here&lt;/a&gt; and get to exploring! It's almost as cute as a pig! 🐖🐖!&lt;/p&gt;

&lt;h2&gt;
  
  
  Thank You
&lt;/h2&gt;

&lt;p&gt;I want to thank you for taking time out of your day to read this post. Follow me here on dev.to if you'd like to see more content like this. I post about my explorations into the world of web development. I'll see you around!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How I REACTO to Algos: First Double</title>
      <dc:creator>A.M. Hernandez</dc:creator>
      <pubDate>Fri, 11 Feb 2022 22:03:28 +0000</pubDate>
      <link>https://dev.to/amhernandez/how-i-reacto-to-algos-33ag</link>
      <guid>https://dev.to/amhernandez/how-i-reacto-to-algos-33ag</guid>
      <description>&lt;h2&gt;
  
  
  How I react to algos
&lt;/h2&gt;

&lt;p&gt;Ah, see what I did there? REACTO and, "react to." Oh, never mind! This is an article about REACTO and how I've learned to love this common approach to solving problems.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is REACTO?
&lt;/h2&gt;

&lt;p&gt;Simply put, REACTO is an acronym that represents the stages of the problem solving with this method. These are the steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;R&lt;/strong&gt;: Restate&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;E&lt;/strong&gt;: Example&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A&lt;/strong&gt;: Approach&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;C&lt;/strong&gt;: Code&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;T&lt;/strong&gt;: Test&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O&lt;/strong&gt;: Optimize&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And yes, in that order. It is important to follow the steps so that you don't get lost in a thought-storm 🌩️. This approach will keep you focused and illuminate the path to the solution before you even begin coding! The trick is to hold off on coding right away, since our natural instinct may be to just jump into the code before making a plan of attack.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Prompt
&lt;/h2&gt;

&lt;p&gt;Okay, say you get a prompt from an algorithms repository of your choice, now what? Let's start with one of the simplest prompts I could find so we don't get too carried away in the coding part.&lt;/p&gt;

&lt;p&gt;Here's the prompt:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Create a function that, when given some input, will take this input and return the first instance of double characters. Input will be a string. If there are no double characters found in the input the function should return boolean &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This one is pretty straightforward. Let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  R: Restate the prompt
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
Restate,

Create a function
- takes input type of string
- return the first instance of double characters
Input may be a string
If there are no double characters found in the input the function should return `false`.
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one is pretty easy to do here, but you may actually only hear the prompt from an interviewer if this was a live interview. Make sure you're actively listening to the prompt and remember to ask clarifying questions. For instance, you might have asked if numbers may be passed into the function or just strings. In this case, the input will be restricted to string values. You may come up with more questions while reading this post, so if you do, please comment below. I'm still learning too!&lt;/p&gt;

&lt;h3&gt;
  
  
  E: Examples
&lt;/h3&gt;

&lt;p&gt;Here is where you will want to write down some examples to visually aid you in your approach later. Your interviewer should give you some examples. If not, now is the time to ask for them! If you are taking a prompt from an online source like Codewars then they will have examples available.&lt;br&gt;
Again, I write these in as comments just after the &lt;strong&gt;Restate&lt;/strong&gt; section. Below, you will see I am naming this function &lt;code&gt;firstDouble&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...

Examples,

firstDouble("aardvark") &amp;gt;&amp;gt; "aa"
firstDouble("1-800-257-8999") &amp;gt;&amp;gt; "00"
firstDouble("pamphlet") &amp;gt;&amp;gt; false
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A: Approach
&lt;/h3&gt;

&lt;p&gt;Here you need to write out your approach to coding a solution. You will write pseudocode here or just write out your plan without using a coding language. Let's add this plan in the comment as well.&lt;/p&gt;

&lt;p&gt;First, you know that you will have an argument passed into the function, that is a string, and if no match is found it should return the boolean &lt;code&gt;false&lt;/code&gt;. Now, if you're used to &lt;em&gt;test-driven development&lt;/em&gt; you'd likely write tests first and then write code that satisfies the tests. In this case we are waiting for the &lt;strong&gt;T: Test&lt;/strong&gt; step to do that. So I am going to note the function name and the argument passed into the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...

Approach,

- create function firstDouble(stringArg)
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, that does look a lot like Javascript, but I won't get too much deeper than that in the &lt;strong&gt;Approach&lt;/strong&gt; step. You know what kind of argument passes into the function and the function created. Let's add some more about how to begin parsing the input.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...

Approach,

- create function firstDouble(stringArg)
- iterate over the input
-- check if the current character is the same as the previous character
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, when I realize I need to compare the current iteration to the last I know I'll need to create a variable to hold onto the value as I move into the next loop. So I will edit the approach to include this consideration in the second step of &lt;strong&gt;Approach&lt;/strong&gt;, before the loop. While I am at it, I'll add that I need to declare a variable in the loop for the current character. This way when the loop ends I could assign the current character's value to the previous character variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...

Approach,

- create function firstDouble(stringArg)
- declare variable to hold value of character from previous loop (lastCharacter)
- iterate over the input
-- declare variable for current character (currentCharacter)
-- check if the current character is the same as the previous character
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Wait, how many times do we loop? Well, it should be just as long as the input length. I'll add that into my approach. Now, I am thinking of the loop like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;loop until we reach end of the input&lt;/li&gt;
&lt;li&gt;each loop we will set a current character and then compare it to the last character&lt;/li&gt;
&lt;li&gt;if the current and last character are the same we should return them both keeping to the same type as they were inputted&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;or&lt;/em&gt; if they don't match, set last character's value to current character&lt;/li&gt;
&lt;li&gt;loop again&lt;/li&gt;
&lt;li&gt;if the looping ends with no matches return &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's look at this approach:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...

Approach,

- create function firstDouble(stringArg)
- declare variable to hold value of character from previous loop (lastCharacter)
- iterate over the input for the length of the input
-- declare variable for current character (currentCharacter)
-- check if the currentCharacter has the same value as the lastCharacter
---- if they match, return them both together as a string
---- else if they don't match, 
     set value of last character to equal current character
-- loop again
- if no matches found after looping ends, return boolean false
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That definitely seems like everything we need to solve the prompt. By now this is how the &lt;strong&gt;REA&lt;/strong&gt; in &lt;strong&gt;REACTO&lt;/strong&gt; looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
Restate,

Create a function
- takes input type of string
- return the first instance of double characters
Input may be a string
If there are no double characters found in the input the function should return `false`.
Examples,

firstDouble("aardvark") &amp;gt; "aa"
firstDouble("1-800-257-8999") &amp;gt; "00"
firstDouble("pamphlet") &amp;gt; false

Approach,

- create function firstDouble(stringArg)
- declare variable to hold value of character from previous loop (lastCharacter)
- iterate over the input for the length of the input
-- declare variable for current character (currentCharacter)
-- check if the currentCharacter has the same value as the lastCharacter
---- if they match, return them both together as a string
---- else if they don't match, 
     set value of last character to equal current character
-- loop again
- if no matches found after looping ends, return boolean false
*/&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  C: Code
&lt;/h3&gt;

&lt;p&gt;Let's finally move on to the &lt;strong&gt;C&lt;/strong&gt; for coding step! in this step, the code is not in a comment, but I leave a small comment above it to show this is the code section. Here is the &lt;strong&gt;Code&lt;/strong&gt; step with just the function created:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="cm"&gt;/*
...
*/&lt;/span&gt;

&lt;span class="cm"&gt;/*
 ** Code,
 */&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;firstDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringArg&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;Wow, we are almost there! 😎 Only need to implement the approach laid out in the previous step and then the function can be tested. I sometimes will paste my Approach comment into the function body to serve as a guide.&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;firstDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//- create function firstDouble(stringArg)&lt;/span&gt;
  &lt;span class="c1"&gt;//- declare variable to hold value of character from previous loop (lastCharacter)&lt;/span&gt;
  &lt;span class="c1"&gt;//- iterate over the input for the length of the input&lt;/span&gt;
  &lt;span class="c1"&gt;//-- declare variable for current character (currentCharacter)&lt;/span&gt;
  &lt;span class="c1"&gt;//-- check if the currentCharacter has the same value as the lastCharacter&lt;/span&gt;
  &lt;span class="c1"&gt;//---- if they match, return them both together as a string&lt;/span&gt;
  &lt;span class="c1"&gt;//---- else if they don't match,&lt;/span&gt;
  &lt;span class="c1"&gt;//    set value of last character to equal current character&lt;/span&gt;
  &lt;span class="c1"&gt;//-- loop again&lt;/span&gt;
  &lt;span class="c1"&gt;//- if no matches found after looping ends, return boolean false&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 move the first comment outside of the function since it correlates to the creation of the function. Then I will go ahead and start coding:&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;//- create function firstDouble(stringArg)&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;firstDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;//- declare variable to hold value of character from previous loop (lastCharacter)&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="c1"&gt;//- iterate over the input for the length of the input&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//-- declare variable for current character (currentCharacter)&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;//-- check if the currentCharacter has the same value as the lastCharacter&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;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lastCharacter&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 they match, return them both together as a string&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;currentCharacter&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;//---- else if they don't match, set value of last character to equal current character&lt;/span&gt;
      &lt;span class="nx"&gt;lastCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentCharacter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//-- loop again&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="c1"&gt;//- if no matches found after looping ends, return boolean false&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, &lt;strong&gt;C: Code&lt;/strong&gt; is now done. I am going to remove the comments so it is easier to read:&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;firstDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringArg&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;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lastCharacter&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;currentCharacter&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;lastCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentCharacter&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, I know you might be thinking that you could have solved this problem without the extra time spent following the REACTO methodology, and that's natural. Just know that as the problems increase in complexity this approach will make solving them more manageable.&lt;/p&gt;

&lt;p&gt;The next step is to test the code!&lt;/p&gt;

&lt;h3&gt;
  
  
  T: Test
&lt;/h3&gt;

&lt;p&gt;Now comes the time to test the code. You can use whatever testing library you'd prefer. I am going to link a codepen here using &lt;code&gt;console.log()&lt;/code&gt; to show the results.&lt;/p&gt;

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

&lt;p&gt;In the above Codepen, click the JS tab to see the tests. They are simple log statements. Here they are, from &lt;strong&gt;E: Examples&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt; firstDouble("aardvark");
aa
&amp;gt; firstDouble("1-800-257-8999");
00
&amp;gt; firstDouble("pamphlet");
false

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  O: Optimize
&lt;/h3&gt;

&lt;p&gt;We passed our own tests! 🎉 Yay! Now let's optimize, if possible.&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;firstDouble&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stringArg&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;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;stringArg&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;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;stringArg&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&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;currentCharacter&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;lastCharacter&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;lastCharacter&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;currentCharacter&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="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;lastCharacter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;currentCharacter&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="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Um, that wasn't much of a change and didn't optimize the function but it does look more tidy. This is as far as we need to take this one. 🎉 Congrats, you've read a very long post, and my first one at that! Thank you very much for sticking around and please comment if you would like to share any tips! Did I mess up somewhere? Please don't hesitate to let me know.&lt;/p&gt;

&lt;p&gt;Also, if you'd like to mess around with this code further &lt;a href="https://codepen.io/am-hernandez/pen/vYWZdPZ?editors=0010"&gt;check it out the Codepen&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
