Home > Software engineering >  Feedback on solving this javascript question.. how to solve splitting array?
Feedback on solving this javascript question.. how to solve splitting array?

Time:11-16

I'm new to javascript and am having trouble solving this problem. This is the code I have entered and would like some feedback on where I'm going wrong/how I can solve this please and how to handle the callback.

Question: 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.

const partition = function(arr, callback) {
  let leftArr = [];
  let rightArr = [];

  for (let i = 0; i < arr.length; i  ) {
    if (arr[i] % 2 === 0) {
      leftArr.push(arr[i]);
    } else {
      (rightArr.push(arr[i]));
    }
    }
    return [leftArr, rightArr];
  };

CodePudding user response:

This callback is a function: which will return true if condition satisfied other wise it will return false so you will use it like this:

const partition = function(arr, callback) {
  //returns 2 arrays
  //if n % 2 === 0 => left
  //else => right, callback which returns a boolean
  let leftArr = [];
  let rightArr = [];

  for (let i = 0; i < arr.length; i  ) {
    if (callback(arr[i])) {
      leftArr.push(arr[i]);
    } else {
      rightArr.push(arr[i]);
    }
    }
    return [leftArr, rightArr];
  }; 

CodePudding user response:

const partition = function(arr, callback) {
  //returns 2 arrays
  //if n % 2 === 0 => left
  //else => right, callback which returns a boolean
  let leftArr = [];
  let rightArr = [];
  
  arr.map(i => {
    if(callback(i)) {
      leftArr.push(i)
    } else {
      rightArr.push(i)
    }
  })
  return [leftArr, rightArr];
};
  
console.log(partition([1, 2, 3, 4, 5, 6, 7], (i) => i % 2 === 0))
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

You can use JavaScript array filter.

const arr1 = [1,2,3,4,5,6,7,8,9];

const arr2 = arr.filter(num => num % 2 === 0);
const arr3 = arr.filter(num => num % 2 !== 0);

console.log(arr2); // [2,4,6,8]
console.log(arr3); // [1,3,5,7,9]

CodePudding user response:

At the moment you're not using the callback to determine is a number is odd or even. Define the function isEven and pass it in to the function as the callback. You can then call it on each loop iteration to check the number.

// Passing in the callback
// I've renamed it `isEven` to make the code
// easier to understand
const partition = function(arr, isEven) {

  let leftArr = [];
  let rightArr = [];

  for (let i = 0; i < arr.length; i  ) {
    
    const el = arr[i];

    // In your condition call the callback
    // function with the current element
    if (isEven(el)) {
      leftArr.push(el);
    } else {
      rightArr.push(el);
    }

  }

  return [leftArr, rightArr];

};

const arr = [0, 3, 1, 6, 7, 22, 100];

// Accepts a number and returns true
// depending if the number is even
const isEven = (n) => n % 2 === 0;

console.log(partition(arr, isEven));
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related