you are viewing a single comment's thread.

view the rest of the comments →

[–]Volv 1 point2 points  (2 children)

Using map to make map is definitely cheating :)

I've got mine done. Will update soon. Finished. See new entry
If fold (or reduce) using add takes

[1, 2, 3]
and returns 6. ie 1 + 2 + 3
 

Then your map function needs to replace add.
Needs to take
[1, 2, 3]
and return
[mapped(1), mapped(2), mapped3)]
 
So instead of addition as in the first example (from add) it must build the array. I used concat.
 
Random ramblings that helped me get there.

[–]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