From logical view it looks fine. You could also try the ternary operator if you want to play around.
return n <= 0 ? 0 : sum(arr, n - 1) + arr[n - 1];
The first block is the if question. If its true is goes to the second block (starts whith ?) and if its false it goes to the third block (starts with :).
Answer from Athii on Stack OverflowFrom logical view it looks fine. You could also try the ternary operator if you want to play around.
return n <= 0 ? 0 : sum(arr, n - 1) + arr[n - 1];
The first block is the if question. If its true is goes to the second block (starts whith ?) and if its false it goes to the third block (starts with :).
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; }
Explanation for the given example for Replace Loops with Recursion
Basic JavaScript - Replace Loops using Recursion
Replace loop using recursion
Replace Loops Using Recursion Clarification
Videos
Actually, with n equals to 1 is quite easy. Let's check step by step. Here your sum function; let's add some line to make it easier to refer:
1: function sum(arr, n) {
2: if(n <= 0){
3: return 0;
4: }else {
5: return sum(arr, n - 1) + arr[n - 1];
6: }
7: }
Now, let's see what's happening step by step when you execute:
sum([2, 3, 4], 1)
The function sum is called with arr equals to [2, 3, 4], and n equals to 1.
Since n is not less or equals than 0 (line 2), we go to the else block, at line 5.
Now, here is where the recursion happens: we call again the function sum, passing the same arr, but not the same n, instead we pass n - 1.
So we call sum again, this time with arr equals to [2, 3, 4] but with n equals to 0.
Since n is 0 this time, at the line 2 check we proceed to line 3 and returns 0.
Now, the function exit, with the value 0, that we gave to the caller.
And the caller of sum([2, 3, 4], 0) was the execution sum([2, 3, 4], 1), specifically at line 5:
5: return sum(arr, n - 1) + arr[n - 1];
Since it returned 0, we can imaging like:
5: return 0 + arr[n - 1];
And remember that n is 1, so:
5: return 0 + arr[0];
Since arr[0] is equals to 2:
5: return 0 + 2;
And then why sum([2, 3, 4], 1) returns 2.
I'm not sure if it's clearer now, but I hope so. :)
sum(arr, n) is a recursive function that returns the sum of the first n elements of an array arr.
In your example, you provide sum([2, 3, 4], 1) which basically says, compute the sum of the first element (i.e. the value of n in this example is 1).
So it would go through the function as such...
// the first time through
function sum([2, 3, 4], 1) {
if(1 <= 0){ // false this time
return 0;
}else { // this is where we end up
return sum([2, 3, 4], 0) + 2; // sum will be the result of the recurse back into the function, plus 2
}
}
// the second time through
function sum([2, 3, 4], 0) {
if(0 <= 0){ // true this time
return 0; // send this result back up to the first run through
}else {
// not relevant this time
}
}
// back in the the first time through,
// we now have a value to work with below
// remember, this isn't the 'third' time through,
// it is back in the first time run through
// just re-printed here so you could see
// where the value gets returned from the second run
function sum([2, 3, 4], 1) {
if(1 <= 0){ // false this time
return 0;
}else { // this is where we end up
return sum(0 + 2); // we got a result from the second run through, sum is now 2
}
}
function operation() {
console.log("testing");
}
function repeat(operation, num) {
if (num === 0) return;
operation();
repeat(operation, num-1);
}
//repeat(operation, 10);
module.exports = repeat
Loops are iterative by nature. Recursive approach does not really fit into this situation. Anyhow, here you go. But use it only for fun, never for real :)
function repeat(func,maxruns,run){
if(run>=maxruns){
return;
}
func();
repeat(func,maxruns,(run||0)+1);
}
repeat(operation,10);