<?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: JEFFREY M JAMES</title>
    <description>The latest articles on DEV Community by JEFFREY M JAMES (@jeffjames9).</description>
    <link>https://dev.to/jeffjames9</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%2F378647%2F1ec9371d-5c51-4d95-bb35-fce1903ac24a.jpeg</url>
      <title>DEV Community: JEFFREY M JAMES</title>
      <link>https://dev.to/jeffjames9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeffjames9"/>
    <language>en</language>
    <item>
      <title>A shallow plunge into DeepEquals</title>
      <dc:creator>JEFFREY M JAMES</dc:creator>
      <pubDate>Mon, 18 May 2020 06:56:16 +0000</pubDate>
      <link>https://dev.to/jeffjames9/a-shallow-plunge-into-deepequals-2kjm</link>
      <guid>https://dev.to/jeffjames9/a-shallow-plunge-into-deepequals-2kjm</guid>
      <description>&lt;p&gt;With nights taken up learning and writing React, Angular JS, and Backbone, my morning toy problems have been a welcome distraction this past week.  And from once, not so long ago, being fearful of recursion - I've gotten to really enjoy these challenges.  Here's the most interesting recursion toy problem I was presented with this week - Deep Equality.&lt;/p&gt;

&lt;p&gt;Here are the rules:&lt;/p&gt;

&lt;p&gt;---Write a function deepEquals that takes in two objects and returns whether &lt;br&gt;
---or not the two are deeply equivalent. This means the structure of the two&lt;br&gt;
---objects is the same, and so is the structure of each of their corresponding&lt;br&gt;
---descendants.&lt;/p&gt;

&lt;p&gt;Easy enough right?  Let's get some IOCE out there to help us out:&lt;/p&gt;

&lt;p&gt;Input: two objects&lt;br&gt;
Output: boolean&lt;br&gt;
Constraints: there's a test that says not to use JSON.stringify&lt;br&gt;
Edge Cases: &lt;/p&gt;

&lt;p&gt;What to do now?  We are going to have to go into an object and check for equality - then we're going to have to go into any nested objects and check for equality, and if that object has another nested object...  and so on.  Smells like our old friend recursion.  So let's get this set up:&lt;/p&gt;

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

&lt;p&gt;The boolean is going to be returned as true unless we have any reason to change it to false.  We've also got the bones of our recursion and the call to it on line 12.  Now it's time to build out our recursion.  The first thing we should check is to see if the first layer of the objects have the same number of keys.  If the second object had more keys than the first, it could trigger a false positive (and there is definitely a test for that!).  Change the boolean to false if they aren't the same length.&lt;/p&gt;

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

&lt;p&gt;So now that we know the lengths are the same, let's compare actual key/value pairs.  We can run a forEach loop on one of our objects.  If that key does not exist in the other object or the value is not the same, we need to change the boolean to false.&lt;/p&gt;

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

&lt;p&gt;Else - if we've made it this far... let's check the typeof on the value of the key to see if we're still dealing with an object.  If we are, we need to keep going... let's call the recursion on this object.&lt;/p&gt;

&lt;p&gt;So once our recursion is finished, our boolean is either changed to false or it has not been - let's return the boolean to complete this exercise.&lt;/p&gt;

&lt;p&gt;What a difference a few weeks makes.  Recursion has gone from being my sworn enemy to an arrow in my JavaScript quiver.  If you're not there already, with a little patience and diligent work you soon can be.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Rock, Paper, Scissors:  A Recursive Jaunt</title>
      <dc:creator>JEFFREY M JAMES</dc:creator>
      <pubDate>Mon, 11 May 2020 04:21:41 +0000</pubDate>
      <link>https://dev.to/jeffjames9/rock-paper-scissors-a-recursive-jaunt-2jd2</link>
      <guid>https://dev.to/jeffjames9/rock-paper-scissors-a-recursive-jaunt-2jd2</guid>
      <description>&lt;p&gt;Since being introduced to recursion, I've wanted to dig deeper into the subject and see the process more visually.  Maybe if I can visualize it, it will come more naturally?  So enough of kicking that can down the road... let's take the time to look at this recursion problem called Rock, Paper, Scissors!&lt;/p&gt;

