DEV Community

Cover image for StairCase | HackerRank Solution in JavaScript
Christotle Agholor
Christotle Agholor

Posted on

2

StairCase | HackerRank Solution in JavaScript

Let's look at the Problem Statement :

Input Format
A single integer, n, denoting the size of the staircase.

Constraints: 0 < n <= 100

Output Format :

Print a staircase of size using # symbols and spaces.

Note: The last line must have spaces in it.

Example:
Sample Input : 4

Sample output

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

Solution 1. in JavaScript.

function staircase(n) {
    // Write your code here
    let str = '';
    for(let i = 1; i < n + 1; i++) {
        str += Array(n - i).fill(' ').join('')
        str += Array(i).fill('#').join('')
        console.log(str)
        str = ''
    }

}
Enter fullscreen mode Exit fullscreen mode

Solution 2. in JavaScript.

function staircase(n) {
  for (let i = 0; i < n; i++) {
    let str = Array(i + 1)
      .fill("#")
      .join("")
      .padStart(n);
    console.log(str);
  }
}
Enter fullscreen mode Exit fullscreen mode

OR

// without the padStart it will still give same result.

function staircase(n) {
  for (let i = 0; i < n; i++) {
    let str = Array(i + 1)
      .fill("#")
      .join("")
     console.log(str);
  }
}
Enter fullscreen mode Exit fullscreen mode

but in this case, the element flows to the left

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

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (1)

Collapse
 
johnchristotle profile image
Christotle Agholor

Great one from me. This is my first post and I believe it is the begining of good things to come for all my javascript enthusias.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay