I'm struggeling a bit with recursion and just solved an exercise where you have to sum up all the elements of an array.
function sumArr (para) {
if (para.length === 0) {
return 0
}
else {
return para.pop() + sumArr(para)
}
}
When invoked sumArr ( [2,3,4] ) it returns 7, as expected. However, now I changed the function a bit
function sumArr (para) {
if (para.length === 0) {
return 0
}
else {
return para.pop() - sumArr(para)
}
}
and when invoked sumArr ( [2,3,4] ) it returns 3, which I can't wrap my head around.
Thanks for reading or even helping me understand recursion a bit better!
4-(3-(2-(0))) = 3(a - b) - c != a - (b - c)