1
respuesta

[Sugerencia] EJECUTANDO ARRAYS_Consulta_código.html

Código

<meta charset="UTF-8">

<input/>
 <button>Verificar si acertó con el secreto</button>


<script>

    function aleatorio(){
        return Math.round(Math.random()*10);
    }

    function sortearNumeros (cantidad){

        var secreto = [];
        var contador = 1;


        while (contador <= cantidad) {

            numeroAleatorio = aleatorio();
            secreto.push (numeroAleatorio);
            contador ++

        } 

        return secreto
    }

    var secreto = sortearNumeros (4);

    console.log(secreto); 

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


    function verificar () {

        var encontrado = false;

        for(var posicion = 0; posicion < 4; posicion ++) {


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

            alert ("Usté acertó");
            encontrado = true;
            break; 
            }

        }

        if (encontrado == false) {
            alert ("Ustéd erró");


        }


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

    }

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

</script>
1 respuesta

Cuando ejecuto este código, en cada cuarta iteración me responde usted acertó y no hace el break