Best Time to Buy and Sell Stock II LeetCode solution in JavaScript
1/**2* @param {number[]} prices3* @return {number}4*/5var maxProfit = function (prices) {6let lowestNum = prices[0];7let highestNum = prices[0];8let profit = highestNum - lowestNum;910for (var indexI = 1; indexI < prices.length; indexI++) {11if (prices[indexI] < prices[indexI - 1]) {12lowestNum = prices[indexI];13}14if (prices[indexI] > lowestNum) {15lowestNum = prices[indexI - 1];16profit = profit + prices[indexI] - lowestNum;17highestNum = prices[indexI];18}19}2021return profit;22};