&lt;p&gt;With this Toy Problem, we are are given an input of n - which represents the number of rounds a single player will play paper, rock, scissors.  We are asked to return every different possibility one person could throw as an array - and all of those arrays will be within one larger return array.&lt;/p&gt;

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

&lt;p&gt;If we knew n, we could potentially skirt the rules and use nested loops, but alas...  sounds like our old friends recursion.  Put simply, recursion is when a function calls itself until it doesn't anymore...&lt;/p&gt;

&lt;p&gt;Here, we're going to create an array and use a recursive function to build up that array until we return it at the end - let's get set up.&lt;/p&gt;

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

&lt;p&gt;Boom.  There's an array and there's a return statement for the array at the end, after we build it.  What else would help?  Well, let's throw an array out there that has all the possible throws - rock, paper, and scissors.  Also, we know a recursive function is coming, so let's get that staged.&lt;/p&gt;

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

&lt;p&gt;The other new things you might notice here are the actual function call on line 51 and the function parameters on line 46.  Each result array that we are going to add to 'differentThrowCombos' to return will be built up from the empty array on line 51 by being inserted for the parameter of the recursive function.  The roundsToGo is going to count down the rounds until it is decremented to zero, by which time it will be an array of 3 items and get pushed into the differentThrowCombos array.&lt;/p&gt;

&lt;p&gt;So now let's see the magic!  ...after we look at one more pic to see the recursive case.  The loop will come to a close when 3 items have been concatenated into an array - we'll know this because the rounds have been decremented to 0.  Let's push it to a results array!  I feel someone will be upset if I don't utter "base case"...so there it is. &lt;/p&gt;

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

&lt;p&gt;So, let's take a look at the recursion and then we can break it down a little further:&lt;/p&gt;

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

&lt;p&gt;*For much of the following explanation, we will assume an 'n' of three - there are 3 rounds.&lt;/p&gt;

&lt;p&gt;Let's talk briefly about what's going on here and then I'll share some diagrammed pics that may help you think about how the recursion is happening.  There are 3 choices in the 'choicesOfThrow' array.  We are using a forEach loop to cycle through those choices.  'thro' here is either going to be 'rock', 'paper', or 'scissors.  &lt;/p&gt;

&lt;p&gt;So, for whichever one of those we are currently looping on, we are going to recursively call the function for that value concatenated with the current array - which on the first level is just an array literal - so we are calling the function recursively with ['rock'] and the decremented roundsToGo of 2 -getCombos(['rock'], 2).  This will go through the function again until it is sent back to function again as getCombos(['rock', 'rock'], 1).  &lt;/p&gt;

&lt;p&gt;While the paper and scissors portions are still waiting to go through the earlier forEach loops, the most recent loops keep executing.  The rounds will go to zero for our first array of ['rock','rock','rock'] and it will reach the base case where it is pushed into the 'differentThrowCombos' array first.  And now we can go back to the loop that has stalled at [rock, rock] and finish the forEach there - ['rock','rock','paper'] and ['rock','rock','scissors'] will be the next to be added to the 'differentThrowCombos' array.  Then back to finish the the previous loop and so on.&lt;/p&gt;

&lt;p&gt;Here is rock paper scissors in tree structure.  Spend a few minutes looking at how we get the results in order from left to right.  I feel like visualizing recursion has helped me tremendously and may help you too.&lt;/p&gt;

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

