Loop | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 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.

21st Sep 2020, 9:51 PM
Timmy Afolami
Timmy Afolami - avatar
18 Respostas
+ 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šŸ‘šŸ»
21st Sep 2020, 11:16 PM
Vachila64ā˜•
Vachila64ā˜• - avatar
+ 8
Try this: for (let i = 0; i < 10; i++) { for (let j = 0; j < i; j++) { document.write("# "); } document.write("<br>"); }
21st Sep 2020, 10:05 PM
Namit Jain
Namit Jain - avatar
+ 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!ā˜•
21st Sep 2020, 10:20 PM
Vachila64ā˜•
Vachila64ā˜• - avatar
+ 5
Vachila64ā˜• Ooo nicee šŸ˜šŸ˜šŸ‘ŒšŸ‘Œ I just forgot that we can declare a variable šŸ˜…šŸ˜†
22nd Sep 2020, 7:08 AM
Namit Jain
Namit Jain - avatar
+ 5
Namit Jain šŸ˜…šŸ˜…šŸ˜…
22nd Sep 2020, 10:14 AM
Vachila64ā˜•
Vachila64ā˜• - avatar
+ 4
I think you should declare a variable to be # at the beginning
21st Sep 2020, 9:56 PM
Cyber Nate
Cyber Nate - avatar
+ 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 šŸ˜…
22nd Sep 2020, 7:52 PM
Vachila64ā˜•
Vachila64ā˜• - avatar
+ 3
Try this bro for (var i=0; i<100; i++){ console.log('#'); // Always prints in a line. }
21st Sep 2020, 10:09 PM
Cyber Nate
Cyber Nate - avatar
+ 3
Timmy Afolami actually I don't know how to prevent that line break with every statement šŸ˜…
21st Sep 2020, 10:58 PM
Namit Jain
Namit Jain - avatar
+ 3
Namit Jain Thanks a lot sir. šŸ˜šŸ˜šŸ˜ You're good. Hope to learn more from you also.
22nd Sep 2020, 12:17 PM
Timmy Afolami
Timmy Afolami - avatar
+ 2
Namit Jain Thanks very much. It worked.. But can I also achieve this with console.log?
21st Sep 2020, 10:26 PM
Timmy Afolami
Timmy Afolami - avatar
+ 2
Vachila64ā˜• Thanks very much.. Really appreciate. Look forward learn more from you.
22nd Sep 2020, 12:15 PM
Timmy Afolami
Timmy Afolami - avatar
22nd Sep 2020, 4:38 PM
Mr. 12
Mr. 12 - avatar
+ 2
Useful stuff Wendirad Demelash šŸ‘šŸ»
24th Sep 2020, 9:20 AM
Vachila64ā˜•
Vachila64ā˜• - avatar
+ 1
Cyber Nate Okay like saying i=# ?
21st Sep 2020, 9:57 PM
Timmy Afolami
Timmy Afolami - avatar
+ 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.
21st Sep 2020, 10:14 PM
Timmy Afolami
Timmy Afolami - avatar
+ 1
Vachila64ā˜• Thanks a lot. It worked. Can I also achieve this with console.log ?
21st Sep 2020, 10:28 PM
Timmy Afolami
Timmy Afolami - avatar
+ 1
You can use 'repeat' var x = "#" for (int i = 1; i < 8; i++){ console.log(x.repeat(i)) }
24th Sep 2020, 5:37 AM
Wendirad Demelash
Wendirad Demelash - avatar