title: using toLowerCase to find matches in arrays with a for-loop
body: <p>I have a function that iterates over one array and compares each value to the values of another array to find matches.  </p>
  
  <p>I'm trying to use toLowerCase() to remove grammar discrepancies between the two.</p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>match: function(array1, array2) {
          counter = 0;
          if (array1.length &gt; 0) {
              for (var i = 0; i &lt; array1.length; i++) {
                  for (var j = 0; j &lt; array2.length; j++) {
                      if (array1[i].toLowerCase() === array2[j].toLowerCase()) {
                          counter++;
                      }
                  }
              }
  
              if (array1 === state.forbidden) {
                  return array1.length - counter;
              }
  
              else {
                  return counter;
              }
          }
  
          else {
              return 0;
          }
      }
  </code></pre>
  
  <p>Line 6 is what's relevant for this question.  Match is a method inside the object 'nutrients' and is called nutrients.match(array1, array2);</p>
  
  <p>This seems to work for array1, but not for array 2 because I get the following error message:  </p>
  
  <p><strong>Error Message</strong></p>
  
  <pre><code>Uncaught TypeError: array2[j].toLowerCase is not a function
  </code></pre>
  
  <p>I'm guessing this is due to the <em>types</em> of arrays I'm passing into this function but I can't put my finger on why it doesn't work.  </p>
  
  <p>The function gets called using a for-loop in the following way:</p>
  
  <p><strong>Javascript</strong></p>
  
  <pre><code>loop: function() {
      for (var i = 0; i &lt; state.greens.length; i++) {
          nutrients.match(state.desired, state.greens[i].ingredients)
  },
  </code></pre>
  
  <p>Loop is a method stored within an object called 'total' and is called by 'total.loop()'</p>
  
  <p>So for some reason state.greens[someNumber].ingredients[someNumber].toLowerCase() is not being recognized as a function.</p>
  
  <p>However, if I try and perform this same operation in the console it comes out correctly:</p>
  
  <p><a href="https://i.stack.imgur.com/KKqdr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KKqdr.png" alt="console"></a></p>
  
  <p>So I'm not sure where the error is in the code.</p>