Computer >> Computer tutorials >  >> Programming >> Javascript

Recursive loop that prints an inverted count in JavaScript


We are required to write a recursive JavaScript function that takes in a number and print the reverse count until 0 from that number. The only condition for us is that we have to write this function making use of recursion only.

Example

The code for this will be −

const recursiveLoop = (counter) =>{
   if(counter > 0){
      recursiveLoop(counter - 1);
   };
   console.log(counter);
   return counter;
}
recursiveLoop(5);
recursiveLoop(15);
recursiveLoop(25)

Output

And the output in the console will be −

0
1
2
3
4
5
0
1
2
3
456789
10
11
12
13
14
150123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25