How to useRecursion in Javascript
The recursion approach recursively prints a reverse Floyd pattern triangle pyramid. It decreases the number of rows in each call, printing descending numbers per row until reaching the base case of zero rows.
Example: The function myFunction recursively generates a reverse Floyd pattern triangle pyramid. It decreases rows in each call, printing descending numbers per row until reaching zero rows.
Javascript
function myFunction(rows, num = rows * (rows + 1) / 2) { if (rows === 0) return ; let pattern = '' ; for (let i = 1; i <= rows; i++) { pattern += num-- + ' ' ; } console.log(pattern); myFunction(rows - 1, num); } myFunction(6); |
21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
JavaScript Program to Print Reverse Floyd Pattern Triangle Pyramid
Printing a reverse Floyd pattern triangle pyramid involves outputting a series of numbers in descending order, forming a triangular shape with each row containing one less number than the previous row. The numbers are arranged in such a way that they form a right-angled triangle, reminiscent of Floyd’s triangle, but in reverse order.
Example:
Input: 5
Output:
15 14 13 12 11
10 9 8 7
6 5 4
3 2
1
These are the following approaches:
Table of Content
- Using nested Loop
- Using Recursion