<?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: Eric Weissman</title>
    <description>The latest articles on DEV Community by Eric Weissman (@ericweissman).</description>
    <link>https://dev.to/ericweissman</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%2F354479%2F335cd467-f5f3-4401-a833-0cef80bfe0d0.jpeg</url>
      <title>DEV Community: Eric Weissman</title>
      <link>https://dev.to/ericweissman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ericweissman"/>
    <language>en</language>
    <item>
      <title>Exploring a New Codebase</title>
      <dc:creator>Eric Weissman</dc:creator>
      <pubDate>Sun, 02 Aug 2020 20:02:01 +0000</pubDate>
      <link>https://dev.to/ericweissman/diving-into-a-new-codebase-4b38</link>
      <guid>https://dev.to/ericweissman/diving-into-a-new-codebase-4b38</guid>
      <description>&lt;p&gt;Congratulations! It's your first day at your brand, spankin' new developer job! You've signed all the paperwork, set up your desk and have already pillaged the free snacks. You're feeling all sorts of happy... until you spin up the codebase and you see what you will be "working on" for the foreseeable future.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tjHyKjG0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/7fRdMq1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tjHyKjG0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://i.imgur.com/7fRdMq1.gif" alt="Opening up the codebase for the first time"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Directories. Sub-directories. READMEs. Testing Suites. And files.... hundreds and hundreds of files! The panic is REAL. &lt;/p&gt;

&lt;p&gt;But don't fret. This phenomenon happens to EVERY developer - from the seasoned Senior Engineer all the way to the newer Junior Developer. Regardless of where you fall in your career trajectory, this post will help give you some strategies to diving into a new codebase so you can ramp up quicker, feel less stressed and start writing great code!&lt;/p&gt;

&lt;h2&gt;
  
  
  Cut Scope
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/k9yS4LbpiVmtG/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/k9yS4LbpiVmtG/giphy.gif" alt="Silly animation about cutting scope"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Look, there is no way that you will be able to understand how every single piece of the codebase is working until you've been working in that codebase for quite some time. &lt;/p&gt;

&lt;p&gt;So, rather than wrack your brain about how you will come to understand it all, take a deep breath and cut scope. &lt;/p&gt;

&lt;p&gt;A good question to ask yourself is "What part of the application do I need to understand FIRST?" There is no exact science to figuring this out, but it can be a good first step into diving into the codebase and the application itself. While there are many entry points into the codebase, picking one and going from there will help make things less overwhelming! &lt;/p&gt;

&lt;h2&gt;
  
  
  To The Tests
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/12vJgj7zMN3jPy/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/12vJgj7zMN3jPy/giphy.gif" alt="Silly testing related animation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OK, so you've decided on a part of the app that you want to focus on. But even that pathway still presents an overwhelming number of potential files and dataflows to parse through. Rather than absently click through every single "potential" file, it is better to run the tests associated with the pathway you chose. &lt;/p&gt;

&lt;p&gt;Besides ensuring that the code is stable and ready for production, the tests should hopefully provide some documentation on &lt;strong&gt;how&lt;/strong&gt; the code is working and what it does! This approach can give you some insight into commonly used conventions in the codebase, how functions are being used/reused, etc.&lt;/p&gt;

&lt;p&gt;But as we all know, there will be times when test coverage is lacking or non-existent. So, where do we go from here?&lt;/p&gt;

&lt;h2&gt;
  
  
  Experiment with the App
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/ule4vhcY1xEKQ/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/ule4vhcY1xEKQ/giphy.gif" alt="Silly animation about trying thing out"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Rather than just look at file after file, a good way to start understanding the app is to actually engage with it! All software has an end user - going through a portion of the application from that users perspective will give you valuable insight into, including...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How the app works&lt;/li&gt;
&lt;li&gt;What the user experience involves&lt;/li&gt;
&lt;li&gt;Potential pain points a user would experience&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To really enhance your understanding through this process, it is important to utilize any relevant development tools as you work through the application. For example, if your application uses a framework such as React, you can utilize the React Dev Tools to determine component structure, what data is being passed through as props, etc. as you interact with the app.&lt;/p&gt;

