I'm struggling to comprehend recursion (I'm just a student) at its core, right now with one particular exercise I'm trying to do.
In the exercise, I'm trying to sum over the odd numbers of an integer, to do that, I have set another condition in order to only check odds (n % 2 === 0) n = n - 1
:
const addOdds = (n) => {
if (n === 0) return 0;
if (n % 2 === 0) n = n - 1;
let result = n + addOdds(n - 1);
return result;
}
console.log('Result of addOdds:',addOdds(7));
7
? It goes up 1, 3, 5, 7
until the base case is met.