DEV Community

Cover image for Printing Staircase in JavaScript
Tadea Simunovic
Tadea Simunovic

Posted on

Printing Staircase in JavaScript

For today's challenge we will create a function staircase, visual stairs made of pound symbol(#).
Let's take a look at the challenge description.

Challenge

Write a function that accepts a positive number N.
The function should console log a step shape
with N levels using the # character. Make sure the
the step has spaces on the right-hand side!
--- Examples
staircase(2)
'# '
'##'
staircase(3)
'#  '
'## '
'###'
staircase(4)
'#   '
'##  '
'### '
'####'
Enter fullscreen mode Exit fullscreen mode

This challenge supposed to form visual stairs made of # symbol. An argument number is a number of lines we want to pass in. If we have one step, we need to add a space (' ') after #.
We will reflect the current row with "i" and column with "j". To start we will for loop through rows from 0 to n.

function staircase(n) {
  for (let i = 0; i < n; i++)
}
Enter fullscreen mode Exit fullscreen mode

For each row, we are considering we will create an empty string step

function staircase(n) {
  for (let i = 0; i < n; i++) {
      let step = ' ';
  }
}
Enter fullscreen mode Exit fullscreen mode

Then we will iterate from 0 to n, through columns with for loop.

function staircase(n) {
  for (let i = 0; i < n; i++) {
      let step = ' ';

   for (let j = 0; j < n; j++) {
   }
  }
}
Enter fullscreen mode Exit fullscreen mode

Then inside of the inner loop, we will say, if the current column that we are looking at is equal to or less than the current row we want to add a pound(#) to step, else, we will add space (' ').

function staircase(n) {
  for (let i = 0; i < n; i++) {
      let step = ' ';

   for (let j = 0; j < n; j++) {
      if (j <= i) {
        step += '#'
     } else {
        step += ' ';
     }
   }
  }
}
Enter fullscreen mode Exit fullscreen mode

We will console.log(step) inside of our for loop, because we want to console.log n number of times.

function staircase(n) {
  for (let i = 0; i < n; i++) {
      let step = ' ';

   for (let j = 0; j < n; j++) {
      if (j <= i) {
        step += '#'
     } else {
        step += ' ';
     }
   }
   console.log(step)
  }
}
Enter fullscreen mode Exit fullscreen mode
staircase(6)
#
##
###
####
#####
######
Enter fullscreen mode Exit fullscreen mode

I hope you will find this helpful practicing data structure and algorithms.

Top comments (4)

Collapse
 
harrywonder profile image
stephen

Real smooth. But you could cut down on the second loop and achieve it this way too.

function StairCase(stairCaseLength) {
  let stairs = "";
  for (let i = 0; i < stairCaseLength; i++) {
    let stair = "#".repeat(i + 1) + "\n";
    stairs += stair;
  }

  console.log(stairs.trim());
}

StairCase(6);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mohammadkhalid23 profile image
Mohammad Khalid

but time complexity would be same because of .repeat

Collapse
 
yajindragautam profile image
Yajindra Gautam • Edited

Great.
You can make even more shorter like this

for(let i = 0; i< n; i++){
          // print out a " " n-i times and append a # i times
      // console log adds a new line by default

      console.log(" ".repeat(n-i) + "#".repeat(i))
    }
Enter fullscreen mode Exit fullscreen mode
Collapse
 
catherinemds profile image
Catherine

Thank you so much for this !!