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

150
Views
Javascript :higher order functions

I am new to coding and am practising higher order functions. Given the below question:

Write a function which will split an array into two arrays (i.e. partition it).

It will take two parameters, the first is an array of Integer values, and the second will be a callback which will return a boolean. If the callback returns true for an element, it should be placed into the left array, otherwise it should be placed into the right array.

Examples:

  • partition([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n => n % 2 === 0) should look like this: [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
  • partition([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], n => n < 0) should look like this: [[-5, -4, -3, -2, -1], [0, 1, 2, 3, 4, 5]]
const partition = function(arr, callback) {
    // IMPLEMENT ME
};

I came up with the below:

const array = [];
arr.filter((integers) => {
  if (integers === callback) {
    array.push(integers);
    return integers;
  }
});
};

partition([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], (n) => n % 2 === 0);
partition([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], (n) => n < 0);

I am stuck and unable to understand how to use .filter with callback, how do I split it using the .filter way, can you please guide me with .filter method for the above question.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The filter function only allows you to return an array matching a condition. Therefore, if you want to build two array with the filter function, you can use it like that:

let partition = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let evenPartition = partition.filter(x => x % 2 === 0);
let oddPartition = partition.filter(x => x % 2 !== 0);
//Even partition : [2, 4, 6, 8, 10]
//Odd partition : [1, 3, 5, 7, 9]

You can also use the map function:

let partition = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let finalResult=[[], []];
partition.map(x => {
   if(x % 2 === 0)
      finalResult[0].push(x);
   else
      finalResult[1].push(x);
});

//Final result: [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]
about 4 years ago · Juan Pablo Isaza Report

0

You need to filter the values which return true for the callback and collect all other values and return a new array with the values from filtering and the other collected values.

const partition = function(arr, callback) { // IMPLEMENT ME
    const array = [];
    return [
        arr.filter(value => {
            if (callback(value)) return true;
            array.push(value);
        }),
        array
    ];
};

console.log(partition([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], n => n % 2 === 0)); // [[2, 4, 6, 8, 10], [1, 3, 5, 7, 9]]

console.log(partition([-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5], n => n < 0)); // [[-5, -4, -3, -2, -1], [0, 1, 2, 3, 4, 5]]

about 4 years ago · Juan Pablo Isaza Report

0

Array.filter should take a callback that returns a boolean. If the callback returns true, it keeps the value in the resulting array, and if it returns false it does not add it to the array.

If you want to continue to use filter, what you have is close to working, except for the fact that you are checking for strict equality between each element and the callback function when you should instead be calling said function with callback(integer).

You also need to store the output of the filter into the second array if you want to use it.

const leftArray = [];
const rightArray = arr.filter((integer) => {
  if (callback(integer)) {
    leftArray.push(integer);
    return false;
  }
  return true;
});
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!