Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

277
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda