title: How can I compare adjecent string elements in an array - javascript?
body: <p>Given an array of strings, I'd like to check for opposite directions and cancel them out (remove them from the array) and return a new array.</p>
  <p>For example:</p>
  <pre><code>let arr = [&quot;N&quot;, &quot;E&quot;, &quot;W&quot;, &quot;S&quot;, &quot;W&quot;, &quot;S&quot;, &quot;N&quot;, &quot;W&quot;]
  </code></pre>
  <p>should reduce to:</p>
  <pre><code>newArr = [&quot;W&quot;, &quot;W&quot;]
  </code></pre>
  <p>Here is what I have come up with so far. I am getting <code>undefined</code> and I am not sure why.</p>
  <pre><code>let arr = [&quot;N&quot;, &quot;E&quot;, &quot;W&quot;, &quot;S&quot;, &quot;W&quot;, &quot;S&quot;, &quot;N&quot;, &quot;W&quot;]
  
  function checkForOpposites(arr) {
    let nOpp = [&quot;N&quot;, &quot;S&quot;]
    let sOpp = [&quot;S&quot;, &quot;N&quot;]
    let eOpp = [&quot;E&quot;, &quot;W&quot;]
    let wOpp = [&quot;W&quot;, &quot;E&quot;]
    for (var i = 0; i &lt; arr.length; i++) {
      if (arr[i] + arr[i + 1] === nOpp) 
      if (arr[i] + arr[i + 1] === sOpp)
      if (arr[i] + arr[i + 1] === eOpp)
      if (arr[i] + arr[i + 1] === wOpp) {
        return true
      }
    }
  }
  
  function newDirections(arr) {
    arr.filter(checkForOpposites)
  }
  </code></pre>