Loop | SoloLearn: Learn to code for FREE!

+2

Loop

Let's say I have for(i=1 ; i<8;i++){ Var txt="#" } How do I create a loop where the next value for i have an extra #. Like I should have # ## ### #### and so on , where # keeps increasing with i.

9/21/2020 9:51:28 PM

Timmy Afolami

18 Answers

New Answer

+7

Perhaps this might do Namit Jain Timmy Afolami In the Js let amount = 1 let lines = 10 let character = "#" let toCon; //Does the loop that is inside the given amount of times for(i=0; i<lines; ++i){ //Sets toCon back to nothing toCon=" "; for(j=0; j<amount; ++j){ toCon+= character //makes one string } console.log(toCon) //prints the string amount++ } The power of nested for loops comes to the rescue once more👏🏻

+8

Try this: for (let i = 0; i < 10; i++) { for (let j = 0; j < i; j++) { document.write("# "); } document.write("<br>"); }

+7

Sorry, my first answer was wrong😅 Timmy Afolami This should work In the Js let amount = 1 let lines = 10 let character = "#" //Does the loop that is inside the given amount of times for(i=0; i<lines; ++i){ //Writes the character amount number of times for(j=0; j<amount; ++j){ document.write(character) } document.write("<br/>") // inserts a new line amount++ } The power of nested for loops😎 Good luck on your coding adventure!☕

+5

Vachila64☕ Ooo nicee 😍😍👌👌 I just forgot that we can declare a variable 😅😆

+5

Namit Jain 😅😅😅

+4

I think you should declare a variable to be # at the beginning

+4

Hi, sorry I took some time to get back to you😅. Nested loops are loops that have loops inside loops The outside loop does the inside loop over and over again. This is useful when you want to do a loop for a particular thing over and over again I think our will find these helpful as well! If you still don't understand, I'll be happy to explain more!🙂 https://goo.gl/search/Understandung+nested+loops+in+js Nested Loops JavaScript - Coding Rooms The most common type of nested loops will be one loop inside another. The first loop is usually called the outer loop while the second loop is called the inner loop. The outer loop always executes first, and the inner loop executes inside the outer loop each time the outer loop executes once. https://dev.to/muzammil1984/explaining-nested-loops-for-beginners-javascript-1b58 Explaining Nested Loops for Beginners - JavaScript. - DEV DM wasn't working properly 😅

+3

Try this bro for (var i=0; i<100; i++){ console.log('#'); // Always prints in a line. }

+3

Timmy Afolami actually I don't know how to prevent that line break with every statement 😅

+3

Namit Jain Thanks a lot sir. 😁😁😁 You're good. Hope to learn more from you also.

+2

Namit Jain Thanks very much. It worked.. But can I also achieve this with console.log?

+2

Vachila64☕ Thanks very much.. Really appreciate. Look forward learn more from you.

+2

https://code.sololearn.com/W7svLi9gJku0/?ref=app Timmy Afolami Simple & Perfect.

+2

Useful stuff Wendirad Demelash 👏🏻

+1

Cyber Nate Okay like saying i=# ?

+1

Cyber Nate Already tried this ,buh didn't work. My expected output is something like # ## ### #### ##### ... And so on ,as i increases the number of # follows.

+1

Vachila64☕ Thanks a lot. It worked. Can I also achieve this with console.log ?

+1

You can use 'repeat' var x = "#" for (int i = 1; i < 8; i++){ console.log(x.repeat(i)) }