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

268
Views
Unary operator '++' used. Lint Error in a while loop

I made a function that sums the average of an array, but I'm having problems with lint, it talks about i++, I've tried [i += 1]; but it breaks my code. enter code here

function average(myArray) {
  let i = 0;
   let summ = 0;
   if (myArray.length === 0) return undefined;
   
    let ArrayVz = myArray.length;
  while (i < ArrayVz) {
    summ += myArray[i += 1];
    if (typeof summ === 'string') return undefined;
  }
  return Math.round(summ / ArrayVz);
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

i++ and i += 1 give different results … which is why your linter is complaining about using i++ in the first place. It makes for confusing code.

You appear to want to take the value of i and then add one to it for the next loop.

i++ will so that, but i += 1 adds one before taking the new value.


Split your code out into separate statements to make it clearer (the order is explicit) and easier to maintain.

summ += myArray[i];
i += 1;

It would be more idiomatic to write it as a for loop instead of a while loop though.

about 4 years ago · Juan Pablo Isaza Report

0

I noticed, your not counting i up. I would use a for loop like this

function average(myArray) {
  let i = 0;
   let summ = 0;
   if (myArray.length === 0) return undefined;
   
    let ArrayVz = myArray.length;
  for (let i = 0; i < ArrayVz.length; i++) {
    summ += myArray[i];
    if (typeof summ === 'string') return undefined;
  }
  return Math.round(summ / ArrayVz);
}
about 4 years ago · Juan Pablo Isaza Report

0

You can do this

function average(myArray) {
          let i = 0;
           let summ = 0;
           if (myArray.length === 0) return undefined;
           
            let ArrayVz = myArray.length;
          for (let i = 0; i < ArrayVz.length; i++) {
            summ += myArray[++i];
            if (typeof summ === 'string') return undefined;
          }
          return Math.round(summ / ArrayVz);
        }
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!