&lt;p&gt;Additionally, feel free to experiment with the code itself and seeing what impact it has on the experience! Strategies such as using debuggers, commenting out code, or adding small snippets of your own code will allow you to see how certain parts of the app are working as you go through the user flow!&lt;/p&gt;

&lt;h2&gt;
  
  
  Follow The Data
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/l0IylOPCNkiqOgMyA/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/l0IylOPCNkiqOgMyA/giphy.gif" alt="Silly animation about following data flow"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Production-level codebases incorporate a frontend and backend codebase that communicate together to send, create, update, and display data. It is crucial to understand how these two systems communicate together to truly understand a codebase. Luckily, there are a lot of ways you can begin to do this!&lt;/p&gt;

&lt;p&gt;For example, many applications follow a MVC architecture pattern - whereby there will be a model, view and controller - each responsible for handling certain functionality within the app. You can analyze the file structure within your text editor/integrated development environment (IDE) to get a sense of where these respective files are stored and how they are organized. You can then "follow" the flow of data from models to controllers to views. This scavenger hunt works even better when you are able to utilize your developer tools as mentioned in the above step to see if any of the data/function calls can lead you to explore other related files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Use Your Tools
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/kaB4PGD4kge6mOc0jk/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/kaB4PGD4kge6mOc0jk/giphy.gif" alt="Silly animation about using tools"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you start to move more into the code itself, it is important to leverage the tools baked into your text editor/IDE that make it easier to scan and search within a large project or codebase.&lt;/p&gt;

&lt;p&gt;Let's say you've identified a file that is related to the functionality you are currently exploring. You can search for any imported methods, data, etc. using the search functionality within your editor. Often these are set up to search within the file you have open, or throughout the project as a whole.&lt;/p&gt;

&lt;p&gt;This process of analyzing a file and finding functionality coming from another file can start to help you piece together how different parts of the app communicate with one another!&lt;/p&gt;

&lt;h2&gt;
  
  
  Take Notes
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/8vOF5hcAuSa6BkBWFD/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/8vOF5hcAuSa6BkBWFD/giphy.gif" alt="Silly animation about taking notes"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you start to dive deeper and deeper into the codebase, it can be extremely helpful to document what you've learned so far and keep tabs on what you've already explored.&lt;/p&gt;

&lt;p&gt;Code annotations can be a great way for you to leave yourself notes about how certain functionality works, how certain files connect, or even ideas you had as you worked through the app from the perspective as a user! I personally like to use code comments to jot down my thoughts and notes, typically working on a separate branch where I can add these without worrying about confusing other developers on my team.&lt;/p&gt;

&lt;p&gt;Also, as you begin to see how different parts of the application work together, it can be extremely useful to diagram how data flows from one part of the app to another. There are plenty of tools that you can use for this - from good ol' pencil and paper to digital tools such as &lt;a href="https://www.figma.com/"&gt;Figma&lt;/a&gt; or &lt;a href="https://miro.com/"&gt;Miro&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;You may also want to consider creating an Entity Relation Diagram (ERD). These can be used to to visulaize database tables and their relationships. This tool can be really useful for understanding the data you are working with within an app. One tool that I've found useful is &lt;a href="http://dbdiagram.io/"&gt;DB Diagram&lt;/a&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Share Your Knowledge
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/3o6ZtfKwnx8bWjDAgo/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/3o6ZtfKwnx8bWjDAgo/giphy.gif" alt="Silly animation about sharing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you take steps to dive into a codebase and learn about its inner workings, it is always a good idea to share what you've learned to your larger team! There may be other new developers on your team who could benefit from your research and diagrams. Also, you may have some more senior engineers on your team that can provide valuable feedback on your notes, diagrams and understandings. Remember, while it may take a bit of time for you ramp-up to the point where you can enhance the codebase, this early research can be incredibly valuable for the entire team. &lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/oBPOP48aQpIxq/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/oBPOP48aQpIxq/giphy.gif" alt="Silly motivational animation"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Learning the intricacies of an unfamiliar codebase is a very difficult and time-consuming endeavor. There will inevitably moments where you get lost in the code, misunderstand how data flows in the app, or encounter code that you don't understand at all. Rather than get frustrated or upset, recognize that this is all part of the process for truly learning something new!&lt;/p&gt;

