<?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: Indrajeet Mandloi 🧑🏻‍💻</title>
    <description>The latest articles on DEV Community by Indrajeet Mandloi 🧑🏻‍💻 (@indrajeetmand).</description>
    <link>https://dev.to/indrajeetmand</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%2F477049%2Fffea53e4-c38d-437a-84c7-7ec36e3e7b8a.jpg</url>
      <title>DEV Community: Indrajeet Mandloi 🧑🏻‍💻</title>
      <link>https://dev.to/indrajeetmand</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/indrajeetmand"/>
    <language>en</language>
    <item>
      <title>Recursion</title>
      <dc:creator>Indrajeet Mandloi 🧑🏻‍💻</dc:creator>
      <pubDate>Tue, 29 Sep 2020 04:06:15 +0000</pubDate>
      <link>https://dev.to/indrajeetmand/recursion-24di</link>
      <guid>https://dev.to/indrajeetmand/recursion-24di</guid>
      <description>&lt;p&gt;Most of the time people struggle working with recursive functions.  So here is the trick - &lt;/p&gt;

&lt;p&gt;A recursive function should always have at least 2 things - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Condition where function ends.&lt;/li&gt;
&lt;li&gt;Recursion where function calls itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's see some examples - &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's try reversing a string using recursion.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function reverseString(str){
  if (str === '') return '';
  return reverseString(str.substr(1)) + str[0];
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;if (str === '') return '';&lt;/code&gt; this is the condition where function ends/exits. When our string becomes empty it will stop the function.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return reverseString(str.substr(1)) + str[0];&lt;/code&gt; this is Recursion logic where we modify string and call function again with updated string. Let's see the execution of above function for string &lt;code&gt;'dev'&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;reverseString('dev');&lt;/code&gt; this will execute in following steps -  &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;reverseString('ev') + 'd';&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reverseString('v') + 'e';&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;reverseString('') + 'v';&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;if (str === '') return ''&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;final output would be &lt;strong&gt;'ved'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sometimes people get confused with when to use recursion and when to use loops. For the above example, we can also use a loop. And of course we can also use a simple javascript &lt;code&gt;str.reverse()&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;But let's see another example where recursion would be the better choice. &lt;/p&gt;

&lt;p&gt;Suppose we have a complex nested object and suppose you are generating this object dynamically and you don't know the complexity of object in advance. For example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const data = {
  'person' : {
     'name' : {
       'fname' : 'Dwight',
       'lname' : 'Schrute'
     },
     'age' : 38
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now you need to get the value of &lt;code&gt;'person.name.fname'&lt;/code&gt;. Here the path of the key is string and we don't how nested the key would be. So let's create a function which will return a value -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function findValue(obj, path) {
    const keysArr = path.split('.');
    if (keysArr.length === 1) {
      return obj[parts[0]];
    }
    return findValue(obj[keysArr[0]], keysArr.slice(1).join('.'));
  }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;code&gt;findValue(data, 'person.name.fname');&lt;/code&gt; this will execute in following steps -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. const keysArr = path.split('.'); // ['person', 'name', 'fname']
2. findValue(obj['person'], 'name.fname');
3. const keysArr = path.split('.'); // ['name', 'fname']
4. findValue(obj['name'], 'fname');
5. const keysArr = path.split('.'); // ['fname']
6. if (keysArr.length === 1) {
      return obj[parts[0]];
    }  // return obj[fname]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;final output would be &lt;strong&gt;'Dwight'&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have any questions or suggestions please let me know. Thank you for reading.&lt;/p&gt;

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