Recursive loop that prints an inverted count in JavaScript


In the given problem statement, we have to print an inverted count with a recursive loop and code with the help of Javascript. So basically we have to print the count in inverted count or we can say in descending order.

What is Recursive technique ?

The recursive technique is an approach that involves solving a problem by breaking it into smaller pieces of the same problem till a base case is reached. This function calls itself with different arguments in order to achieve the required output. This is commonly used when the solution to a problem depends on solving a small portion of the same problem. For example, the Fibonacci series is a classic example of a problem that can be solved with the help of recursion.

Understanding the problem statement

The problem statement is to write a function in Javascript with the help of it we can print an inverted count or descending order numbers. And we have to implement this code by a recursive algorithm. A recursive loop is a loop that calls itself and can be used to solve the problems where the solution depends on solving smaller problems. In our case we have to print an inverted count which means starting from a given number the loop should print every integer in descending order until it becomes zero.

Logic for the given problem

So we will create a function that will be used to call itself for printing the countings starting from n to 0. The function will take a single parameter n, here n will represent the number of iterations to perform. First case in the function will be the base case where n is zero so the function will print 0 and return outside of the function. If n is not equal to zero then the function will print the current value of n and then will call itself with n-1 as the argument.

Algorithm

Step 1 − Create a recursive function to print the inverted count starting from n to 0.

Step 2 − Check the condition that the n is equal to zero. If it is zero then return the value of n and stop the loop.

Step 3 − Create a recursive case where n is not equal to zero. So call the function inside the above function with n-1 as argument.

Step 4 − Call the created function to print the required result.

Code for the algorithm

function invertedCount(n) {
   // Base case
   if (n === 0) {
      console.log(n);
      return;
   }
   // Recursive case
   console.log(n);
   invertedCount(n - 1);
}
// Call the function with an argument
console.log("The inverted count is as follows");
invertedCount(5);

Complexity

The time complexity for the above recursive function is O(n) because it performs a single operation as it is printing the current value of n for every value from n to 0. And the space complexity of the function is O(n) because it creates a new stack for each recursive call of the function.

Conclusion

So the recursive loop which is printing an inverted count is a simple algorithm. The function uses recursion technique to get the desired outcome. And the algorithm has time complexity of O(n) and space complexity of O(n).

Updated on: 18-May-2023

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements