IFCD0210/Practicas/Practicas JS/Bucles 02/02_bucles_javascript.html
Marcos Lopez 085cb5dc63 Dia 31ç
2024-01-19 13:49:22 +01:00

59 lines
1.9 KiB
HTML

<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>02 Bucles javascript</title>
<link rel="stylesheet" href="css/estilos.css" />
</head>
<body>
<h1>Bucles Javascript 02</h1>
<script>
//1 Arrays beneficios y gastos
// Arrays de beneficios y gastos diarios(semana)
const ingresosDiarios = [120, 150, 200, 180, 250, 300, 210];
const gastosDiarios = [50, 60, 40, 80, 90, 70, 60];
// Inicializamos el beneficio neto total
let beneficioNetoTotal = 0;
/*
// Utilizamos un bucle 'for' para calcular el beneficio neto diario y total
for (let dia = 0; dia < ingresosDiarios.length; dia++) {
// Calculamos el beneficio neto diario restando los gastos del beneficio
let beneficioNetoDiario = ingresosDiarios[dia] - gastosDiarios[dia];
// Mostramos el beneficio neto diario
document.write(`Día ${dia + 1}: Beneficio Neto = ${beneficioNetoDiario}`);
document.write("<br>");
// Sumamos el beneficio neto diario al beneficio neto total
beneficioNetoTotal += beneficioNetoDiario;
}
// Mostramos el beneficio neto total al final de la semana
document.write(`Beneficio Neto Total Semanal = ${beneficioNetoTotal}`);
*/
//CON FOR EACH*
// Beneficio neto diario y total
ingresosDiarios.forEach(function(ingreso, dia) {
// Utilizamos la variable 'dia' para acceder a los gastos correspondientes
const gastoDiario = gastosDiarios[dia];
// Calculamos el beneficio neto diario restando los gastos del beneficio
let beneficioNetoDiario = ingreso - gastoDiario;
// Mostramos el beneficio neto diario
document.write(`Día ${dia + 1}: Beneficio Neto = ${beneficioNetoDiario}`);
document.write("<br>");
// Sumamos el beneficio neto diario al beneficio neto total
beneficioNetoTotal += beneficioNetoDiario;
});
document.write("Beneficio Neto Total Semanal ="+beneficioNetoTotal);
</script>
</body>
</html>