Adding spaces in Javascript

Asked

Viewed 1,797 times

5

I need some help with that...

We want to represent a ladder with variable height, using an array of strings.

For example, a ladder with height 3, we will represent with the following array:

var escada3 = [
 "  #",
 " ##",
 "###"
]

And a ladder with height 5, as follows:

var escada5 = [
 "    #",
 "   ##",
 "  ###",
 " ####",
 "#####"
]

Write a ladder function that uses a height (a number) and return an array that represents the corresponding ladder.

TIP In Javascript you can repeat a text using repeat as follows:

var degrau = "#".repeat(2); // agora degrau = "##";

This will serve to mount our steps;

But how do I insert the number of steps according to the number indicated by my result array? How to insert an element into an array?


I was able to solve part of the question by doing the repetitions, but he wants the white spaces, I don’t know how to add them, since I’ve tried " ".repeat, but javascript does not repeat spaces this way.

function escada(numeroDegraus) {
  var degrausEscada = [];
  var comparacao = numeroDegraus;
  for (let i = 1; i <= numeroDegraus; i++) {
    var degraus = "#".repeat(i);
    degrausEscada.push(degraus);
  }
  return degrausEscada;
}

console.log(escada(5));

  • "since I already tried " ". repeat, but javascript does not repeat spaces like this", No? I tested it here and it worked perfectly.

  • It repeats only one space, unlike the one requested in the question, that when passed a value 5, would have to

2 answers

3

A padStart can help you:

function escada(numeroDegraus) {
  for (let i = 1; i <= numeroDegraus; i++) {
    console.log('#'.repeat(i).padStart(numeroDegraus, ' '));
  }
}

escada(5);

Another option without the repeat and padStart:

var numeroDegraus = 5;
for (let i = 0; i < numeroDegraus; i++) {
    var temp = [];

    // Inclui os espaços primeiro
    let k = 0;
    for (; k < numeroDegraus - (i + 1); k++) {
        temp.push(' ');
    }

    for (; k < numeroDegraus; k++) {
        temp.push('#');
    }

    console.log(temp.join(''));
}

  • So, the site I’m doing the course, does not accept methods that were not taught, IE, would have to try to do everything using the if, for, repeat, push and a few more, but its worked perfectly the way he asks.

  • @Douglasmorais You can create the functions repeat and padStart to follow the rules of the exercise. For this take a look at their respective polyfills. Another alternative is to use this idea and write with these functions

  • @Douglasmorais added another option without the repeat and padStart. I hope it helps ;)

3


Hello! So if you can’t use another function, use the same one. Whereas you need to print enough space to fill the gap that will exist before, so it is the steps - the position that is (i)

function escada(numeroDegraus) {
  var degrausEscada = [];
  var comparacao = numeroDegraus;
  for (let i = 1; i <= numeroDegraus; i++) {
    var degraus = " ".repeat(numeroDegraus-i)+"#".repeat(i);
    degrausEscada.push(degraus);
  }
  return degrausEscada;
}

console.log(escada(5));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.