moringaman
3/28/2017 - 5:53 PM

functional array operation

functional array operation

//create a function to iterate through an array passed to  it
//pushing the result of a function passed to it, to each value in newArray

function mapForArray(arr, fn){
  var newArray = [];
  for(i=0; i < arr.length; i ++){
    newArray.push(
    fn(arr[i])
    )
  }
  
  return newArray;
  
}

var arr1 = [1,2,3];
console.log(arr1)

//create a variable equal to the iterating function passing in the array
//plsa the function to be ran on each item

var arr2 = mapForArray(arr1, function(item){
  return item * 2;
})

var arr3 = mapForArray(arr1, function(item){
  return item < 3;
})

console.log(arr2);
console.log(arr3);