<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nuevo secreto</title>
</head>
<body>
<h1>Secreto<h1>
<input type="text">
<button>Validar</button>
<h2></h2>
<script>
let input = document.querySelector("input");
let button = document.querySelector("button");
let text = document.querySelector("h2");
let valor = 0;
let arreglo = [2,4,16,0];
let fin = false;
button.addEventListener("click", comprobar);
function comprobar(){
let fin = false;
valor = parseInt(input.value);
for (let i = 0 ; i <= arreglo.length; i++){
if(valor == arreglo[i]){
text.textContent = "¡Acertaste!";
alert("¡Acertaste!");
fin = true;
break;
}
}
if(fin == false){
text.textContent = "¡Fallaste!";
alert("¡Fallaste!");
}
input.value = "";
input.focus();
}
</script>
</body>
</html>