Hi,
Your solution for exercise 20 is not really correct. It works for provided functions and inputs, but won't work for situations where, for example, first function appends something to the string. It also assumes that parameters are strings.
Example where it will fail (return ssss):
ssss
const addS = (str) => str + 's'; const addLowerCase = (str) => str + str.toLowerCase(); const repeat = (str) => str + str; const capAddlowRepeat = [addS, addLowerCase, repeat]; console.log(pipe(capAddlowRepeat, "cat"));
I believe more complete and actually also simpler solution is to correctly use initial value of reduce, like below:
function pipe(callbacks, value) { return callbacks.reduce((accum, cb) => cb(accum), value); }
Cheers!
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Hi,
Your solution for exercise 20 is not really correct. It works for provided functions and inputs, but won't work for situations where, for example, first function appends something to the string. It also assumes that parameters are strings.
Example where it will fail (return
ssss
):I believe more complete and actually also simpler solution is to correctly use initial value of reduce, like below:
Cheers!