title: JavaScript Longest common subsequence
body: <p>I have an algorithm that must return the result as follow:</p>
  <pre><code>/*
  &quot;ABAZDC&quot;, &quot;BACBAD&quot; =&gt; ABAD
  &quot;AGGTAB&quot;, &quot;GXTXAYB&quot; =&gt; GTAB
  &quot;aaaa&quot;, &quot;aa&quot; =&gt; &quot;aa&quot;
  &quot;&quot;, &quot;...&quot; =&gt; &quot;&quot;
  &quot;ABBA&quot;, &quot;ABCABA&quot; =&gt; &quot;ABBA&quot;
  */
  </code></pre>
  <p>The code that I developed didn't return these results. How can I solve it?</p>
  <pre><code>console.log(solution('ABAZDC', 'BACBAD')) 
  
  function solution(str1, str2) {
    str1 = str1.split('')
    str2 = str2.split('')  
    
    const output = []
   
    for(let i = str1.length -1; i &gt;= 0; i--) {   
      for(let j = str2.length -1; j &gt;= 0; j--) {
        if( str2[j] === str1[i] ) {
          output.push(str2[j]) 
          break
        }      
      } 
      
    }
    
    return output.reverse().join('')
   
  }
  </code></pre>
  <p><strong>NOTE:</strong></p>
  <p>Here's a solution on youtube. But for me, this solution is complicated for those who are not familiar with this problem. I'd like to see a simpler solution for now. It would be a solution that does NOT contain recursive functions or memoization.</p>
  <p><a href="https://www.youtube.com/watch?v=10WnvBk9sZc&amp;feature=youtu.be" rel="nofollow noreferrer">https://www.youtube.com/watch?v=10WnvBk9sZc&amp;feature=youtu.be</a></p>