&lt;p&gt;While this process will always be a little prickly and tedious, utilizing some of the strategies above should help you to refine your process for breaking down a large codebase. If you have other strategies that have worked well in the past, please add them in the comments below!&lt;/p&gt;

&lt;p&gt;Don't have that first job yet but still want to get your feet wet with this process? Luckily, the &lt;a href="https://github.com/forem/forem"&gt;Forem platform&lt;/a&gt; (which powers DEV) is completely open-source and provides a good sandbox for you to start putting these strategies into practice! &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Recursion in JS for New Developers</title>
      <dc:creator>Eric Weissman</dc:creator>
      <pubDate>Sun, 02 Aug 2020 16:50:16 +0000</pubDate>
      <link>https://dev.to/ericweissman/recursion-in-js-for-new-developers-3ncd</link>
      <guid>https://dev.to/ericweissman/recursion-in-js-for-new-developers-3ncd</guid>
      <description>&lt;p&gt;For many new developers, recursion is one of the most misunderstood concepts in JavaScript. Unfortunately, this misunderstanding manifests itself in many different ways, generally falling somewhere between total indifference to abject terror. &lt;/p&gt;

&lt;p&gt;Today I hope to demystify the concept of recursion and work through a few examples that DON'T involve the tricky math concepts you likely forgot from your "glory days" of high school. &lt;/p&gt;

&lt;h2&gt;
  
  
  So... what is recursion again?
&lt;/h2&gt;

&lt;p&gt;Simply put, recursion is a programming technique where a function &lt;strong&gt;CALLS ITSELF&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/aZ3LDBs1ExsE8/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/aZ3LDBs1ExsE8/giphy.gif" alt="Recursion WTF"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Yup. Mind-bending, I know. &lt;/p&gt;

&lt;p&gt;But let's break it down a little more to hopefully clear things up.&lt;/p&gt;

&lt;p&gt;More specifically, a recursive function is a function that calls itself &lt;em&gt;until&lt;/em&gt; it arrives at a final result.&lt;/p&gt;

&lt;p&gt;Well, how do we know when we've arrived at a final result? Great question. Before we can get into that, we need to first understand what situations recursion might be useful for!&lt;/p&gt;

&lt;h3&gt;
  
  
  When You Might Use Recursion
&lt;/h3&gt;

&lt;p&gt;Recursion is great for when we want to perform the same action over and over. The only that will change when we repeat the action will be the data involved.&lt;/p&gt;

&lt;p&gt;Sound familiar? That's because many of the techniques we use for iteration, such as &lt;code&gt;for loops&lt;/code&gt;, &lt;code&gt;while loops&lt;/code&gt;, etc. do a very similar thing! So remember, while we can often use recursion in place of these foundational tools, we usually don't NEED to use recursion. &lt;/p&gt;

&lt;p&gt;One pitfall I see many of my students encounter is as soon as they learn recursion they try implementing it &lt;strong&gt;EVERYWHERE&lt;/strong&gt;, even in settings where iteration may be easier to read/understand from a developer empathy perspective!&lt;/p&gt;

&lt;p&gt;There are definitely situations where recursion is a better choice than relying solely on iteration - but remember, there are multiple ways to do the same thing in programming! &lt;/p&gt;

&lt;h2&gt;
  
  
  How to Build a Recursive Function
&lt;/h2&gt;

&lt;p&gt;While recursion may be a very intimidating concept, the actual construction of a recursive function is fairly straightforward. For this breakdown, we will use the following scenario to start building our recursive function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Create a function that takes in an array of numbers &lt;/span&gt;
&lt;span class="c1"&gt;// and adds the numbers together&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;superCoolNumbers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;getSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;superCoolNumbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Part I - Creating a Base Case
&lt;/h3&gt;

