title: how to get common substring in two string to maintain order?
body: <p>I am trying to do one problem of <code>hackerrank</code> but I am not able to solve that problem </p>
  
  <p>Can someone please help me with wrong logic implementation done by me?</p>
  
  <p>problem </p>
  
  <p><strong>Print the length of the longest string, such that  is a child of both  s1 and s2.</strong></p>
  
  <p>Sample Input</p>
  
  <pre><code>HARRY
  SALLY
  </code></pre>
  
  <p>Sample Output</p>
  
  <pre><code> 2
  </code></pre>
  
  <p>Explanation</p>
  
  <p>The longest string that can be formed by deleting zero or more characters from  HARRY and SALLY is AY, whose length is 2.</p>
  
  <p>Sample Input 1</p>
  
  <pre><code>AA
  BB
  </code></pre>
  
  <p>Sample Output 1</p>
  
  <pre><code>0
  </code></pre>
  
  <p>Explanation 1</p>
  
  <p>AA and BB have no characters in common and hence the output is 0</p>
  
  <p>Sample Input 2</p>
  
  <pre><code>SHINCHAN
  NOHARAAA
  </code></pre>
  
  <p>Sample Output 2</p>
  
  <pre><code>3
  </code></pre>
  
  <p>Explanation 2</p>
  
  <p>The longest string that can be formed between  SHINCHAN and NOHARAAA  while maintaining the order is NHA.</p>
  
  <p>I have written some logic which is as follows:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code> 
  
     function commonChild(s1, s2) {
  
      var arr = s2.split(),
          currenString = '',
          maxLength = 0,
          index = -1;
    console.log(arr);
  
      for (var i = 0; i &lt; s1.length; i++) {
          var char = s1.charAt(i),
              charIndex = arr.indexOf(char);
          console.log(char)
          if (index &lt; charIndex) {
              index = charIndex;
              currenString +=char;
          }
  
          maxLength= Math.max(maxLength,currenString.length)
      }
  
      return maxLength;
  
  }
  
  
  commonChild('ABCDEF', 'FBDAMN');
  
  
  console.log(commonChild('ABCDEF', 'FBDAMN'));</code></pre>
  </div>
  </div>
  </p>