JSFiddle - React, Tailwind, and code Playground

JavaScript

const maxProfit = prices => {
	let left = 0;
  let right = 1;
  let maxProfit = 0;
  
  while (right < prices.length) {
  	if (prices[left] < prices[right]) {
    	const profit = prices[right] - prices[left];
    	maxProfit = Math.max(maxProfit, profit);
    }
    else {
    	left = right;
    }
    
  	right++;
  }
  
  return maxProfit;
}

console.log(maxProfit([10,1,5,6,7,1]));
console.log(maxProfit([10,8,7,5,2]));