1

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

swr
  • 11
  • 2

4 Answers4

0
let prices = [2, 1, 2, 0, 1];
let lowestP = 999999999999999;

prices.forEach((price, index) =>
  price > 0 && lowestP > price ? (lowestP = prices[index]) : null
);
Tasos Tsournos
  • 314
  • 2
  • 9
0

You should keep the index of the lowest and highest p, prices.indexOf(highestP), for the second 2 returns index of the first 2 in the prices list.

var maxProfit = function (prices)
{
    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]
            index_lowest = i
        }
        if (highestP < prices[i]) {
            highestP = prices[i]
            index_highest = i
        }
        if (index_lowest > index_highest) {
            highestP = 0
            continue
        }
        startWorking = highestP - lowestP
    }
    return startWorking
}
Shima
  • 1
  • 1
0

You should always try to make structure your code so that it is self-documentary.

If you e.g. first want to filter your array so that certain values don't exist then use filter, if you search the minimum or maximum, then use the corresponding functions.

To pass the filtered array to the Math.min function you can use the Spread Syntax (...)

let prices = [2, 1, 2, 0, 1]
prices = prices.filter(value => value > 0);

if (prices.length === 0) {
  console.error('no prices larger then 0')
} else {
  let lowestP = Math.min(...prices);
  console.log(lowestP)
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
-1

Your code is totally fine (what you displayed in your inquiry). In any case, I think you need to show more piece of your code to distinguish issue. How are you handling 0 value in array? there might be some issue. But just for reference attaching piece of code to finding minimum value from array.

   public static int getMaxValue(int[] numbers){
  int maxValue = numbers[0];
  for(int i=1;i < numbers.length;i++){
    if(numbers[i] > maxValue){
      maxValue = numbers[i];
    }
  }
  return maxValue;
}
public static int getMinValue(int[] numbers){
  int minValue = numbers[0];
  for(int i=1;i<numbers.length;i++){
    if(numbers[i] < minValue){
      minValue = numbers[i];
    }
  }
  return minValue;
}
  • I forgot to specify that im writing it in javascript + here is the code `var maxProfit = function(prices) { 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.indexOf(highestP)){ highestP = 0 continue } startWorking = highestP - lowestP }return startWorking };` – swr Jan 15 '22 at 12:29