Created
August 18, 2016 23:38
-
-
Save rgtalbot/a3cd993c63b43d78e5d317c36be6d4d4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} | |
} | |
} | |
document.write("Profit: $" + (max * 10000)); | |
document.write("Buy price: " + min); | |
document.write("Sell price: " + 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