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); }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 .
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); }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); }