Skip to content

Instantly share code, notes, and snippets.

@rgtalbot
Created August 18, 2016 23:36
Show Gist options
  • Save rgtalbot/d652755050cfa50ec33e777d239260ba to your computer and use it in GitHub Desktop.
Save rgtalbot/d652755050cfa50ec33e777d239260ba to your computer and use it in GitHub Desktop.
/**
* Buy Low, Sell High -- Starter Code
**/
// Stock Prices
iagStockPrices = [1.32, 1.14, 1.45, 1.20, 1.34, 1.74, 1.18, 1.90, 1.1];
// Your Biggest Profit function
var biggestProfit = function (stockArray, sharesBought) {
var min = stockArray[0];
var max = stockArray[1] - stockArray[0];
var maxprice = stockArray[1]
for (var i = 1; i < stockArray.length; i++) {
var current = stockArray[i];
var potential = current - min;
if (max < potential) {
max = potential;
maxprice = current;
}
if (min > current) {
if (i < stockArray.length-1) {
min = current;
}
}
}
console.log(max * 10000);
console.log(min);
console.log(maxprice);
};
// A Call to your Biggest Profit function.
biggestProfit(iagStockPrices, 10000);
// NOTE: This should return 7600,
// because you could have bought it at 1.14 per share
// and then sold it at 1.90 per share.
// 1.90 - 1.14 = 0.76. 0.76 * 10000 is 7600.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment