Monday, 9 January 2017

Build a map function that takes a list as input and a function as input and returns a list of items where the function is applied to each item in the input list

function square(number) {
  return number * number;
}

function map(list[], function) {
result = [];
    foreach(number in list) {
        result.push(function(number));
    }
    return result;
}

map([1,2,3,4], square)

No comments:

Post a Comment