title: How to write a function to be used in a forEach statement to add all numbers in an array?
body: <p>I have an exercise where I have to write my own forEach function which takes an array and a 'callback' function to add all the elements in the array.
  So far I have this:</p>
  
  <pre><code>function forEachTest(arr, func){
      func(arr) // Simply apply the function to the array
  }
  </code></pre>
  
  <p>However I don't know if this is the correct way a forEach call works.</p>
  
  <p>Secondly, the said function to add the elements will therefore take the array and somehow add each element one by one till it reaches the end of the array and then console.log(result).</p>
  
  <p>I tried using a variable to store the value but this doesn't work, I get NaN as a result.</p>
  
  <pre><code>function addEach(arr) {
      var sum;
      for(i = 0; i &lt; arr.length; i++) {
          sum += arr[i];
      }
      console.log(sum);
  }
  </code></pre>
  
  <p>The Nan happens when I call forEachTest(arr, addEach);</p>
  
  <p>Note that arr is any array containing numbers.</p>