Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!