2
respuestas

no me sale alert usted acerto

Hola compañeros, podrian por favor ayudarme, escribi el codigo exactamente como lo hizo el instructor y al poner el dato correcto igual sale "usted erro". muchas gracias

<!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>Juego secreto</title>
</head>
<body>


    <input/>
    <button>Verificar si acerto con el secreto</button>

    <script>

        var secretos = [3,5,7,9];

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


        function verificar () {

            var encontrado = false;

            for(var posicion = 0; posicion < secretos.length; posicion++) {

                if(parseInt(input.value) == secreto[posicion]) {

                    alert("usted acerto");
                    encontrado = true;
                    break;
                }
            }   

            if(encontrado == false) {

                alert("usted erro")
            }



            input.value = "";
            input.focus();


        }

        var button = document.querySelector("button");
        button.onclick = verificar;



    </script>

</body>
</html>
2 respuestas

Hola Luis!

en la línea 29 dice secreto en lugar de secretos:

if(parseInt(input.value) == secreto[posicion]) {

debería corregirse así:

if(parseInt(input.value) == secretos[posicion]) {

con ese cambio ya funciona. Slds!

Hola Jose, Muchas gracias, no habia podido dar con el error,