How do I use nested repetitions to make this example?

Asked

Viewed 708 times

4

*
**
***
****
*****
******
*******
********
*********
**********

How do I do this with nested repetitions using the for Javascript, or in Visualg?

to give you an idea of what the code would be like :

    for(var linha = 0; linha < 10; linha = linha++) {
         for(var coluna = 0; coluna < 10; coluna = coluna++) {          Document.write("*"); 
}
 Document. Write("");
 }

It’s like this, but I can’t think of anything to give that effect using two loop

  • You don’t have to nest ribbons to print that on the screen.

  • You want to do that where? in an html page?

  • It’s cuddle, I’ve edited.

  • What is the need of the second for? Is it because you will use different characters? We want to help but I think it is not yet clear what is missing in the answers already given.

  • It’s a lesson. And she’s asking to do this exercise with 2 for

  • Ah!... ok, so you can do it like this: https://jsfiddle.net/ykmq1rnt/ This is what you were looking for?

  • I don’t understand this link

  • @H.Vceaser If any of the answers helped you solve the problem, mark it as accepted by clicking on the "arrow" below the score!

Show 3 more comments

2 answers

10


If you want to do this effect with Javascript you can do so:

var string = [];
for (var i = 0; i < 20; i++) {
    var stars = Array.apply(null, Array(i)).map(function(){return '*'});
    string.push(stars.join(''));
}
document.body.innerHTML = string.join('<br>');

This creates 20 lines within an array and then you can merge into a string with <br> between each line for line breaking.

A more modern Javascript version could be:

const string = [...Array(20)].map(
    (u, i) => [...Array(i)].map(() => '*').join('')
);
document.body.innerHTML = string.join('<br>');

Or using the new method .repeat as indicated in the other answer:

const string = [...Array(20)].map((u, i) => '*'.repeat(i));
document.body.innerHTML = string.join('<br>');


Note:

after reading the comment you wrote, I leave a version also with 2 loops for, if you prefer to use this:

var linhas = 20;
var texto = []
for (var i = 0; i < linhas; i++) {
	var string = '';
	for (var j = 1; j <= i; j++) {
		string += '*';
	}
	texto.push(string);
}
document.body.innerHTML = texto.join('<br>');

  • Yes, but I need to finish 2 nested repetition structure.

  • @H.Vceaser as so 2? side by side? what my code generates would be what part?

  • Sergio, I think he means to use one for inside another, where one controls the number of stars in the line, and the other controls only the line change

  • @H.Vceaser can give a static example of the result you want? is like diegofm suggested? that you want to have 2 simultaneous loops?

10

You can use the String.repeat to repeat a sequence according to the current iteration of loop:

for (var contagem = 1; contagem < 11; contagem++) {
    console.log("*".repeat(contagem));
}

To do the same in two loops nestled, do like this:

var linhas = 11;

for (var linha = 1; linha < linhas; linha++) { // 1 até 11
   for (var coluna = linha; ; coluna++) {      // De 1 até 11, porém sem condição
       console.log("*".repeat(coluna));
       break; // Interrompe o segundo loop
   } 
}

The first for will repeat the code until the current iteration reaches the value of linhas. The second for will only follow the first as no condition has been specified to repeat your code.

  • Almost that, I have to use 2 repeat structure. One inside the other. ( column and row )

  • 1

    Good! +1 for .repeat()

  • @H.Vceaser See if this is it. Thanks Sergio yours turned out great too!

  • 1

    I was just going over your answer with the double for. In this case the second for not even run once. I think it only complicates, it would be better as you have in the first version.

  • @Sergio De facto! he runs along with the first, I agree that the first version is more suitable, but the OP wants to use two for, that was the way I found to do it. :)

Browser other questions tagged

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