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

129
Views
How to I add a counter to this while loop

I need to add a counter to my while loop, so that I can know how many times the loop iterated.

var popSize = 100;
var endSize = 200;

var x = popSize / 3;     
var y = popSize / 4;     

while (popSize < endSize) 
{
    popSize = popSize + x - y; 
    popSize++;        
}
console.log(popSize);
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

var counter = 0;

while (popSize < endSize) 
{
    popSize = popSize + x - y; 
    popSize++;        
    counter++;
}

console.log(counter);
about 4 years ago · Juan Pablo Isaza Report

0

Store the iterations in a variable and increment it inside the loop:

var popSize = 100;
var endSize = 200;

var iterations = 0
var x = popSize / 3;
var y = popSize / 4;

while (popSize < endSize) {
  iterations++;
  popSize = popSize + x - y;
  popSize++;
}
console.log(popSize);
console.log('iterations', iterations)

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!