5
\$\begingroup\$

What can be more elegant ways to solve the given problem.

    #
   ##
  ###
 ####

Code:

function StairCase(n) {
    for (var i = 1; i <= n; i++) {
        var col = i;
        for(var j = 1; j <= n - col; j++) {
            process.stdout.write(' ');
        }
        for(var j = 1; j <= col; j++) {
            process.stdout.write('#');
        }
        process.stdout.write('\n');
    }

}
\$\endgroup\$
6
\$\begingroup\$

JavaScript function names should start with a lowercase character, and "staircase" is one compound word in English.

Since every line is nearly the same, and only one character changes at a time, you should take advantage of that and use an array as a buffer.

function staircase(n) {
    var line = Array(n + 1).fill(' ');
    line[n] = '\n';
    for (var i = n - 1; i >= 0; i--) {
        line[i] = '#';
        process.stdout.write(line.join(''));
    }
}
\$\endgroup\$
4
\$\begingroup\$

No need for the additional for loops:

function StairCase(n) {
    var s = '';
    for (var i = 1; i <= n; i++) {
            s += ' '.repeat(n - i) + '#'.repeat(i)  + '\n';
    }
    return s;
}

If you don't have repeat for some reason, another implementation could be:

String.prototype.Repeat = function(count) {
    var ret = '';
    for(var i = 0; i < count; i++){
        ret += this.valueOf();
    }
    return ret;
}

function StairCase(n) {
    var s = '';
    for (var i = 1; i <= n; i++) {
            s += ' '.Repeat(n - i) + '#'.Repeat(i)  + '\n';
    }
    return s;
}
\$\endgroup\$
  • \$\begingroup\$ Why not write Repeat as String.prototype.repeat? Wouldn't that allow for more flexibility? \$\endgroup\$ – Heman Gandhi Jul 26 '16 at 15:27
  • \$\begingroup\$ @HemanGandhi Thanks, I've updated my answer. \$\endgroup\$ – ChatterOne Jul 26 '16 at 16:03
4
\$\begingroup\$

This traditional Javascript code works fine:

function staircase(n) {
  var line = Array(n + 1).fill(' ');
  //line[n] = '';
  for (var i = n - 1; i >= 0; i--) {
    line[i] = '#';
    console.log(line.join(''));
  }
}

and ES6 we can write:

const staircase = n => {
  const line = Array(n + 1).fill(' ');
  //line[n] = ''; 
  for (let i = n - 1; i >= 0; i--) {
    line[i] = '#';
    console.log(line.join(''));
  }
}

If our given input is 6 the output should be:

     #
    ##
   ###
  ####
 #####
######
\$\endgroup\$
3
\$\begingroup\$

No need to use two for loops, you can use repeat function inside for loop.

See the below example.

var staircase = "",
    n = 6,
    space = " ",
    hash = "#";
for(var i = n ; i > 0 ;i--){
    staircase+=space.repeat(i-1).concat(hash.repeat(n-(i-1))) + "\n";
}
console.log(staircase);

Output Will be :

     #
    ##
   ###
  ####
 #####
######
\$\endgroup\$
  • \$\begingroup\$ Welcome to StackExchange Code Review! Please see: How do I write a good answer?, where you will find: "Every answer must make at least one insightful observation about the code in the question. Answers that merely provide an alternate solution with no explanation or justification do not constitute valid Code Review answers and may be deleted". \$\endgroup\$ – Stephen Rauch Mar 22 '17 at 6:18
0
\$\begingroup\$

The last loop must not be the same as the previous one, because the problem ask for not leave any space characters. So You need to skip from creating it as '\n'.

let i = 0;
let stair = '';

for (i = 1; i <= n; i++){
    if (i < n) {
    stair += ' '.repeat(n - i) + '#'.repeat(i) + '\n';   
    } else {
        stair += '#'.repeat(i);   


    } 
}

console.log(stair)

}
\$\endgroup\$
  • 1
    \$\begingroup\$ Welcome to Code Review! Your answer states: "the problem ask for not leave any space characters". Where is this mentioned? \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Jul 17 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.