so I have this code here and the value i want to receive froma this array [2,1,2,0,1] is 1, i Know how to make it skip 0 but for some reason it accepts 2 as the lowest value, can anyone explain to me why and find a way to deal with it?
let lowestP = 100000000000000000000000000
let highestP = 0
let startWorking = 0
for(let i = 0; i < prices.length;i++){
if(lowestP > prices[i] && prices[i] > 0){
lowestP = prices[i]
}if(highestP<prices[i]){
highestP = prices[i]
}
if(prices.indexOf(lowestP) > prices.indexOf(highestP)){
highestP = 0
continue
}
startWorking = highestP - lowestP
}return startWorking
};
I did some console logging and it seems like the first 2 in [2,1,2,0,1] is the problem, still i didnt find the solution