• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Can someone teach me how to understand this code, and point out my mistake?

Resolved: Can someone teach me how to understand this code, and point out my mistake?

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:


Hello im a newbie in learning javascript. May I know how the code works line by line? My answer is 12121212 but modal answer is 111222333444.
To my understanding, the inner loop must run finish first. The outer loop will be the number of times the loop will run. So, when i = 1 (first loop), j = 1, then since it ++, it will become 2 but not 3 since its j<3.
After that loop number 2, will print out j = 1 and 2 again This continues until the 4th loop ends.

Answer:

The concept behind your code is nested looping. So you need to multiply the count of each loops to get the total number of loops happening in the code. In your case
for(i=1;i<5;i++) runs 4 times i=1,2,3,4 for(j=0;i<3;j++) runs 3 times j=0,1,2
Here, It is 3*4 = 12 times
When it enters the first loop(i.e., i). The loop inside it runs 3 times. Since you are printing i, For the first loop, It would be 111. And then for the second loop. It would be 222 and So on.
You would understand better If you write like below,

If you have better answer, please add a comment about this, thank you!

javascript
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: The ‘Access-Control-Allow-Origin’ header contains multiple values ‘*, *’, but only one is allowed. cors error not resolving

02/04/2023

Resolved: SQLSTATE[42S02]: Base table or view not found: 1146 Table ‘crm.email’ doesn’t exist (Connection: mysql, SQL: select count(*) as aggregate from `email`

02/04/2023

Resolved: Regex: Curly braces in reverse? Match X characters before a string?

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.