I have code like this:
s='';
For (i=1 ; i<=5 ; i++){
For(j=5 ; j>=i ; j--){
s+='*';
}
console.log (s)
}
The result
*****
*********
************
**************
***************
Please can anyone explain how the logic work?
Thanks before
Sorry i’m new in this forum, so if i’m wrong with my question please correct my question
Does formatting it help make it clearer? Have you already studied for
loops?
s = "";
for (i = 1; i <= 5; i++) {
for (j = 5; j >= i; j--) {
s += "*";
}
console.log(s);
}
Here’s the flow:
Add a star to s
Add a star to s
Add a star to s
Add a star to s
Add a star to s
Log s
Add a star to s
Add a star to s
Add a star to s
Add a star to s
Log s
Add a star to s
Add a star to s
Add a star to s
Log s
Add a star to s
Add a star to s
Log s
Add a star to s
Log s
1 Like
Thanks bro
I’m still learning
Hi StarSphere, the inner loop completes all of its iterations until it’s condition is false on each iteration of it’s outer loop. in the first iteration of outer loop inner loop iterates for five times and in the second iteration of outer loop the inner loop iterates for 4 times untill i becomes 6. Hope this helps
Thanks bro
Its help me to understand basic of javascript,
system
Closed
November 23, 2022, 1:09pm
6
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.