JSFiddle - React, Tailwind, and code Playground

by eitanp461

JavaScript

var stockPricesYesterday = [10, 7, 5, 8, 11, 9, 12];

function getMaxProfit(arr) {
var maxProfit = 0;
for(var i = 0; i < stockPricesYesterday.length; i++){
	var currentPrice = stockPricesYesterday[i];
  for(var j = i+1; j < stockPricesYesterday.length; j++){
  console.log("i" + i)
  console.log("j" + j)
  	if(currentPrice < stockPricesYesterday[j]){
    	var currentProfit = stockPricesYesterday[j] - currentPrice;
      if(currentProfit > maxProfit){
      	maxProfit = currentProfit;
      }
    }
  }
}
	return maxProfit;
}

console.log(getMaxProfit(stockPricesYesterday));
// returns 6 (buying for $5 and selling for $11)