1
respuesta

sorteo aleatorio con el método push

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Secreto</title>
    <h1>SECRETO</h1>
  </head>
  <body>
    <input placeholder="Digite numero aqui" />
    <button>Comprobar número secreto</button>
    <script>
      function sorteo() {
        return Math.round(Math.random() * 10);
      }

      function numeroAsignado(cantidad) {
        let secretos = [];

        for (let i = 1; i <= cantidad; i++) {
          numeroSorteado = sorteo();
          secretos.push(numeroSorteado);
        }

        return secretos;
      }

      let secretos = numeroAsignado(3);
      console.log(secretos);

      input1 = document.querySelector("input");
      input1.focus();

      function verificar() {
        encontrado = false;
        for (let position = 0; position < secretos.length; position++) {
          if (parseInt(input1.value) == secretos[position]) {
            alert("Es correcto");
            encontrado = true;
            break;
          }
        }
        input1.value = "";
        input1.focus();
        if (encontrado == false) {
          alert("Incorreto");
        }
      }
      let button = document.querySelector("button");
      button.onclick = verificar;
    </script>
  </body>
</html>
1 respuesta

Hola , espero que estés muy bien.

Estamos muy contentos con tu aprendizaje. Gracias por compartir tu código con nosotros. Continúa con tus estudios y cualquier duda estaremos aquí =)

Si este post te ayudó, por favor, marca como solucionado ✓. Continúa con tus estudios