One way to work around your problem is to pass a flag to the function which indicates whether this is the first call, and only in that case capitalise the hi
. Note that you can simplify the code slightly by returning a !
when n == 0
; then you don't need to special case n == 1
:
function greeting (n, first = true) {
if (n === 0) {
return "!";
}
else {
return `${(first ? 'Hi' : 'hi') + greeting(n - 1, false)}`
}
}
console.log(greeting(3)) // should return Hihihi!
console.log(greeting(5)) // should return Hihihihihi!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…