Codementor Events

Algorithms, building a staircase.

Published Sep 07, 2020
Algorithms, building a staircase.

i've been studying algorithms and some of the problems that i needed to solve was how to create an staircase using spaces and "#" symbol. The Staircase should be

n*n

with only one input indicating n, the staircase should return the satircase like this

     #
    ##
   ###
  ####

if n is 4.

My solution was.

let repeatFn = (strIn, q) => {
    return strIn.repeat(q);
};


function staircase(n) {

    let hash = '#';
    let spaces = ' ';
    let stairCaseSpaces = '';
    let stairCaseHashes = '';
    let stairCaseFinal = '';

    for(let i=1; i<= n; i++){

        stairCaseSpaces = repeatFn(spaces, n-i);
        stairCaseHashes = repeatFn(hash, i);
        stairCaseFinal = stairCaseSpaces.concat(stairCaseHashes);
        console.log(stairCaseFinal);
        
    };

}

staircase(6);

module.exports = staircase;
Discover and read more posts from Simon
get started
post commentsBe the first to share your opinion
Show more replies