I have the following situation where I have a function f
which takes an argument input
.
I want to be able to have f
such that it satisfies the following output:
f('l') --> fl
f() --> fo
f()('l') --> fol
f()()('l') --> fool
f()()()('l') --> foool
I thought this would be achievable with:
function f(input) {
let str = 'f'
if (input) {
return `${str}${input}`
}
str = `${str}o`
return f()
}
However this ends up in an infinite loop. I also tried having f
return a function, but this too does not work.
How can I write f
in order to get my desired output while keeping the function stateless?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…