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

124
Views
What's wrong in my code ? in continuous count in JavaScript loop

I am trying to achieve 3,4,5 in output. But, am getting only 3,5 .its jumping/skipping one number. Donno why. Given my code below. Help me to fix the error and please let me know why its happening and what i am doing as error ?

Expected output,

3
4
5

output am getting,

3
5

my code,

var input1, input2;
input1 = Number(2);
input2 = Number(5);
for(let i=input1;i<input2;i++) {
    i=i+1;
    console.log(i);
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You are incrementing i inside the body of the for loop even though it already does it for you.

input1 should also be 3.

Use:

var input1, input2;
input1 = Number(3);
input2 = Number(5);
for (let i = input1; i <= input2; i++) {
  console.log(i);
}

about 4 years ago · Juan Pablo Isaza Report

0

The for loop already increments i for you. The loop you wrote executes in the following path, going from 1 -> 2 -> 3 -> 4 -> 2 -> 3 and repeats unless the check at 3 is false:

for(
let i=input1; // 1. Declare initial value [i]
i<input2;     // 3. Check if [i] < [input2]
i++           // 4. increment [i] by 1
) 
{             
  i=i+1;      // 2. Run the contents of the loop body
  console.log(i);
}

That means

// First iteration
i = input1        // 1. i = Number(2);
  i = i + 1;      // 2. i = 2 + 1;
  console.log(i)  // 2. console.log(3);
i < input2 ?      // 3. Check if i is smaller than input2. End loop if not
i++;              // 4. Increment i so i = 4

// Second iteration
  i = i + 1;      // 2. i = 4 + 1;
  console.log(i)  // 2. console.log(5);
i < input2 ?      // 3. Check if i is smaller than input2. End loop if not

// End loop since i < input2 is not true
about 4 years ago · Juan Pablo Isaza Report

0

You are incrementing variable 'i' two times

  1. On for loop
     for(let i=input1;i<input2;i++) {}
    
  2. Inside for loop
      i=i+1; 

So your variable 'i' will increment with 2

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!