How to find the minimum in an array excluding 0?

I have an array with some values, and I want to find the minimum value of that array in order to print out the index that this value has. In this array, one of the values is 0. My guess is that in order to find the index, we must iterate through this array find the minimum, but not the 0, and return the index. Can you help me understand what I am doing wrong with the iterations? I cannot find the minimum number.

This is the array:

[ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]

I am currently stuck here:

  var smallest = 0
    var biggest = 0

    for (let i = 0; i < merged.length; i++) {


        if (merged[i] > biggest && merged[i] != 0) {
            biggest = merged[i];

        } else merged[i] < smallest ? smallest = merged[i] : smallest = merged[i];



        console.log('the biggest is', biggest, 'in the iteration', i)
        console.log('the smallest is', smallest, 'in the iteration', i)
    }


    console.log('min-> : ', smallest, biggest);

That gives this:

the biggest is 10 in the iteration 0
the smallest is 0 in the iteration 0
the biggest is 10 in the iteration 1
**the smallest is 5 in the iteration 1**
the biggest is 10 in the iteration 2
**the smallest is 6 in the iteration 2**
the biggest is 10 in the iteration 3
the smallest is 5.5 in the iteration 3
the biggest is 10 in the iteration 4
the smallest is 3.75 in the iteration 4
the biggest is 10 in the iteration 5
the smallest is 0 in the iteration 5
the biggest is 10 in the iteration 6
the smallest is 4.25 in the iteration 6
the biggest is 10 in the iteration 7
the smallest is 3 in the iteration 7
the biggest is 10 in the iteration 8
the smallest is 5.5 in the iteration 8
the biggest is 10 in the iteration 9
the smallest is 6.75 in the iteration 9
the biggest is 10 in the iteration 10
the smallest is 8 in the iteration 10
the biggest is 10 in the iteration 11
the smallest is 9.25 in the iteration 11
the biggest is 10 in the iteration 12
the smallest is 4 in the iteration 12
the biggest is 15 in the iteration 13
the smallest is 4 in the iteration 13
the biggest is 15 in the iteration 14
the smallest is 4.25 in the iteration 14
the biggest is 15 in the iteration 15
the smallest is 6 in the iteration 15
the biggest is 15 in the iteration 16
the smallest is 6 in the iteration 16
the biggest is 15 in the iteration 17
the smallest is 4.75 in the iteration 17
the biggest is 15 in the iteration 18
the smallest is 3.75 in the iteration 18
min-> :  3.75 15

As you can see above, the smallest should not change from 5 to 6. The minimum should be 3.

Thank you very much.

Question solved, thank you all for your time and help !!

>Solution :

You can first find the min without zero then find the index using the following, this can be changed to also find the maximum index.

const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]
const minIndex = arr.indexOf(Math.min.apply(null, arr.filter(Boolean)))
console.log(minIndex)

Here’s an iterative approach incase it is clearer:

const findMin = (arr) => {
  let min;

  for(let i =0; i < arr.length; i++){
      if(!min && arr[i]!==0) min = arr[i]
      if(arr[i] < min && arr[i]!==0) min = arr[i]
  }
  return arr.indexOf(min)
}
const arr = [ 10, 5, 6, 5.5, 3.75, 0, 4.25, 3, 5.5, 6.75, 8, 9.25, 4, 15, 4.25, 6, 6, 4.75, 3.75 ]
console.log(findMin(arr))

Leave a Reply