Andaba aburrido y hice esto, ahora quiero poner condicionales para validar los ingresos de datos
<script>
let br = "<br />", name = "";
let myArray = [];
let sum = 0, average = 0, count = 0, weight = 0, height = 0;
function jumpLine() {
document.write(br.repeat(3));
}
function toPrint(text) {
document.write(text);
jumpLine();
}
function calculateBMI(personName, weight, height) {
let imc = weight / Math.pow(height, 2);
myArray.push(imc)
return { personName, imc } // return object {personName: "string", imc: number}
}
/*
this function is done to loop through an array and get an average of that same array,
when there is more than one person whose BMI is calculated.
*/
function arraySum(array) {
for (let i = 0; i < array.length; i++) {
sum = sum + array[i]
}
return sum / array.length;
}
count = prompt("Ingrese el número de personas a calcular su IMC")
for (let j = 1; j <= count; j++) {
name = prompt(`Persona # ${j} Ingrese su nombre`);
weight = prompt(`${name}, Ingrese su peso`);
height = prompt(`${name}, Ingrese su altura`);
const personIMC = calculateBMI(name, weight, height) // personIMC is a object {personName: "string", imc: number}, the last value entered.
toPrint(`El IMC de ${personIMC.personName} es: ${personIMC.imc.toFixed(2)}, su peso es: ${weight} y su estarura ${height}`)
}
average = arraySum(myArray).toFixed(2);
toPrint(`Promedio de IMC ${average}`)
</script>