+ 2

Triangular loops(JavaScript)

can you help me creat a loop that prints # 7 times towards the bottom,increasing according to its position.. for example line one prints-# line two prints-## line three prints-### line four prints-#### ..etc .. pls help me out..(Javascript)

16th Jul 2018, 5:10 AM
Mensch
Mensch - avatar
4 Respuestas
+ 3
If you want to print a right triangle, then use this: var line = "#"; for (var n = 0; n < 7; n++) { document.write(line + "<br>"); line += "#"; } The program starts by defining a variable called "line" and assigning it to a single #. In the loop, line is printed with a break tag. Another # is added to line with each iteration.
16th Jul 2018, 5:27 AM
Erik Johanson
Erik Johanson - avatar
+ 2
Here you use substring method to print the string partially: var c = "#######"; for(i = 1; i < 8; ++i) document.write(c.substring(0, i) + "<br />"); Hth, cmiiw
16th Jul 2018, 5:41 AM
Ipang
+ 1
Tari var stars; for(var i=0; i<=7; i++){ stars += "#"; console.log(stars); }
16th Jul 2018, 5:22 AM
Dlite
Dlite - avatar
+ 1
For proper element update by JavaScript: <body> <div id="main"></div> <script> var printCh = "#"; var totalLine = 7; var main = document.querySelector("#main"); for(var row=0;row<totalLine;row++) { var lineCh = ""; for(var ch=0;ch<row+1;ch++) { lineCh += printCh; } main.innerHTML += lineCh + "<br/>"; } </script> </body> https://code.sololearn.com/W48Hg3nrsGKW/?ref=app
16th Jul 2018, 5:48 AM
Calviղ
Calviղ - avatar