<script> var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99]; var total = 0; oilPrices.forEach(function (value) { total += value }); console.log(total); </script>
You should get the total of 362.19 as your total when you add up the last five days of oil prices.
If you want to pass in all three values you can have a function that can manipulate the actual value itself like the code below. Let's say you buy your oil in large quantity so you get a $1 discount for each barrel of oil. The following forEach() method will show you the discounted oil prices.
<script> var oilPrices = [70.15, 69.50, 71.23, 74.32, 76.99]; var total = 0; oilPrices.forEach(function (value) { total += value }); console.log(total); oilPrices.forEach(function (value, index, prices) { prices[index] = value - 1; }); console.log(oilPrices); </script>
As you can you got yourself a discount
No comments:
Post a Comment