Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

246
Views
JavaScript for loop, I don't understand

I don't understand the output of this question from CodeAcademy. It is stated in the for loop that X+=1, but why is the output printing the X index 0 for the second time? I thought it would be index 1(A) as X+=1.

const tags = ['G', 'A', 'T',' C'];
for (let x = 0; x < tags.length; x +=1) {
  for(let y = 1; y < tags.length - 1; y +=1) {
    console.log(`${tags[x]}${tags[y]}`,x,y);
  }
}
//output GA  GT  AA  AT  TA  TT  CA  CT

Output:

Console Image

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The inner loop starts with y = 1 and ends with y = 2 for this tags array. That means for each iteration of the outer loop, the console.log runs twice. That's why you are seeing 4 * 2 = 8 outputs printed out, where 4 is the number of iterations the outer loop goes through, and 2 is the number of iterations the inner loop goes through each time.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!