</description>
    </item>
    <item>
      <title>Javascript Instantiation</title>
      <dc:creator>JEFFREY M JAMES</dc:creator>
      <pubDate>Sun, 03 May 2020 20:34:11 +0000</pubDate>
      <link>https://dev.to/jeffjames9/javascript-instantiation-36d5</link>
      <guid>https://dev.to/jeffjames9/javascript-instantiation-36d5</guid>
      <description>&lt;p&gt;An instantiation pattern is a process of creating a class of objects.  There are 5 different instantiation patterns in modern Javascript. If we were creating the pieces for a game of chess, the top class could conceivably be "GamePieces" which all contain a series of similar properties like color and position.  It would also make sense if each piece contained a function to move it to a different place on the board - maybe a moveTo function which updates its location.  &lt;/p&gt;

&lt;p&gt;We could create each game piece individually, but rewriting the functionality for each piece will take a lot of time and make it harder to revise the code.  So, we are going to create a "factory" to create new instances of GamePieces - such as pawn1W and queenB  which will all share some common traits and methods.&lt;/p&gt;

&lt;p&gt;1) Functional - with the functional instantiation pattern, we will use a class production function (GamePieces) to create a new instance (pawn1W) of a piece for the board.  The functional style will put a copy of each of the included methods (moveTo) onto each GamePieces instance.&lt;/p&gt;

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

&lt;p&gt;2) Functional Shared - this instantiation pattern is just like functional, but with one large bonus.  With this instantiation pattern, we don't have to give all of the methods to each instance we create.  Instead, we use _.extend to give each instance a reference to where the method can be found.&lt;/p&gt;

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

&lt;p&gt;3) Prototypal - instead of using _.extend to copy over a reference to each method, this instantiation pattern uses Object.create() to create an object which will point at another object in cases where the instance itself does not contain that method/value.  Take a look at this and let's meet below the graphic to discuss...&lt;/p&gt;

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

&lt;p&gt;...so, when .moveTo is called for pawn1W, the compiler will not find that method on pawn1W, but because of Object.create(pieceFuncs), it will know to go to the pieceFuncs object to look for .moveTo.&lt;/p&gt;

&lt;p&gt;4) Pseudoclassical - this instantiation class's name is a nod to the author's fascination with late 70s yacht rock.  Other names considered, such as "The Loggins" style, were decidedly too edgey for a young programming language attempting to be taken seriously.  This style adds a couple of interesting things.  First, every function in javascript is an object - and that object comes with an empty object already attached to it named 'prototype'.  This prototype object on the constructor function is where we are going to store all methods that we want to share with the instance.  Check out the following code, and let's meet below to discuss...&lt;/p&gt;

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

&lt;p&gt;As you can see by the comment lines above, there is another feature that we need to consider with this style.  It uses the 'new' keyword.  The 'new' keyword is used when creating the object and it basically works to replace the Object.create() and return lines of code seen in the prototypal style.&lt;/p&gt;

&lt;p&gt;5) ES6 - ES6 introduces classes as a new instantiation pattern.  Syntactically, class is not a function or an object - it is something new to ES6.  Look through the following code, and let's take a moment to reflect...&lt;/p&gt;

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

&lt;p&gt;While the syntax is new, this constructor function handles creation of objects about the same as the pseudoclassical style.  Notice also the syntax to include methods on this class.  Although it may not be intuitive, the new Class construct is going to save that method to the prototype object of the constructor.&lt;/p&gt;

&lt;p&gt;While you can use any of these instantiation styles, the latter styles require less copying of methods which becomes important as systems scale.  So, learn the ES6 and use it to impress potential employers.&lt;/p&gt;

&lt;p&gt;Special thanks to the youtube channel "hola〈code/〉" for helping me to understand these concepts.  Here are helpful links for followup research:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.youtube.com/watch?v=zd2STahSBaA&amp;amp;t=314s"&gt;https://www.youtube.com/watch?v=zd2STahSBaA&amp;amp;t=314s&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=T-HGdc8L-7w&amp;amp;t=258s"&gt;https://www.youtube.com/watch?v=T-HGdc8L-7w&amp;amp;t=258s&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.youtube.com/watch?v=FrSW-dSJTyg"&gt;https://www.youtube.com/watch?v=FrSW-dSJTyg&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
