<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
#resultado {margin-top:20px;color:grey}
</style>
</head>
<body>
<form onSubmit="return false">
Introduce un numero: <input type="text" id="numero"> <input type="submit" value="Añadir" id="add"> <input type="button" value="Vaciar" id="clear">
</form>
<div id="resultado"></div>
</body>
</html>
<script>
let valores=[];
const add=document.getElementById("add").addEventListener("click",() => {
let numero=document.getElementById("numero");
if (!isNaN(parseInt(numero.value))) {
// añadimos el elemento en el array
valores.push(parseInt(numero.value));
// sumamos los valores del array
let suma=valores.reduce((a,b)=>a+b,0);
// Mostramos los valores
document.getElementById("resultado").innerHTML="Valores: "+valores+"<br>Suma: "+suma+"<br>Media: "+(suma/valores.length);
}
numero.value="";
});
document.getElementById("clear").addEventListener("click",() => {
valores=[];
document.getElementById("resultado").innerHTML="";
});
</script>
Comentarios sobre la versión: Versión 1.0 (0)
No hay comentarios