Could someone please break this down for me line by line in code?

I mean to know in which order the interpreter reads the text. I completely do not understand why then the numbers are increasing when first decreasing, I have a hypothesis that they somehow reverse push, I do not understand because the array is formed at the end, although technically it can not be formed at the end, so it turns out that it is not formed at the end. Someone please explain :pray:t3:

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);
    countArray.push(n);
    return countArray;
  }
}
console.log(countup(5));

This is a complicated subject and this question gets asked a lot. If you search the forum for “countup” you will find a lot of explanations. Let us know if none of those work.

3 Likes

i dont know what topic, but ur code is saying if what u pass in as n(5) is lesser than 1 return an empty array else u should keep subtracting n - 1 i.e 5-1,4-1,3-1 still its lesser than 1 push the numbers into the array,hope it helps.
Its recursion,u can google about it

Exactly. Why? That’s what I don’t understand

1 Like
function countup(n) {
  console.log("countup(" + n + ") called");
  if (n < 1) {
    console.log("Base case");
    console.log("  Returning", []);
    return [];
  } else {
    console.log("Recursive case");
    console.log("  Calling countup(" + (n - 1) + ")");
    const countArray = countup(n - 1);
    console.log("  Returned from countup(" + (n - 1) + ") =", countArray);
    console.log("  Note: back in countup(" + n + ")");
    console.log("  Pushing", n);
    countArray.push(n);
    console.log("  Returning", countArray);
    return countArray;
  }
}
console.log("Final result:", countup(5));
1 Like

Thanks for your effort bro!

1 Like

Is it possible to explain it in such a way that when a subroutine is executed, the whole stack of executions of these subroutines flies somewhere like a boomerang, and the main program goes on to push, then the boomerangs all come back and write themselves into the array? Maybe it’s not the best explanation, but it’s more or less the same?

There is no boomerang. Each function call executes line by line. The output happens in exactly the order that the code is executed.

countup(5) called
Recursive case
Calling countup(4)
  countup(4) called
  Recursive case
  Calling countup(3)
    countup(3) called
    Recursive case
    Calling countup(2)
      countup(2) called
      Recursive case
      Calling countup(1)
        countup(1) called
        Recursive case
        Calling countup(0)
          countup(0) called
          Base case
          Returning []
        Returned from countup(0) = []
        Pushing 1
        Returning [ 1 ]
      Returned from countup(1) = [ 1 ]
      Pushing 2
      Returning [ 1, 2 ]
    Returned from countup(2) = [ 1, 2 ]
    Pushing 3
    Returning [ 1, 2, 3 ]
  Returned from countup(3) = [ 1, 2, 3 ]
  Pushing 4
  Returning [ 1, 2, 3, 4 ]
Returned from countup(4) = [ 1, 2, 3, 4 ]
Pushing 5
Returning [ 1, 2, 3, 4, 5 ]
Final result: [ 1, 2, 3, 4, 5 ]

If it helps, note when you return from countup(n-1) to countup(n):

function countup(n) {
  console.log("countup(" + n + ") called");
  if (n < 1) {
    console.log("Base case");
    console.log("  Returning", []);
    return [];
  } else {
    console.log("Recursive case");
    console.log("  Calling countup(" + (n - 1) + ")");
    const countArray = countup(n - 1);
    console.log("  Returned from countup(" + (n - 1) + ") =", countArray);
    console.log("  Note: back in countup(" + n + ")");
    console.log("  Pushing", n);
    countArray.push(n);
    console.log("  Returning", countArray);
    return countArray;
  }
}
console.log("Final result:", countup(5));
1 Like

I suggest you watch some tutorial videos on YouTube on the following:

  • Recursive functions
  • Higher order functions.

It will make sense then.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.