I really need a feed back on my explanations especially on 2) and 3). I just want to confirm if I understood it correctly or no since I am a newbie.
This is a freeCodeCamp challenge which was really challenging for me because I have no experience with JS before. It goes as below.
Write a recursive function, sum(arr, n), that returns the sum of the first n elements of an array arr.
function sum(arr, n) {
if(n <= 0) {
return 0;
} else {
return sum(arr, n - 1) + arr[n - 1];
}
}
/* 1) sum([1], 0) should equal 0.
2) sum([2, 3, 4], 1) should equal 2.
3) sum([2, 3, 4, 5], 3) should equal 9. */
/* My explanations are down below */
/*
Explanation 1)
sum([1], 0) should equal 0.
n is less or equal to 0 so line 2 works and returns 0 at line 3.
*/
/* Explanation 2)
sum([2, 3, 4], 1) should equal 2
n is not less or equal to 0 so it will not return 0 according to line 2. We move to line 5.
*return sum(arr, n - 1) + arr[n - 1];
=> return sum([2, 3, 4], 1 - 1) + arr[1 - 1];
=> return sum([2, 3, 4], 0) + arr[0] => n is less or equal to 0 so it will return zero according to line 2.
=> return 0 + arr[0]
=> Since arr[0] is equals to 2
=> return 0 + 2;
=> 2
*/
/* Explanation 3)
sum([2, 3, 4, 5], 3) should equal 9
n is not less or equal to 0 so it will not return 0 according to line 2. We move to line 5.
*return sum(arr, n - 1) + arr[n - 1];
=> return sum([2, 3, 4, 5], 3 - 1) + arr[3 - 1];
=> return sum([2, 3, 4, 5], 2) + arr[2]; => n is 2, not less or equal to 0 so go back to line 5 + arr[2].
=> return sum(arr, n - 1) + arr[n - 1] + arr[2];
=> return sum([2, 3, 4, 5], 2 - 1) + arr[2 - 1] + arr[2];
=> return sum([2, 3, 4, 5], 1) + arr[1] + arr[2]; => n is 1, not less or equal to 0 so goes back to line 5 arr[1] + arr[2].
=> return sum(arr, n - 1) + arr[n - 1] + arr[1] + arr[2];
=> return sum([2, 3, 4, 5], 1 - 1) + arr[1 - 1] + arr[1] + arr[2];
=> return sum([2, 3, 4, 5], 0) + arr [0] + arr[1] + arr[2];
=> return 0 + arr[0] + arr[1] + arr[2];
=> in our array, arr[0] is 2, arr[1] is 3, arr[2] is 4.
=> 0 + 2 + 3 + 4
=> 5 + 4
=> returns 9
*/Advertisement
Answer
I don’t think we can make it shorter:
const sum = (arr,n) => --n<0 ? 0 : sum(arr,n) +arr[n] console.log ( sum([1], 0) ) console.log ( sum([2, 3, 4], 1) ) console.log ( sum([2, 3, 4, 5], 3) )
.as-console-wrapper { max-height: 100% !important; top: 0; }