Skip to content

Instantly share code, notes, and snippets.

@ronit-mukherjee
Created August 28, 2018 12:51
Show Gist options
  • Save ronit-mukherjee/e0d791283ae8b3fc8b1e0d855105b23b to your computer and use it in GitHub Desktop.
Save ronit-mukherjee/e0d791283ae8b3fc8b1e0d855105b23b to your computer and use it in GitHub Desktop.
HackerRank - Right Aligned Triangle (JS)
//https://www.hackerrank.com/challenges/staircase/problem
'use strict';
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
inputString += inputStdin;
});
process.stdin.on('end', _ => {
inputString = inputString.replace(/\s*$/, '')
.split('\n')
.map(str => str.replace(/\s*$/, ''));
main();
});
function readLine() {
return inputString[currentLine++];
}
// Complete the staircase function below.
function staircase(n) {
let staircase = "";
for(let r=0;r<n;r++){
for(let c = 0; c<n; c++){
if(c<(n-1)-r){
staircase += " ";
} else{
staircase += "#";
}
}
staircase += "\n";
}
console.log(staircase);
}
function main() {
const n = parseInt(readLine(), 10);
staircase(n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment