Question:

Write the $$$.inject() function so the result variable is assigned to the array [1, 4, 9, 16].

var callback = function (num) {
  return num * num;
}

var arr = [1, 2, 3, 4]
result = $$$.inject(arr, [], callback)
console.log(result)

Answer:

$$$ = {
  inject: function (arr, memo, callback) {
    arr.forEach(function (e) {
      memo.push(
        callback(e)
      )
    })
    return memo;
  }
}

Keywords:

© 2017 QuizBucket.org