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

155
Visualizações
why after applying my function to the array it returns the same array?

I'm looping over an array with my function compare to get the max number of temperature from the array but there was no any changes after applying the function to the array temperature.

let temperature = [1, 2, 3, 4, 5, 6, 7, 8, 9, "error"];

function compare(temperature) {
  let i = 0;

  for (i = 0; i < temperature.length; i++) {
if (typeof temperature[i] === "string") temperature.splice(i, 1);
  }

  for (let k = i + 1; k < temperature.length; k++) {
if (temperature[i] < temperature[k]) Array.splice(i, 1);
else if (temperature[i] > temperature[k]) Array.splice(k, 1);
  }
  return temperature;
}

console.log(temperature);

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

0

Check this out!

let temperature = [1, 2, 3, 4, 5, 6, 7, 8, 9, "error"];

function compare(temperature) {
    let i = 0;

    for (i = 0; i < temperature.length; i++) {
        if (typeof temperature[i] === "string") 
            temperature.splice(i, 1);
    }

    for (let k = i + 1; k < temperature.length; k++) {
        if (temperature[i] < temperature[k]) 
            Array.splice(i, 1);

        else if (temperature[i] > temperature[k]) 
            Array.splice(k, 1);
    }
    return temperature;
}

console.log(compare(temperature));
about 4 years ago · Juan Pablo Isaza Relatório

0

You can achieve this with a single line of code by using Math.max() on array.

Working Demo :

// Input array
let temperature = [1, 2, 3, 4, 5, 6, 7, 8, 9, "error"];

// Math.max() to get maximum number element from an array.
const maxTemp = Math.max(...temperature.filter(elem => typeof elem !== 'string'));

// Result
console.log(maxTemp);

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