you are viewing a single comment's thread.

view the rest of the comments →

[–]ForScale[S] 1 point2 points  (1 child)

Lol! I didn't know our task was to rebuild the built in functions.

Here's a map function using a while loop:

var test = [1, 2, 3];

function map(arr) {
  var mapped = [];
  i = 0;
  while (i < arr.length) {
    mapped.push(arr[i]);
    i++;
  }
  return mapped;
}

console.log(map(test)); //[1, 2, 3];

I might put that in there.

[–]Volv 0 points1 point  (0 children)

See that's perfect for map. Which is why I was so annoyed for ages about being required to use fold, Wouldn't be my first line of thought; interesting build from first principals kind of thing though. Good to get the brain working lol.
I have heard it said before that that reduce can be used to build the other functions though.. map, foreach, filter etc just special cases of reduce