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

151
Visualizações
Finding & Removing the top 10 largest numbers and the top 10 smallest numbers of the array

I would like to ask about an issue regarding an array I have generated an array with 100 integers

//Generate 100 numbers
        var arr = [];
        while(arr.length < 100){
            var r = Math.floor(Math.random() * 100) + 1;
            if(arr.indexOf(r) === -1) arr.push(r);
        }
        console.log(arr);

My issue is Finding and Eliminating the top 10 maximum and Minimum of the array

        //Find the Minimum 


        for (let i=0; i != 10; i++){

            arr.splice(Math.min(...arr), 1)

        }
            

        //Find the Maximum

        
        for (let i=0; i != 10; i++){
            

                arr.splice(Math.max(...arr), 1)
            
        
        }

and the result is only 90 of the array is eliminated is the maximum & minimum code fighting?

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

0

You need to get the index of the max and min value from the arr and then use splice. Also change i != 10; to i<10

var arr = [];
while (arr.length < 100) {
  var r = Math.floor(Math.random() * 100) + 1;
  if (arr.indexOf(r) === -1) arr.push(r);
}
for (let i = 0; i < 10; i++) {
  const min = arr.indexOf(Math.min(...arr));
  arr.splice(min, 1)

}
for (let i = 0; i < 10; i++) {
  const max = arr.indexOf(Math.max(...arr));
  arr.splice(max, 1)

};

console.log(arr.length)

about 4 years ago · Juan Pablo Isaza Relatório

0

You can sort the array and then use the splice method to remove the first and last 10 elements.

var arr = [];
while (arr.length < 100) {
  var r = Math.floor(Math.random() * 100) + 1;
  if (arr.indexOf(r) === -1) arr.push(r);
}

arr.sort((a, b) => +a - +b);

arr.splice(0, 10);

arr.splice(80, 10);

console.log(arr.length);
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