This community-built FAQ covers the “Maximize Stock Trading Profit” code challenge in JavaScript. You can find that challenge here, or pick any challenge you like from our list.
Top Discussions on the JavaScript challenge Maximize Stock Trading Profit
There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
You can also find further discussion and get answers to your questions over in Language Help.
Agree with a comment or answer? Like () to up-vote the contribution!
function maxProfitDays(stockPrices) {
let maxDiff = 0;
let days = [];
for (let j in stockPrices) {
for (let i in stockPrices){
if (j != i && j < i) {
if (stockPrices[i]- stockPrices[j] > maxDiff) {
maxDiff = stockPrices[i]- stockPrices[j];
days[0] = parseInt(j, 10);
days[1] = parseInt(i, 10);
}
}
}
}
return days;
}
// Leave this so we can test your code:
module.exports = maxProfitDays;
I brute forced this one. I can optimize using a hash or map for the min and max values and make sure that the min day is before the max.
function maxProfitDays(stockPrices) {
let newA=0;
let newB=1;
for (let a=0;a<stockPrices.length-1;a++) {
for (let b=a+1;b<stockPrices.length;b++) {
if(stockPrices[b]-stockPrices[a]>stockPrices[newB]-stockPrices[newA]){
newA=a;
newB=b;
}
}
}
return [newA,newB];
}
// Leave this so we can test your code:
module.exports = maxProfitDays;
I did this challenge and noticed that all of the solutions here check for absolute differences between stock prices even though the profit is related to the relative stock prices.
Example 1
Price bought: 1
Price sold: 12
Profit: 12x the original investment
Example 2
Price bought: 2
Price sold: 20
Profit: 10x the original investment
So in fact you should use division “/” for comparing the prices to determine the biggest profit.
function maxProfitDays(stockPrices) {
let minValue=stockPrices[0];
let minPosition=0;
let maxValue=0;
let buyDay=0;
let sellDay=0;
for(let i=1;i<stockPrices.length;i++)
{
if(maxValue<stockPrices[i]-minValue){
maxValue=stockPrices[i]-minValue;
buyDay=minPosition;
sellDay=i;
}
if(stockPrices[i]<minValue)
{
minValue=stockPrices[i];
minPosition=i;
}
}
return [buyDay,sellDay];
}
// Leave this so we can test your code:
module.exports = maxProfitDays;
I think you have to take out the console.log stuff when you submit it so that it doesn’t give you an error.
Your stuff passed one test
because the max minus the min may mot give you the maximum profit
in a situation where the max is after the min (meaning the index of the max is more than the index of the min)
so I don’t know whether that strategy could work.