'Create staircase from symbols using javascript

I want to output the staircase from the symbols "#". It should look like this:

enter image description here

but all I achieve is this:

enter image description here

What should I do to get right output?

var n = 6;
var rows=[];
var cols=[];

for(i=n-1;i>=0;i--) {
    rows=[];
    for(j=0;j<n;j++) {
        if(j >= i) {
            rows[j] = "#";
        } else {
            rows[j] = "";
        }
    }
    
    cols.push(rows);
    cols.splice(0, cols.length - 1);
    console.log(cols.join(","));
}


Solution 1:[1]

Think of it as a coordinate system, loop through y and x and add the needed symbols.

Remember to increase max x with current y if you want it dual sided.

function ladder(size, dualSided, empty, occupied) {
    if (dualSided === void 0) { dualSided = true; }
    if (empty === void 0) { empty = " "; }
    if (occupied === void 0) { occupied = "?"; }
    var str = "";
    for (var y = 0; y < size; y++) {
        for (var x = 0; x < size + y; x++) {
            if (dualSided != true && x == size) {
                break;
            }
            if (x >= size - y - 1) {
                str += occupied;
            }
            else {
                str += empty;
            }
        }
        str += "\n";
    }
    return str;
}
console.log(ladder(20, false));

Solution 2:[2]

Ok, try this(last 3 lines);

cols.push(rows.join(""));
cols.splice(0, cols.length - 1);
console.log(cols.join("")); 

The issue is you are pushing array(row) in cols, where row array itself contains comma. If you do cols.push(rows.join("")) , all comma will be removed.

Solution 3:[3]

Simple solution using Array.from
Live demo check below.

function staircase(n) {
    
    let arr = Array.from({ length: n }).fill(0);
    
    arr.map((v,i) => {
        
        let dummyArr = Array.from({ length: i+1 }).fill('#');
        
        let spaceArr = Array.from({ length: arr.length - (i+1) }).fill(' ');
        
        console.log(`${spaceArr.join('')}${dummyArr.join('')}`);        
    });
    
}

staircase(6);

Solution 4:[4]

try this simple solution

for (let i = 1; i <= n; i++) {
    console.log("#".repeat(i).padStart(n));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Emil S. Jørgensen
Solution 2 KOUSIK MANDAL
Solution 3 Suresh Pattu
Solution 4 Mohsin Sunasara