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

136
Visualizações
Drop it exercise using map()

I am answering this exercise in FreeCodeCamp here's the instruction Drop it Given the array arr, iterate through and remove each element starting from the first element (the 0 index) until the function func returns true when the iterated element is passed through it.

Then return the rest of the array once the condition is satisfied, otherwise, arr should be returned as an empty array.

this is what I have so far. I used the map function to iterate the indexes of array then check if it met the condition of the function parameter inside the map. In this case, the function should return only the numbers less than 3 but I can't get rid of 3 when returning it.


function dropElements(arr, func) {
return arr.map(x => func(x) ? x : "");
}
console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Would suggest using a filter, which returns another array:

function dropElements(arr) {
  return arr.filter(x => x < 3);
}

const someArray = [1, 2, 3];
console.log(dropElements(someArray)); // [1, 2]

about 4 years ago · Juan Pablo Isaza Relatório

0

You could take a closure over a boolean value and keep a true value for the rest of the array for filtering.

Source of data/results: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it

function dropElements(arr, func) {
  return arr.filter((b => v => b ||= func(v))(false));
}

console.log(...dropElements([1, 2, 3], function(n) { return n < 3; }));       // [1, 2, 3]
console.log(...dropElements([1, 2, 3, 4], function(n) { return n >= 3; }));   // [3, 4]
console.log(...dropElements([0, 1, 0, 1], function(n) { return n === 1; }));  // [1, 0, 1]
console.log(...dropElements([1, 2, 3], function(n) { return n > 0; }));       // [1, 2, 3]
console.log(...dropElements([1, 2, 3, 4], function(n) { return n > 5; }));    // []
console.log(...dropElements([1, 2, 3, 7, 4], function(n) { return n > 3; })); // [7, 4]
console.log(...dropElements([1, 2, 3, 9, 2], function(n) { return n > 2; })); // [3, 9, 2]

about 4 years ago · Juan Pablo Isaza Relatório

0

It says "remove elements" so I'm gonna mutate the array that was passed in.

function dropElements(arr, func) {
  while (arr.length && !func(arr[0])) {
    arr.splice(0, 1);
  }
  return arr;
}

console.log(dropElements([1, 2, 3], function(n) {return n < 3; }));

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