Callback function not executing


Callback function not executing



The array, that is returned should be [1,2,3], however instead [3,4,5] is being logged to the console. Can somebody explain to me why?


[1,2,3]


[3,4,5]




function forEach(array, callback) {
var newA =
for (var i = 0; i < array.length; i++) {
newA.push(callback(array[i]));
}
}

function map(array, callback) {
forEach(array, callback)
return array
}

console.log(map([3, 4, 5], n => n - 2));





You never change array - the only thing that ever happens in forEach is that you call callback with array[i], but as primitives are call-by-value, that won't mutate anything. Did you perhaps mean to return newA; in forEach and accordingly return forEach(array,callback); in map?
– ASDFGerte
Jul 2 at 20:20



array


forEach


callback


array[i]


return newA;


forEach


return forEach(array,callback);


map




2 Answers
2



Your map() function requires that forEach() modifies the array in place, rather than creating a new array. So it should be:


map()


forEach()




function forEach(array, callback) {
for (var i = 0; i < array.length; i++) {
array[i] = callback(array[i]);
}
}

function map(array, callback) {
forEach(array, callback);
return array;
}

console.log(map([3, 4, 5], n => n - 2));



If map() isn't supposed to modify its argument, you could make a copy of the array before calling forEach().


map()


forEach()




function forEach(array, callback) {
for (var i = 0; i < array.length; i++) {
array[i] = callback(array[i]);
}
}

function map(array, callback) {
array = array.slice();
forEach(array, callback);
return array;
}

var testArray = [3, 4, 5];
console.log(map(testArray, n => n - 2));
console.log(testArray);



Callback Function not executing



Yes, it does, you just need to return the new array properly



Stack snippet




function forEach(array, callback) {
var newA = ;
for (var i = 0; i < array.length; i++) {
newA.push(callback(array[i]));
}
return newA; // added
}

function map(array, callback) {
return forEach(array, callback); // changed
}
console.log(map([3, 4, 5], n => n - 2));



Or deep clone the array and clear it, like this


array




function forEach(array, callback) {
var newA = JSON.parse(JSON.stringify(array)); // deep clone
array.length = 0; // clear the array
for (var i = 0; i < newA.length; i++) {
array.push(callback(newA[i]));
}
}

function map(array, callback) {
forEach(array, callback);
return array
}
console.log(map([3, 4, 5], n => n - 2));



or this




function forEach(array, callback) {
var newA = array.slice(); // shallow copy
array.length = 0; // clear the array
for (var i = 0; i < newA.length; i++) {
array.push(callback(newA[i]));
}
}

function map(array, callback) {
forEach(array, callback);
return array
}
console.log(map([3, 4, 5], n => n - 2));





So there's no difference between map() and foreach()?
– Barmar
Jul 2 at 20:42


map()


foreach()





@Barmar I answered the title question. Updated with an in place update of the array
– LGSon
Jul 2 at 20:48







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?