0

JS Nested loop

Why var = a ; is printing 1,2,3,4 and 5 times can anyone explain it? https://code.sololearn.com/WXf0BrGoQM1w/?ref=app

9th Jun 2021, 11:10 AM
Muhammad Imtiaz
Muhammad Imtiaz - avatar
9 ответов
+ 1
FAITH increment of b depends on a So if a = 1 then b range will be 1 to 1 so a will print 1 time For a = 2 , b range will be 1 to 2 so a will print 2 times For a = 3, b range will be 1 to 3 so a will print 3 times. For a = 5, b range will be 1 to 5 so a will print 5 times.
9th Jun 2021, 11:17 AM
A͢J
A͢J - avatar
+ 1
FAITH Because you have range from 1 to 5 and you have printed a and inner loop depends on outer loop.
9th Jun 2021, 11:13 AM
A͢J
A͢J - avatar
+ 1
FAITH Now you are printing b. Operation is same only value changed.
9th Jun 2021, 11:43 AM
A͢J
A͢J - avatar
+ 1
FAITH For a = 1, b will print 1 For a = 2, b will print 1 2 For a = 5, b will print 1 2 3 4 5
9th Jun 2021, 11:44 AM
A͢J
A͢J - avatar
+ 1
FAITH Yes a will print 5 times because for a = 5 inner loop will run till 5 times and a will be print 5 times because a is not changing.
9th Jun 2021, 1:38 PM
A͢J
A͢J - avatar
+ 1
FAITH Yes because for a = 5 inner loop runs 5 times so if you print b then it will print 1 2 3 4 5 and if you print a then it will print 5 5 5 5 5
9th Jun 2021, 1:39 PM
A͢J
A͢J - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 So is it depend on increment of var b ?
9th Jun 2021, 11:16 AM
Muhammad Imtiaz
Muhammad Imtiaz - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 Check the code Again .can you explain it?
9th Jun 2021, 11:36 AM
Muhammad Imtiaz
Muhammad Imtiaz - avatar
0
🅰🅹 🅐🅝🅐🅝🅣 For a = 5, b range will be 1 to 5 so a will print 5 times. It is printing like 5 5 5 5 5 But For a = 5, b will print 1 2 3 4 5 Why it is printing 1 2 3 4 5 ?
9th Jun 2021, 1:35 PM
Muhammad Imtiaz
Muhammad Imtiaz - avatar