0

I'm trying to create a forEach() function that takes an array and a function, then performs the function action on each element of the array. However, when trying to pass the below anonymous function, I get undefined. I tried adding a return to the forEach() function after reading some other posts but then the function doesn't run at all and just returns the first array[i] it receives without modifying it.

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

var myArray = [1, 2, 3];
var something = forEach(myArray, function(element){return element++;});
console.log(something)
//undefined

That returns undefined.

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

var myArray = [1, 2, 3];
var something = forEach(myArray, function(element){return element++;});
console.log(something)
//undefined

That returns 1.

What am I missing?

(I am aware a .forEach() function exists, I am trying this as a learning exercise)

3
  • First function doesn't have a return statement. Second one has a loop that exits on first iteration. It's a strange learning exercise anyway. Commented Jan 26, 2016 at 17:58
  • 2
    what are you expecting forEach to return ? Commented Jan 26, 2016 at 17:58
  • @ÁlvaroGonzález he is trying to implement forEach him self. It's a perfectly valid learning exercise I think. Commented Jan 26, 2016 at 18:00

2 Answers 2

3

Your forEach function doesn't return anything. It looks like you're trying your hand at a map implementation. If so, you need to add your results to a new array and return that array.

function map(array, action) { // Renamed forEach to a more suitable name
  var results = [];
  for (var i = 0; i < array.length; i++) {
    results.push(action(array[i]));
  }
  return results;
}
Sign up to request clarification or add additional context in comments.

1 Comment

don't name a returning function "forEach", name it "map" to avoid confusion
0

Your foreach implementation is correct - it shouldn't return anything. If you want map (i.e. transform an argument into a new array), you can implement reduce first:

function reduce(array, action, result) {
    forEach(array, function (elem) {
        result = action(result, elem);
    });
    return result;
}

and then, define map via reduce:

function map(array, action) {
    return reduce(array, function (result, elem) {
        return result.concat(action(elem));
    }, []);
}

Putting it all together:

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

function reduce(array, action, result) {
    forEach(array, function (elem) {
        result = action(result, elem);
    });
    return result;
}

function map(array, action) {
    return reduce(array, function (result, elem) {
        return result.concat(action(elem));
    }, []);
}

a = map([1, 2, 3, 4], function (x) {
    return ++x
});
document.write('<pre>' + JSON.stringify(a, 0, 3));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.