norbidrak norbidrak - 1 year ago 67
Javascript Question

Javascript - map and callback - returning values are not affected

I want to use map method on array with callback to another function. Everything seems to be working fine, but at the end returning values are not affected. I don't know what seems to be the problem.

var arr=[4,5,3,2];

function multi (x,callback){
return callback(x*2);

function add(x){
// alert(x); when I'm alerting "x" here, it's value is multiplied as it should be
return x+3;

var{multi(a,add); return a;});
final; // returns same values as Array "arr"

Jon Jon
Answer Source

Your callback function returns a, which is the same element as passed in to it. Therefore the result is the same as the input.

To get the expected output you should instead return the modified result, e.g.

var{return multi(a,add);});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download