-2

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));
Shouldn't the recursion count down from 7? It goes up 1, 3, 5, 7 until the base case is met.
dr3nan
  • 37
  • 1
  • 8
  • 6
    This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Aug 29 '22 at 15:13
  • 2
    "*It goes up 1, 3, 5, 7 until the base case is met*" - what makes you assume that? Did you add some extra output to the function calls? – Bergi Aug 29 '22 at 15:14
  • @Bergi if I log the var result it logs 1, 3, 5, 7. I'm assuming it does 7, 5, 3, 1 at some point, otherwise n === 0 would be useless and there would be an infinite loop, but I'm struggling to see how does it. – dr3nan Aug 29 '22 at 15:19
  • 1
    @dr3nan Which values are you logging precisely? Do you log them before or after the recursive call? Try putting the `console.log('entering:', n)` statement in the first line of `addOdds`. – Bergi Aug 29 '22 at 15:19
  • @dr3nan: Because the `result` variable isn't created/assigned until after the recursion has taken place. So you're logging the value from the bottom of the recursion stack to the top. There's nothing *special* about recursion, it's just calling a function like any other function. If you log the value *after* the function is called then the function will have already been called. Step through with a debugger, or even just a pencil and paper, to see specifically what each step of the logic does. – David Aug 29 '22 at 15:21
  • @David thanks for the remark, but none of the three reasons apply to my case since I know the output and the function does what is has to do, I'm just having a hard time dissecting it and understanding each step. – dr3nan Aug 29 '22 at 15:22
  • @dr3nan: *"dissecting it and understanding each step"* - Then focus on one step that you don't understand. When you step through the code, one operation at a time, which specific operation is producing a result you didn't expect? – David Aug 29 '22 at 15:23
  • 1
    I think it has been made clear what my 'visual' issue was, I was only logging the variable after it has been defined, hence the 7, 5, 3, 1. Thanks @Bergi and David – dr3nan Aug 29 '22 at 15:28

1 Answers1

-2

Well, check first condition, n===0, it's not and pass the next one: 7%2 = 1. It means it doesn't equal zero then It will move to the next one and call function. Here the main case is calling function recursively and then n starts execution

  • 2
    This doesn't answer the question of why it's counting up instead of down. – Barmar Aug 29 '22 at 15:30
  • You should understand the call stack, stack works with LIFO (Last In First Out) Structure and therefore, and draw a recursive tree to see why. I wrote the simplest variant of thinking. Just draw a recursive tree and you will see that it will start from down to up – Ilkin Nazarov Aug 29 '22 at 17:59