This is my first question on Stackoverflow :)
Below, I have set up two functions and I'm trying to use them to mutate the array "nums". But it doesn't seem to work. What am I doing wrong, especially in the last part after the function definitions? I am using higher order functions to learn how they work.
function forEach(array, callback) {
for(let i=0; i<array.length; i++) {
callback(array[i]);
}
}
function mapWith(array, callback) {
forEach(array, callback);
}
let nums = [1, 2, 3];
mapWith(nums, item => item+=2);
console.log(nums); //console output: [1, 2, 3]
But changing the last part the following way, does show that the array is changing the way I wanted. But why can't I manipulate it directly?
let nums = [];
mapWith([1, 2, 3], item => nums.push(item+=2));
console.log(nums); //console output: [3, 4, 5]