Soy un principiante en JavaScript. Escribí esta función para calcular el porcentaje de tres valores, pero la salida se muestra incorrectamente. Está mostrando 176.33 en lugar de 74.3
function percentageCalculator(history, math, science) { let percentage = history + math + science * 100 / 300; console.log(percentage); }; percentageCalculator(88, 65, 70);Debería ser:
let percentage = (history + math + science) * 100 / 300;Al igual que las matemáticas, JavaScript también tiene un orden de cálculo.
Después de agregar los corchetes circundantes apropiados. Podrías simplemente dividir por 3 directamente en lugar de multiplicar por 100 y luego dividir por 300:
function percentageCalculator(history, math, science) { const percentage = (history + math + science) / 3; return percentage; } console.log(percentageCalculator(88, 65, 70).toFixed(1));