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

266
Views
Se utiliza el operador unario '++'. Error de pelusa en un ciclo while

Hice una función que suma el promedio de una matriz, pero tengo problemas con lint, habla de i++, probé [i += 1]; pero rompe mi código. Ingresa el código aquí

 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++ e i += 1 dan resultados diferentes... por lo que su linter se queja de usar i++ en primer lugar. Hace que el código sea confuso.

Parece que desea tomar el valor de i y luego agregarle uno para el siguiente ciclo.

i++ lo hará, pero i += 1 agrega uno antes de tomar el nuevo valor.


Divida su código en declaraciones separadas para hacerlo más claro (el orden es explícito) y más fácil de mantener.

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

Sin embargo, sería más idiomático escribirlo como un bucle for en lugar de un bucle while .

about 4 years ago · Juan Pablo Isaza Report

0

Me di cuenta, no estás contando. Yo usaría un bucle for como este

 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

Puedes hacerlo

 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!