Home > Blockchain >  return a new array containing the results of passing the original elements into the callbacks in an
return a new array containing the results of passing the original elements into the callbacks in an

Time:10-09

what should I do to get [3, 1.5, 15, 3.5, 27, 5, 63] output? Should I change the second for loops location ? Thanks in advance.

let alternatingMap = function(array,callback1,callback2) {
    const newArr=[];
    for (let i = 0; i < array.length ; i  = 2) {
        newNum1 = callback1(array[i]);
        newArr.push(newNum1)
    }
    for (let i = 1; i < array.length ; i  = 2) {
        newNum2 = callback2(array[i]);
        newArr.push(newNum2);
    }
    return newArr;
};

let half = function(num) {
    return num / 2;
}

let triple = function(num) {
    return num*3
}


let numbers = [1,3,5,7,9,10,21] // My output [3, 15, 27, 63, 1.5, 3.5, 5]
console.log(alternatingMap(numbers,triple,half));

CodePudding user response:

You could use only a loop with conditioned callback

let alternatingMap = function (array, callback1, callback2) {
  const newArr = []
  for (let i = 0; i < array.length; i  = 1) {
    let newNum = (i % 2 === 0 ? callback1 : callback2)(array[i])
    newArr.push(newNum)
  }
  return newArr
}

let alternatingMap = function (array, callback1, callback2) {
  const newArr = []
  for (let i = 0; i < array.length; i  = 1) {
    let newNum = (i % 2 === 0 ? callback1 : callback2)(array[i])
    newArr.push(newNum)
  }
  return newArr
}

let half = function (num) {
  return num / 2
}

let triple = function (num) {
  return num * 3
}

let numbers = [1, 3, 5, 7, 9, 10, 21]
console.log(alternatingMap(numbers, triple, half))

Moreover, you could still use two loops like your current way, a slight change is to mutate the new array

let alternatingMap = function (array, callback1, callback2) {
  const newArr = []
  for (let i = 0; i < array.length; i  = 2) {
    newArr[i] = callback1(array[i])
  }
  for (let i = 1; i < array.length; i  = 2) {
    newArr[i] = callback2(array[i])
  }
  return newArr
}

let alternatingMap = function (array, callback1, callback2) {
  const newArr = []
  for (let i = 0; i < array.length; i  = 2) {
    newArr[i] = callback1(array[i])
  }
  for (let i = 1; i < array.length; i  = 2) {
    newArr[i] = callback2(array[i])
  }
  return newArr
}

let half = function (num) {
  return num / 2
}

let triple = function (num) {
  return num * 3
}

let numbers = [1, 3, 5, 7, 9, 10, 21]
console.log(alternatingMap(numbers, triple, half))

CodePudding user response:

Just map the array and use one function or the other depending on whether the current index is even or odd:

function half (num) {
  return num / 2;
}

function triple (num) {
  return num * 3;
}

const numbers = [1, 3, 5, 7, 9, 10, 21];

const result = numbers.map((n, i) => (i % 2 === 0 ? triple : half)(n));

console.log(result); // [3, 1.5, 15, 3.5, 27, 5, 63]

More explanation:

numbers.map((n, i) => (i % 2 === 0 ? triple : half)(n))

The statement above can be written more verbosely this way:

numbers.map((number, index) => {
  const isEven = index % 2 === 0;
  const transformFunction = isEven ? triple : half;
  return transformFunction(number);
})

CodePudding user response:

In your 2 for loops you are not preserving the indexes, result array is populated completely from 1st loop and then 2nd loop adds its elements. Compare your output and expected output in terms of actual values you got.

So, feel free to take any solution you like. I replaced the half and triple functions with anonymous ones, you can change it back if you want.

const numbers = [1, 3, 5, 7, 9, 10, 21];

const alternatingMap = function (array, callback1, callback2) {
  const newArr = [];
  for (let i = 0; i < array.length; i  = 2) {
    const newNum1 = callback1(array[i]);
    newArr[i] = newNum1;
  }
  for (let i = 1; i < array.length; i  = 2) {
    const newNum2 = callback2(array[i]);
    newArr[i] = newNum2;
  }
  return newArr;
};

const alternatingMap2 = function (array, callback1, callback2) {
  const newArr = [];
  for (let i = 0; i < array.length; i  ) {
    const fn = i % 2 === 0 ? callback1 : callback2;
    const newNum1 = fn(array[i]);
    newArr.push(newNum1);
  }
  return newArr;
};

const alternatingMap3 = function (array, callback1, callback2) {
  return array.map((x, i) => (i % 2 === 0 ? callback1(x) : callback2(x)));
};

// [3, 1.5, 15, 3.5, 27, 5, 63]
console.log(
  alternatingMap(
    numbers,
    (x) => x * 3,
    (x) => x / 2
  )
);

console.log(
  alternatingMap2(
    numbers,
    (x) => x * 3,
    (x) => x / 2
  )
);

console.log(
  alternatingMap3(
    numbers,
    (x) => x * 3,
    (x) => x / 2
  )
);

  • Related