We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

functioncreateString(str,ns,n){if(ns>0)returncreateString(str+" ",ns-1,n);elseif(n>0&&ns===0)returncreateString(str+"#",ns,n-1);elseif(n===0&&ns===0)returnstr;}// Complete the staircase function below.functionstaircase(n){letns=n.length-1;console.log(createString("",ns,n));}

i was going to add a while loop but i keep getting undefined not sure the problem.

And then I print a number of # amounting to the current loop index on each line followed with a newline break.

But since this will lead to the output being aligned on the left,
I also print (n-i) spaces on each line where n is the maximum number and i is the current loop index before executing (2). So for instance when i is 1, I print 5 spaces before I print 1 # to fill up the six spaces (total number of spaces on each line). When i is 2, I print 4 spaces before I print 2 # to make sure all the six spaces on the line are filled.
This also ensures that the # is aligned to the right.

NB. The range is (1, n+1). I didn't start from 0 because that will cause an empty space at the beginning of the output and I also ended at n+1 because the loop ends and n-1 so I add 1 to get to the correct n.

## Staircase

You are viewing a single comment's thread. Return to all comments →

How about this mate

might

3 nested for loops? There's def. something to be improved here, think about recursion.

its really only 2 nested for loops and therye each operating on different ranges.

2 nested loops and the size is extremely small so computation time is not something we need to consider

how do you see my Python 2 solution?

i was going to add a while loop but i keep getting undefined not sure the problem.

okay im an idiot and for some reason put n.length as if a int has a length.

fantastic idea dude

can you explain this ?

That was my solution basically as well. I like it ;)

i m still confused with the logic, can you please explain it furthure more?

NB. The range is (1, n+1). I didn't start from 0 because that will cause an empty space at the beginning of the output and I also ended at n+1 because the loop ends and n-1 so I add 1 to get to the correct n.

Do you get it now?

Yes! you explained it awesome. Thank you man!