DEV Community

Cover image for Staircase detail

Staircase detail

Klecianny Melo on February 16, 2024

Prepare your favorite cup of coffee because we are about to enter the fantastic world of Staircases. What is a Staircase? A staircase i...
Collapse
 
efpage profile image
Eckehard

Hy,

I was wandering about the output. You should put the printout in the listings, as the markdown kills all the leading spaces. Your result should look like this:

   #
  ##
 ###
####
Enter fullscreen mode Exit fullscreen mode

Anyway, this is also a nice case to learn some recursion:

function stair(n){
  function f(i){  
    if(i>=0){
      console.log(" ".repeat(i)+"#".repeat(n-i))
      f(i-1)
    }
  }
  f(n-1)
}
stair(4)
Enter fullscreen mode Exit fullscreen mode

If you like the freak style, you can also create a "one-liner":

const stair=(n,f)=>(f=(i)=>{console.log(" ".repeat(i)+"#".repeat(n-i));i?f(i-1):0 })(n-1)
stair(4)
Enter fullscreen mode Exit fullscreen mode

It works, try it out

Collapse
 
kecbm profile image
Klecianny Melo

Thank you for this contribution @efpage! 😁

Collapse
 
cherryramatis profile image
Cherry Ramatis

nice article ❤️

Collapse
 
kecbm profile image
Klecianny Melo

Thank you! 😁

Collapse
 
anthonyvii profile image
Anthony Vinicius

Wow! Nice article!

Collapse
 
kecbm profile image
Klecianny Melo

Thank you! 😁

Collapse
 
reenatoteixeira profile image
Renato Teixeira

nice article! thanks for sharing with us 😊

Collapse
 
kecbm profile image
Klecianny Melo

Thank you my friend! 😁

Collapse
 
vinidotruan profile image
Ruan Vinícius

Another one good article o/

Collapse
 
kecbm profile image
Klecianny Melo

Thank you! 😁