var maxProfit = function(prices) { var minP = prices[0], maxP = 0; prices.forEach(function (value,index,arry) { minP = Math.min(value,minP); maxP = Math.max(maxP,value - minP); }); return maxP; }; console.log(maxProfit([7,1,5,3,6,4]))