Search code examples
javascriptarraysloopsminimum

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

  • Simply change your code to the following,

    var smallest = 0;
    var biggest = 0;
    var merged = [ 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 ];
    
        for (let i = 0; i < merged.length; i++) {
    
            if (merged[i] > biggest && merged[i] != 0) {
                biggest = merged[i];
            }
    
            if(smallest == 0 || (merged[i] < smallest && merged[i] != 0)) {
                smallest = merged[i];
            }
    
            console.log('the biggest is', biggest, 'in the iteration', i)
            console.log('the smallest is', smallest, 'in the iteration', i)
        }
    

    I assumed you no need to check the index of 0 valued item in the array.