Recursive loop that prints an inverted count in JavaScript

In JavaScript, we can create a recursive function to print numbers in descending order from a given number down to zero. This demonstrates how recursion can solve problems by breaking them into smaller subproblems.

What is Recursion?

Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem. Each recursive call works on a reduced version of the original problem until reaching a base case that stops the recursion. Classic examples include calculating factorials, Fibonacci sequences, and tree traversals.

Problem Understanding

Our goal is to create a function that prints numbers in descending order from a given starting number down to zero using recursion. For example, if we start with 5, the output should be: 5, 4, 3, 2, 1, 0.

Algorithm

Step 1: Create a recursive function that accepts a number n as parameter.

Step 2: Define the base case: if n is 0, print 0 and return.

Step 3: Define the recursive case: print current n, then call the function with n-1.

Step 4: Call the function with the desired starting number.

Implementation

function invertedCount(n) {
    // Base case: stop when n reaches 0
    if (n === 0) {
        console.log(n);
        return;
    }
    
    // Recursive case: print current value and call with n-1
    console.log(n);
    invertedCount(n - 1);
}

// Example usage
console.log("Inverted count from 5:");
invertedCount(5);

console.log("\nInverted count from 3:");
invertedCount(3);
Inverted count from 5:
5
4
3
2
1
0

Inverted count from 3:
3
2
1
0

How It Works

The function works by:

  • Base Case: When n equals 0, it prints 0 and stops recursion
  • Recursive Case: For any positive n, it prints the current value and calls itself with n-1
  • Call Stack: Each function call waits for the next call to complete before finishing

Alternative Implementation

Here's a version that handles negative numbers and includes input validation:

function safeInvertedCount(n) {
    // Handle negative numbers
    if (n < 0) {
        console.log("Please provide a non-negative number");
        return;
    }
    
    // Base case
    if (n === 0) {
        console.log(n);
        return;
    }
    
    // Recursive case
    console.log(n);
    safeInvertedCount(n - 1);
}

// Test with different inputs
safeInvertedCount(4);
console.log("---");
safeInvertedCount(-2);
4
3
2
1
0
---
Please provide a non-negative number

Complexity Analysis

Complexity Type Value Explanation
Time Complexity O(n) Function calls itself n+1 times
Space Complexity O(n) Each call adds a frame to the call stack

Key Points

  • Always define a clear base case to prevent infinite recursion
  • Each recursive call should work on a smaller problem
  • Recursion uses the call stack, which has memory limitations
  • For very large numbers, consider iterative solutions to avoid stack overflow

Conclusion

Recursive functions provide an elegant way to print inverted counts by calling themselves with decremented values. While simple to implement, remember that recursion uses call stack memory, making it less suitable for very large numbers compared to iterative approaches.

Updated on: 2026-03-15T23:19:00+05:30

479 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements