title: JS: calculate savings based on last element of a list
body: <p>I've list of prices:</p>
  
  <pre><code> prices =  [57, 69, 90, 108, 142, 216, 344, 459, 670, 1134]
  </code></pre>
  
  <p>I need to calculate the savings of the current element based on the last one.</p>
  
  <pre><code>savings = [39, 61, 68, 75, 81, 85, 87, 88, 90] #first element should be empty or dropped, as first element or **prices** doesn't have something to calculate savings on.
  </code></pre>
  
  <p>See image with exact values:</p>
  
  <p><a href="https://i.stack.imgur.com/weqxQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/weqxQ.png" alt="enter image description here"></a></p>
  
  <p><strong>Note</strong>: I can calculate other metrics, like average, etc. But cannot think of a way of using map or something to calculate based on last item on a list.</p>
  
  <pre><code>   var sum = prices.reduce(function (sum, value) {
            return sum + value;
          }, 0);
  
     alert("SUM: " + sum);
  
     var avg = sum / prices.length;
  
     alert("AVG: " + avg);
  
     var diffs = prices.map(function (value) {
              var diff = value - avg;
              return diff;
     });
  
     alert(diffs);
  </code></pre>