How To Print StairCase Pattern In Javascript | letsbug
Hi, in this article we are printing staircase pattern in javascript. It will help you understand loops and how you can use then in different ways. Understanding loops and how they work is very important.
Creating Staircase Pattern In Javascript
In the Below function we have first created a function staircase which takes n : number of rows of the staircase as parameter to it. Then the function print the staircase with n rows. In the function we have one outer loop and two inner loops.
code:
function staircase(n){ for(let i = 0; i < n; i++){ let str = '' let str1 = '' for(let j = 0; j < n - i - 1; j++){ str += " " } for(let k = 0; k <= i; k++){ str += "#" } console.log(str+str1) }}staircase(5)
Output:
#
##
###
####
#####
staircase when n is 15 |
That's It ☺
Comments
Post a Comment