&lt;p&gt;Our base case is our condition that we will write that tells our recursive function to STOP calling itself over and over again. Think of it like an big stop button or an emergency break. &lt;/p&gt;

&lt;p&gt;One thing I've learned in my time programming is that computers aren't super smart - we have to give them explicit instructions on what we want them to do. Recursion is no exception. We have to create a base case to tell our function when to stop executing! &lt;/p&gt;

&lt;p&gt;If we don't, we run the risk of recursively calling the function FOREVER. You'll know you've entered this zone if you get an error that says something like &lt;code&gt;RangeError: Maximum call stack size exceeded&lt;/code&gt;. For the purposes of this post, we will not cover the nitty gritty of the JavaScript call stack, but we will talk about how it works in relation to some of our later examples.&lt;/p&gt;

&lt;p&gt;OK, so back to our &lt;code&gt;getSum&lt;/code&gt; problem. In simple terms, when do we want the function to STOP? Well, when there aren't any numbers left to add together, that seems like a pretty good time to stop!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create a function that takes in an array of numbers&lt;/span&gt;
    &lt;span class="c1"&gt;//create a base case&lt;/span&gt;
        &lt;span class="c1"&gt;//when there are no more numbers, stop executing&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Great! Now we have some steps we can follow to write out our actual code! It may look something like this...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//base case&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;numbers&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;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So, why did I choose to return &lt;code&gt;0&lt;/code&gt;? Well, let's remember what we are trying to do with this function. If we are adding numbers together to get a sum, then adding zero won't affect the sum and will allow us to stop execution by using the &lt;code&gt;return&lt;/code&gt; statement!&lt;/p&gt;

&lt;h3&gt;
  
  
  Part II - Creating the Recursive Case
&lt;/h3&gt;

&lt;p&gt;Alright campers, buckle up. This is where things often get a little wild.&lt;/p&gt;

&lt;p&gt;With our recursive case, all we want to do is come up with a set of repeatable instructions that move us closer to our base case. &lt;em&gt;The one caveat is that this part needs to include calling the function we are currently writing&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Let that settle in for a second... Great. Now that we've addressed it, let's focus on making it seem a little less wonky. &lt;/p&gt;

&lt;p&gt;So if we look back at our base case, we are trying to get to a place where we no longer have any numbers to use for our recursive case. Sounds like we need to do some manipulating of the numbers array that we are feeding this function.&lt;/p&gt;

&lt;p&gt;Also, we want to keep our eye on the prize - what are we trying to do? Add numbers! OK, what is easier... &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Adding two numbers together?&lt;/li&gt;
&lt;li&gt;Adding more than two numbers together?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is an important concept of recursion. Being able to break the problem down into the smallest, simplest form will often allow you to write more simple, repeatable steps that make recursion an excellent tool for the job!&lt;/p&gt;

&lt;p&gt;So, if all our function does is remove a number and add that number to another number, we can start to break this down recursively!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//create a function that takes in an array of numbers&lt;/span&gt;
    &lt;span class="c1"&gt;//create a base case&lt;/span&gt;
        &lt;span class="c1"&gt;//when there are no more numbers, stop executing&lt;/span&gt;
    &lt;span class="c1"&gt;//create recursive case&lt;/span&gt;
        &lt;span class="c1"&gt;//take out the first number and store in variable&lt;/span&gt;
        &lt;span class="c1"&gt;//add that variable to the result of calling the function recursively with the remaining numbers&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Essentially what our recursive case will do is remove one of the numbers and add it to the result of the next call. &lt;/p&gt;

&lt;p&gt;But what is the result of the next call? &lt;/p&gt;

&lt;p&gt;Well, simply put, it will be the next number that we remove! All this recursive function will do is remove a number and add it to the next number until we have no more numbers to add. It might look a little something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;getSum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//base case&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;numbers&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="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;firstNum&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;shift&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;firstNum&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;getSum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;numbers&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;Whoa. That might seem like a big step, but let's break it down how it's working step by step. &lt;/p&gt;

