+ 18
Nested for-loops are my biggest problem!
I understand when a for-loop stands alone... But when two or more are nested, I lose the picture of what is goes on in them... Can someone explain these two examples please (javascript) // First: var a = 0; for (var i = 1; i <= 2; i++) { for (var j = 0; j <= 5; j++) {} a++; console.log(a); } // Second: for (var i = 1; i <= 5; i++) { for (var j = 0; j <=5 j++) { // I have no idea what it does 😀 console.log(i + '' " + j); } }
24 ответов
+ 73
//first one:
the inner loop increments 'a'
6 times,
and since the count of the outer loop is 2,
it does so again :
for a total of 12.
alternatively,
inner_loop_count = 6;
outer_loop_count = 2;
final_loop_count = 6*2 =12;
//since a++ increments by 1;
//second one :
same concept applies:
inner_count = 6;
outer_count = 5;
final_count = 6*5 = 30;
(i + "" +j)
means i and j values are outputted as concatenated strings.
Each value of i,
is printed out 6 times,
before moving on to the next i value:
this is repeated 5 times:
111111 222222 333333 444444 555555.
for j,
each value is printed out 5 times,
before moving on to the next j value:
this is repeated 5 times :
012345 012345 012345 012345 012345.
Now since (i+""+j)
matches each i with a corresponding j,
the final output is:
10 11 12 13 14 15 20 21 22 23 24 25 30 31 32 33 34 35 40 41 42 43 44 45 50 51 52 53 54 55
//note: the total count of elements is thirty.
/*I don't think I like my explanation,
but I hope you now get the idea */
+ 50
@Wilson:
if a++ was inside the inner loop,
the output would have been :
1 2 3 4 5 6 7 8 9 10 11 12.
//based on my explanation earlier.
putting a++ outside the inner loop,
gives the final value of 'a',
I suppose.
+ 7
A demo using something more familiar, if it helps:
for(var week=1; week<=52; week++) {
document.write("Week " + week + ": ");
for (var day=1; day<=7; day++) {
document.write(day + " ");
}
document.write("<br />");
}
Week 1: 1 2 3 4 5 6 7
Week 2: 1 2 3 4 5 6 7
Week 3: 1 2 3 4 5 6 7
....
Week 52: 1 2 3 4 5 6 7
+ 5
@Washika I like your explanation. I understand the second one.....
But in the first one, I don't understand how inner-loop increments "a" 6 times since "a++;" is outside it?
It looks like:
for (var = 0; j <= 5; j++) {}
a++;
+ 5
for your first example, you can ignore the inner loop altogether as it does nothing { } - just pretend it isn't there -
'a' is incremented by the 'a++' and then output only within the main outer loop for each time it iterates (i.e. two times)
+ 5
if you want to iterate all vvalues of a bidimensional array you can represent it as a grid of rows and columns and use a nested for loop to cycle throu columns iterating all rows of that column each time.
remember the inner for loops completes all its iterations before the outer for loop moves to the next iteration; the cycle repeats until the outer for loop completes all its iterations.
+ 4
I use a nested for loop along side several includes functions in this code. Debugging is what helped me understand, lol.
https://code.sololearn.com/WELA9BYaWs7y/?ref=app
Plenty have answered your original question, but I have a question for you. How does the for Each method work?
+ 3
I hate old for..loop style too, makes a messy code. I have stopped using it, but instead with ES6, for...of loop and couple of awesome array methods forEach, maps, reduce, includes etc makes code clean.
+ 3
Well as long as I can get condition is true or false, that would be cool... I may try this on my chatBot there. Thanks for the response.
+ 2
Ans of 1st
for i=1
inner loop (j's loop) will iterate 6 times (0-5) and in every iteration a's value will be incremented by 1 and when j becomes 5 the loop will end.
for i=2 same thing will happen
Ans of 2nd
i assume that you know how iteration works in loop so let me explain the statement
console.log(i+" "+j); prints values of i and j on the console for example the values of i and j is 1 & 2 respectively so the statement will print
1 2
on the console
+ 2
@Russel, here is the syntax for forEach
arr.forEach((value, index, array) => // codes here)
The method iterate through all array items and calls a callback function to run some code on each items.
Gotchas:
The method does not return a value by itself. So either you push items after iteration to a new array or alert or console.log anything. You just have to manually return your iterated items.
+ 2
//1st one
always remember for each value of a variable in outer loop the inner loop runs..here for each value of I the j loop is iterating..how many times?? 6 times because j starts from 0 and ends in 5..j loop is null...here I loop iterates 2 times..and for each iteration of I loop j loop iterates 6 times..
+ 1
if you have a problem to understand something, try to find out for what you can use it. then the code is easier to understand, from my point of view.
when have two nested for-loops you can use them to walk through the cells of a table.
in the first example i can stand for the rows and j for the columns. in this example you walk first through the 6 columns of the first row and then through the 6 columns of the second. if you put a++ inside the second for-loop a gives you the total number of cells. here it stands for the number of rows.
the second example is nearly equal. here you have 5 rows and 6 columns. the output gives you the coordinates of the cell you are right know. could be necessary for debugging to find wrong cell input. try a try-catch around to show the user of your code for example: "wrong type in cell row 5 column 3"
have fun!
+ 1
To understand first ignore the outer for loop, a inner for loop will run like normal for loop that you understand with the condition j<=5. Starting from 0 to 5.
Now, when you take outer for into considerstion, it'll run 2 times for i=1 and 2.
for every outer iteration, inner loop will be initialised again so, it'll run from j=0 to j=5 with updated values of i.
Hope this helps.
+ 1
think about moon, earth and sun !
+ 1
draw a big circle for the main loop and little circles for loops inside. that is very useful to understand the loop function. I hope help you
+ 1
//2nd
outer loop iterates for 5 times as I time starts from 1 and ends in 5..now for each value of I loop..j loop is iterating..how many times ?? 6 times as j starts from 0 and ends in 5...
+ 1
more you practice
it will become more easy.
+ 1
They are my problem too, but I try to play with them...
https://code.sololearn.com/cpZASb23I6Jd/?ref=app
0
For nested loops if you have problem then you should at first write down the whole programme in some paper AND read it few times and calculate the outputs in your mind then try it..