Answer the question
In order to leave comments, you need to log in
Two sets of parameters in one function? Or a function that returns another function? How to use it at all?
I was solving problems on codewars.com and came across the following problem:
I need to design a higher order function so that it extracts other functions from an array and executes them in a chain. Moreover, the input value is set ... as it is called something ... say, at the second level or order. That is:
chained([f1,f2,f3])(0)
Here is the original:
Your task is to write a higher order function for chaining together a list of unary functions. In other words, it should return a function that does a left fold on the given functions.
chained([a,b,c,d])(input)
Should yield the same result as
d(c(b(a(input))))
function chained(functions) {
return function(input){
console.log(input);
var param = input;
for (i = functions.length - 1; i >= 0; i--){
let making = functions[i];
param = making(param);
}
}
}
Expected: 4, instead got: undefined
Answer the question
In order to leave comments, you need to log in
why do you start iterating from the end of the array?
function chained(functions) {
return function(input) {
for (let i = 0; i < functions.length; i++) {
input = functions[i](input);
}
return input;
}
}
return
forgot, soundefined
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question