&lt;p&gt;One thing to be aware of is that each time we make a recursive call, it gets added to the call stack. Think of the call stack like a Pringles can - the first chip that goes in is the last chip that is taken out. So in our example, the first call that is added to the stack is the last one that will be executed. &lt;/p&gt;

&lt;p&gt;If this part feels a little fuzzy, that's OK! The JavaScript call stack is a really tricky concept, but there are ton of great resources out there to help understand it better, including &lt;a href="https://www.youtube.com/watch?v=8aGhZQkoFbQ"&gt;this awesome video&lt;/a&gt;. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;When we first call the function, we are removing the number &lt;code&gt;1&lt;/code&gt; and adding it to the recursive function call with our remaining numbers, like so:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//1st Call&lt;/span&gt;
    &lt;span class="c1"&gt;// 1 + getSum([2, 3, 4])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;We still have not hit our base case, so we continue our execution by removing the first number, in this case &lt;code&gt;2&lt;/code&gt;, and adding that to the result of our upcoming recursive call, like so:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//1st call&lt;/span&gt;
    &lt;span class="c1"&gt;//1 + getSum([2, 3, 4])&lt;/span&gt;
    &lt;span class="c1"&gt;//2nd call&lt;/span&gt;
        &lt;span class="c1"&gt;// 2 + getSum([3, 4])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;This will repeat until we have no numbers left and we hit our base case. This will look like:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//1st call&lt;/span&gt;
    &lt;span class="c1"&gt;//1 + getSum([2, 3, 4])&lt;/span&gt;
    &lt;span class="c1"&gt;//2nd call&lt;/span&gt;
        &lt;span class="c1"&gt;// 1 + 2 + getSum([3, 4])&lt;/span&gt;
        &lt;span class="c1"&gt;//3rd call&lt;/span&gt;
            &lt;span class="c1"&gt;//1+ 2 + 3 + getSum([4])&lt;/span&gt;
            &lt;span class="c1"&gt;//4th call&lt;/span&gt;
                &lt;span class="c1"&gt;//1 + 2 + 3 + 4 + getSum([])  &amp;lt;- triggers our base case!&lt;/span&gt;
                &lt;span class="c1"&gt;//5th call (triggers base case!)&lt;/span&gt;
                    &lt;span class="c1"&gt;//1 + 2 + 3 + 4 + 0&lt;/span&gt;

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



&lt;ol&gt;
&lt;li&gt;Now, the call stack will resolve the same way we would eat chips from a Pringles can - pulling the top layer off and working our way one level at a time until we get to the bottom! So this would look something like this...
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;9&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;10&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Congrats! We have written our first recursive function!&lt;/p&gt;

&lt;h2&gt;
  
  
  Recursion Example without Math!
&lt;/h2&gt;

&lt;p&gt;If you're like me, I imagine you've been doing quite a bit of Googlin' to start building your understanding of recursion. One frustration I encountered was most of the example problems dealt with mathematical concepts like the Collatz conjecture, the Fibonacci sequence, etc. Unfortunately, these problems provided a barrier to entry of sorts for me because I had a hard time teasing out the &lt;code&gt;WHAT&lt;/code&gt; I was trying to do while also learning recursion. So, let's try a non-math problem that we can use recursion to solve!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight markdown"&gt;&lt;code&gt;Write a function called &lt;span class="sb"&gt;`isPalindrome`&lt;/span&gt; that takes in a string. 
Using recursion, determine if the string is a palindrome - a word that reads the same forwards and backwards. A few conditions to be aware of...
&lt;span class="p"&gt;-&lt;/span&gt; An empty string can be considered a palindrome
&lt;span class="p"&gt;-&lt;/span&gt; A single character can be considered a palindrome
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;OK - so remember, for any recursive function we need:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;A base case&lt;/li&gt;
&lt;li&gt;A recursive case&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We need to figure out how we can start to determine if the string is a palindrome. To accomplish this recursively, it's best to try to break this problem down into small, repeatable steps. &lt;/p&gt;

&lt;p&gt;When I think about this problem, my approach would be to compare the first and last letters of the string to determine if they are the same. If they are, we can move inwards from the front and back and compare those letters to determine if they are the same. If we do that all the way through with matching letters, that means we have a palindrome. &lt;/p&gt;

