2
respostas

Erro syntax

Meu cod não esta executando os comando Erro L 22 não entendi o motivo

<meta charset="UTF-8">

<input/>
<button>Compare com o meu segredo</button>

<script>


    var segredos = [5,7,10,2,3];

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

    function verifica(){

    var achou  = false;
        for(var posicao = 0; posicao < segredos.length; posicao++);{

            if(input.value == segredos[posicao]){
                alert("voçê ACERTOU!");
                achou = true;
                break;
            }
        }

        if(achou == false);{
        alert("voçê ERROU!");
        }

        input.value = "";
        input.focus();
    }
    var button = document.querySelector("button");

    button.onclick = verifica;

</script>
2 respostas

Olá Irani, você acabou utilizando (ponto e virgula) ; apos suas declaracoes de for :

for(var posicao = 0; posicao < segredos.length; posicao++);{}

E if: if(achou == false);{}

Basta remove-los e seu código, ira funcionar; espero ter ajudado. Coloquei o código corretamente abaixo:

<meta charset="UTF-8">

<input/>
<button>Compare com o meu segredo</button>

<script>
     var segredos = [5,7,10,2,3];

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

    function verifica() {

    var achou  = false;

        for(var posicao = 0; posicao < segredos.length; posicao++) {

            if(input.value == segredos[posicao]){
                alert("voçê ACERTOU!");
                achou = true;
                break;
            }
        }

        if(achou == false) {
        alert("voçê ERROU!");
        }

        input.value = "";
        input.focus();
    }
    var button = document.querySelector("button");

    button.onclick = verifica;

</script>

Ok obg.

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software