I have a class that has two functions. Add, which adds a function to an array, and Execute which executes the array of function against its argument. My code is:
class LazyEvaluation {
constructor(){
this.functionQueue = []
}
add(fn){
this.functionQueue.push(fn)
return this
}
evaluate(target){
for (let i = 0; i < this.functionQueue.length; i++){
let newArray = target.map(this.functionQueue[i])
return newArray
}
}
}
As it stands, this code works when i only have one function in the array. My problem is as soon as there is more than one function in the array the execute function creates a new array for each function.
For example, when the functionQueue array has the following two functions:
(function timesTwo(a){ return a * 2 })
(function addOne(a) { return a + 1 })
And the execute function is given [1, 2, 3]
The output I need is [3, 5, 7]
however I am getting two separate outputs of [2, 4, 6]
and [2, 3, 4]
How do I ensure that the execute function doesn't create a new array for each function in the functionQueue?