&lt;p&gt;But if anywhere along the way they are NOT equal, that means we cannot possibly have a palindrome.&lt;/p&gt;

&lt;p&gt;Alright, now what about the recursive case. Well thankfully, this problem gives us some big hints that can lead us to the base case. If our string is either empty (no letters) or is one character, that means we have a palindrome. So we can wait until we get to zero or one character remaining and kick out of our recursive function!&lt;/p&gt;

&lt;p&gt;Before we dive into the actual syntax, let's capture our approach in some psuedocode so we have a strong plan of attack.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//isPalindrome(string)&lt;/span&gt;
    &lt;span class="c1"&gt;//base case&lt;/span&gt;
        &lt;span class="c1"&gt;//if the string is either one letter OR an empty string&lt;/span&gt;
            &lt;span class="c1"&gt;// return true&lt;/span&gt;
    &lt;span class="c1"&gt;//recursive case&lt;/span&gt;
        &lt;span class="c1"&gt;// grab first letter&lt;/span&gt;
        &lt;span class="c1"&gt;// grab last letter&lt;/span&gt;
        &lt;span class="c1"&gt;// if the two letters are the same&lt;/span&gt;
            &lt;span class="c1"&gt;//return isPalindrome with the remaining letters&lt;/span&gt;
        &lt;span class="c1"&gt;//otherwise, return false (can't be a palindrome)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Part I  - Base Case
&lt;/h3&gt;

&lt;p&gt;Based on our psuedocode, this should be fairly easy to translate into actual syntax.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//base case&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;string&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;&amp;lt;=&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="kc"&gt;true&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;
  
  
  Part II - Recursive Case
&lt;/h3&gt;

&lt;p&gt;There are a few more moving parts in our recursive case compared to our base case. We need to figure out how to do several things...&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to capture the first letter from a string&lt;/li&gt;
&lt;li&gt;How to capture the last letter from a string&lt;/li&gt;
&lt;li&gt;How to capture the "remaining" letters from the string we are manipulating.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Time to hit the ol' Googleator! After about 5-10 minutes of reading the documentation, I've found a few tools that can work for our given psuedocode.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I can use the index position of &lt;code&gt;[0]&lt;/code&gt; to capture the first letter of the string&lt;/li&gt;
&lt;li&gt;I can use the index position of &lt;code&gt;[string.length - 1]&lt;/code&gt; to capture the last letter of the string&lt;/li&gt;
&lt;li&gt;I can use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring"&gt;substring&lt;/a&gt; method to capture the "remaining" letters of the string after comparing the first and last letters from steps 1 and 2. Specifically, I will need to feed this method the following arguments:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;1&lt;/code&gt; - the index I want to start on (since we captured the first letter with &lt;code&gt;[0]&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;substring.length - 1&lt;/code&gt; this will capture the remainder of the letters remaining in the string&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we have all the necessary tools at our disposal to implement our recursive case!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isPalindrome&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;//base case&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;string&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;&amp;lt;=&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="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;//recursive case&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;string&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;remaining&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;substring&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;string&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="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;first&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;last&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;isPalindrome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;remaining&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;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To prove this works, let's run my favorite palindrome through our brand, spankin' new recursive function... &lt;code&gt;TACOCAT&lt;/code&gt;!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vgWx1mnf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/564x/00/5f/83/005f83fbfebe966796a5972df0a39391.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vgWx1mnf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.pinimg.com/564x/00/5f/83/005f83fbfebe966796a5972df0a39391.jpg" alt="Tacocat"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And voilà! It works! Hopefully, this article allowed you to start understanding recursion just a &lt;em&gt;little&lt;/em&gt; bit better. &lt;/p&gt;

&lt;p&gt;However, recursion is definitely a tricky topic and will take a lot of practice to really feel comfortable with - so keep at it and you'll be a &lt;code&gt;Recursion Rockstar&lt;/code&gt; before you know it!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>recursion</category>
    </item>
  </channel>
</rss>
