<?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: James Kelsey</title>
    <description>The latest articles on DEV Community by James Kelsey (@jamesmskelsey).</description>
    <link>https://dev.to/jamesmskelsey</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%2F68018%2Fe44ec35c-d243-415d-83e2-20f983977b9d.png</url>
      <title>DEV Community: James Kelsey</title>
      <link>https://dev.to/jamesmskelsey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamesmskelsey"/>
    <language>en</language>
    <item>
      <title>Overthinking Problems for Fun and Learning - An Example with Arrays</title>
      <dc:creator>James Kelsey</dc:creator>
      <pubDate>Tue, 09 Feb 2021 04:29:00 +0000</pubDate>
      <link>https://dev.to/jamesmskelsey/overthink-it-searching-arrays-of-arrays-3m97</link>
      <guid>https://dev.to/jamesmskelsey/overthink-it-searching-arrays-of-arrays-3m97</guid>
      <description>&lt;h1&gt;
  
  
  Why?
&lt;/h1&gt;

&lt;p&gt;Recently I started doing something that I think is a lot of fun. Over thinking every problem I find. Exhaustively.&lt;/p&gt;

&lt;p&gt;In this case, I was compelled by &lt;a href="https://www.freecodecamp.org"&gt;freeCodeCamp&lt;/a&gt; to "Return the Largest Numbers in Arrays". I just decided to see how many ways I could solve it. Really dive deep in to the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array"&gt;MDN&lt;/a&gt; documentation for JavaScript Arrays to see what I could find. The problem at hand was this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Return an array consisting of the largest number from each provided sub-array. For simplicity, the provided array will contain exactly 4 sub-arrays.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h1&gt;
  
  
  What'd I find?
&lt;/h1&gt;

&lt;p&gt;I found four ways to solve the problem. &lt;/p&gt;

&lt;h2&gt;
  
  
  Array.map()
&lt;/h2&gt;

&lt;p&gt;The first is using probably the most common method. Here I use &lt;code&gt;Array.map()&lt;/code&gt; to return an array of the highest value found by &lt;code&gt;Math.max()&lt;/code&gt;. It's short, it's sweet. If you're not aware, the map() function takes in a function that it applies to each of the elements of it's array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_dSvYdDJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ag3plar63u9l5ksg1fyn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_dSvYdDJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ag3plar63u9l5ksg1fyn.png" alt="Shortest method to solve the problem discussed"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;largestOfFourRefactored([[1,2,3,4],[10,9,3,5],[100,101,95,92],[85,98,74,12])
// returns [4,10,101,98]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Nested Loops
&lt;/h2&gt;

&lt;p&gt;The next is probably no surprise. It's really just &lt;code&gt;Array.map()&lt;/code&gt; in long hand. I use the first loop (i) to go through the four subarrays, then use the inner loop (j) to find the maximum value for each of them. After the inner loop, I use a bit of that new(ish) array destructuring to build up my output, and then finally return it. The output is the same as above. Much more difficult to read though. &lt;/p&gt;

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

&lt;h2&gt;
  
  
  Array.from()
&lt;/h2&gt;

&lt;p&gt;I'll admit, I actually didn't know about &lt;code&gt;Array.from()&lt;/code&gt; until yesterday when I was writing this code. This method takes in an array like object as it's first parameter and then runs the supplied function on each of them. It then returns the resulting array. &lt;/p&gt;

&lt;p&gt;The example that MDN gives looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Array.from([1,2,3], x =&amp;gt; x * 2)
// [2,4,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we can use it almost exactly like &lt;code&gt;Array.map()&lt;/code&gt;. But it was new to me, so I wanted to share it. It can also do this with strings, so you could use it to filter letters out of a string if you wanted.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  Array.forEach()
&lt;/h2&gt;

&lt;p&gt;This example sort of combines &lt;code&gt;Array.map()&lt;/code&gt; and the long hand form. I have to set aside an &lt;code&gt;output&lt;/code&gt; variable, and then return it. But then I have to push data in to the array from &lt;code&gt;forEach()&lt;/code&gt;. It works just as well as the others, and in a similar fashion.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LeATheyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lrukkj45z8nzhfqa92kv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LeATheyK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lrukkj45z8nzhfqa92kv.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;I'll leave it up to you to decide which you like best. I personally prefer option one. It's so silky smooth and is easy to read. I know what I'm going to get from that right away. The others add in extra steps that aren't necessary... but do highlight the point that there are lots of ways to go about a problem.&lt;/p&gt;

&lt;p&gt;I'd love to hear about it if you found other ways to do this problem. I'll definitely be posting more of these.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This post is based on a video from &lt;a href="https://www.youtube.com/channel/UCV3CqbXc3vdWijpHM55g9bA"&gt;my YouTube channel&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

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