Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

290
Visualizações
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 !!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

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))

about 4 years ago · Juan Pablo Isaza Relatório

0

Regarding your failed function, it is reporting the current iteration but it appears it doesn't keep track of each iteration or better yet it should carry the most minimal number in each iteration.

  • Spread the array with ... operator

  • .filter() array straight through and it will return truthy values which excludes 0.

  • Then use Math.min()

  • Finally, get the index with .indexOf()

let data = [ 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 ];

let out = data.indexOf(Math.min(...data.filter(n => n)))

console.log(out);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can scan the array only once to find the minimum as well as the index.

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,
];

let [minVal, minIndex] = merged.reduce(
  function ([minVal, minIndex], currentVal, currentIndex) {
    if (currentVal!= 0 && minVal > currentVal) {
      minVal = currentVal;
      minIndex = currentIndex;
    }
    return [minVal, minIndex];
  },
  [Infinity, -1]
);

console.log(minVal, minIndex);

Also, it works.

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,
];

var smallest = Infinity;

for (let i = 0; i < merged.length; i++) {
  if (merged[i] < smallest && merged[i] != 0) {
    smallest = merged[i];
  }
  console.log("the smallest is", smallest, "in the iteration", i);
}

console.log("min-> : ", smallest);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda