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

184
Visualizações
Why can't I remove elements from the array?

This function should remove elements that are less than 1 and more than 4, but it doesn't. The array is still the same.

let arr = [5, 3, 8, 1];

function filterRangeInPlace(arr, a, b) {
    arr.forEach((item, index) => {
        if (a > item && b < item) {
            arr.splice(index, 1);
        };
    });
};

filterRangeInPlace(arr, 1, 4);
console.log(arr);

What's wrong?

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

0

When would an individual item be both less than 1 and more than 4? If you want to remove items that are less than 1 or more than 4 you need to do:

if (a > item || b < item)
about 4 years ago · Juan Pablo Isaza Relatório

0

you are inverting the arguments when calling the function. you should rather do

filterRangeInPlace(arr, 4, 1)

about 4 years ago · Juan Pablo Isaza Relatório

0

you are basically keeping items between given range instead removing items go for keeping items this will return same also use filter function it basically iterate through array and just like foreach there are two parameters inside it item and index the only difference is filter keeps value when you return true or ignore if you return false

you can check more about filter here filter

let arr = [5, 3, 8, 1];

function filterRangeInPlace(arr, a, b) {
    return arr.filter(each=>{
       return each > a && each < b
    })
};

arr = filterRangeInPlace(arr, 1, 4);
console.log(arr);

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