title: Javascript - updating array values by adding the previous value
body: <p>I have an array like</p>
  
  <p><code>arr=[20, 40, 40, 40]</code> and I want to update it to<code>arr=[20, 60, 100, 140]</code></p>
  
  <p>I don't want to update the first value but the following values should be that value plus the value before it in the array</p>
  
  <p>I sort of have it working here but it has to add the previous value after its updated from the previous loop and I'm having to add the first value separately at the end.</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>let arr = [20, 40, 40, 40]
  
  let arrUpdate = []
  
  for(let n=0; n&lt;arr.length; n++){
    arrUpdate.push(arr[n] + (arr[n-1]))
  }
  
  arrUpdate[0] = arr[0]
  
  console.log(arrUpdate)</code></pre>
  </div>
  </div>
  </p>