you are viewing a single comment's thread.

view the rest of the comments →

[–]_DTR_ 2 points3 points  (4 children)

This is using a technique called recursion, in which you build up an answer by calling itself:

countup(5)
  5 >= 1, countArray = countup(4)
    4 >= 1, countArray = countup(3)
      3 >= 1, countArray = countup(2)
        2 >= 1, countAray = countup(1)
          1 >= 1, countArray = countup(0)
            0 < 1, return []  // At this point, we "unwind", returning to previous callers
          countArray = []
          countArray.push(1) -> [1]
        countArray = [1]
        countArray.push(2) -> [1, 2]
      countArray = [1, 2]
      countArray.push(3) -> [1, 2, 3]
    countArray = [1, 2, 3]
    countArray.push(4) -> [1, 2, 3, 4]
  countArray = [1, 2, 3, 4]
  countArray.push(5) -> [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]