Concatenate 2 repeaters of strings

0

'I need to make an array that makes a staircase with numerals, before the numeral there has to be a space, in the first "step" there are the same number of spaces as steps - 1, in the second - 2, and so, What I need is to concatenate the two repeaters, I leave what I have written so far.

function escalera(numero){
  var escalera = [];

  for(var i= 1; i <= numero; i++){

    for(var a =1; a < numero; a++){
      var escalon = " ".repeat(numero-a) + "#".repeat(i);

    }


    escalera.push(escalon);

  }
  return(escalera)

}

This should be seen:

escalera(5) = [ " #", " ##", " ###", " ####", "#####" ]

    
asked by Bautista Querejeta 17.02.2018 в 17:50
source

1 answer

2

You do not really need two loops. You can do just one and add as many elements # as i and as many spaces as the difference from the total minus i :

function escalera(numero){
  var escalera = [];
  for(var i= 1; i <= numero; i++){
    var escalon = " ".repeat(numero-i) + "#".repeat(i);
    escalera.push(escalon);
  }
  return(escalera)
}

console.log(escalera(5));
    
answered by 17.02.2018 / 18:09
source