Hi all,
I try to do code challenges on codewars at least once a day. Todays challenge topic was “Unary Function Chainer,” which I was able to solve (with the help of google), but I have a question regarding the solution.
The challenge can be found here.
My solution was:
function f1(x){ return x*2 }
function f2(x){ return x+2 }
function f3(x){ return Math.pow(x,2) }
function chained(functions) {
return function inner (a) { //is a return here necessary to call function (a)?
for (var b=0; b<functions.length; b++){
a=functions[b](a);
}
return a
}
}
chained(f1,f2,f3)(4) //yields 4
I just want to make sure that I understand how this function chain works and was hoping you could help check my thought process.
Is it necessary to return my function inner (a) because that is the only way to call my function?
For instance, this didn’t work:
function chained(functions) {
function inner (a) { //is a return here necessary to call function (a)?
for (var b=0; b<functions.length; b++){
a=functions[b](a);
}
return a
}
return inner();