title: Compare two sentences word by word and return the number of word matches with some conditions
body: <p>Here is a piece of code to compare two sentences word by word and return the number of word matches with some conditions:</p>
  
  <p><strong>hint:</strong> the word in the first sentence ::::  the word in the second sentence</p>
  
  <p>1)   <strong>protecting</strong>  ::::  <strong>i</strong>      should result <strong>Not matched</strong></p>
  
  <p>2)   <strong>protecting</strong>  ::::  <strong>protect</strong>      should result <strong>matched</strong></p>
  
  <p>3)  <strong>protect</strong>   ::::  <strong>protecting</strong>  should result <strong>matched</strong></p>
  
  <p>4)  <strong>him</strong>   ::::  <strong>i</strong>  should result <strong>Not matched</strong></p>
  
  <p>5)  <strong>i</strong>   ::::  <strong>i</strong>  should result <strong>matched</strong> but only once not twice: (let me explain this)</p>
  
  <p>We have this string as the <strong>first sentence</strong>:</p>
  
  <pre><code> let speechResult = "they're were protecting him i knew that i was aware";
  </code></pre>
  
  <p>It has two <strong>i</strong> as you see but there is only one <strong>i</strong> in the <strong>second sentence</strong> here:</p>
  
  <pre><code>let expectSt = ['i was sent to earth to protect you'];
  </code></pre>
  
  <p>So we should consider this match as one occurrence not two, If we had two <strong>i</strong> occurrences in the second sentence too, then we would consider the <strong>i</strong> matches as two occurrences.</p>
  
  <p>6)  <strong>was</strong>   ::::  <strong>was</strong>  should result <strong>matched</strong></p>
  
  <p>Here is my code so far:</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>// Sentences we should compare word by word
  let speechResult = "they're were protecting him i knew that i was aware";
  let expectSt = ['i was sent to earth to protect you'];
      
  // Create arrays of words from above sentences
  let speechResultWords = speechResult.split(/\s+/);
  let expectStWords = expectSt[0].split(/\s+/);
  
  // Here you are..    
  //console.log(speechResultWords)
  //console.log(expectStWords)
          
  // Count Matches between two sentences
  function includeWords(){
  // Declare a variable to hold the count number of matches    
  let countMatches = 0;    
  for(let a = 0; a &lt; speechResultWords.length; a++){
          
      for(let b = 0; b &lt; expectStWords.length; b++){
            
          if(speechResultWords[a].includes(expectStWords[b])){
             console.log(speechResultWords[a] + ' includes in ' + expectStWords[b]);
             countMatches++
          }
                
      }  // End of first for loop  
      
  } // End of second for loop
      
  return countMatches;
  };
      
  // Finally initiate the function to count the matches
  let matches = includeWords();
  console.log('Matched words: ' + matches);
       </code></pre>
  </div>
  </div>
  </p>