1
resposta

[Bug] Meu programa não abre, fica carregando eternamente no navegador

<meta charset="UTF-8">

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

<script>
    function sorteia() {
        Math.round(Math.random() * 10);
    }

    function sorteiaNumeros (quantidade) {
        var segredos = [];
        var numero = 1;
        while(numero <= quantidade) {

            var numeroAleatorio = sorteia();
            
            if (numeroAleatorio !== 0) {
            
                var achou = false;

                for(var posicao = 0; posicao < segredos.length; posicao++) {
                    if(segredos[posicao] == numeroAleatorio) {
                        achou = true;
                        break;
                    }
                }

                if(achou == false) {
                    segredos.push(numeroAleatorio);
                    numero++;
                }
            }
        }
        return segredos;
    }

    var segredos = sorteiaNumeros(3);
    console.log(segredos);
    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("Você ACERTOU!");
                    achou = true;
                    break;
                }
        }

        if (achou == false) {
            alert("Você ERROU!");
        }
        input.value = "";
        input.focus();
    }
        
    var button = document.querySelector("button");
    button.onclick = verifica;

</script>

``
1 resposta

Olá, Luiz! Como vai?

Ao analisar o seu código, notei que você esqueceu de incluir a palavra-chave return na função sorteia(). Sem o return, a função não está realmente retornando nenhum valor, o que pode estar causando o problema de carregamento eterno.

Aqui está o código corrigido:

<meta charset="UTF-8">

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

<script>
    function sorteia() {
        return Math.round(Math.random() * 10); // Adicionei o 'return' aqui
    }

    function sorteiaNumeros (quantidade) {
        var segredos = [];
        var numero = 1;
        while(numero <= quantidade) {

            var numeroAleatorio = sorteia();
            
            if (numeroAleatorio !== 0) {
            
                var achou = false;

                for(var posicao = 0; posicao < segredos.length; posicao++) {
                    if(segredos[posicao] == numeroAleatorio) {
                        achou = true;
                        break;
                    }
                }

                if(achou == false) {
                    segredos.push(numeroAleatorio);
                    numero++;
                }
            }
        }
        return segredos;
    }

    var segredos = sorteiaNumeros(3);
    console.log(segredos);
    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("Você ACERTOU!");
                    achou = true;
                    break;
                }
        }

        if (achou == false) {
            alert("Você ERROU!");
        }
        input.value = "";
        input.focus();
    }
        
    var button = document.querySelector("button");
    button.onclick = verifica;

</script>

Lembre-se, quando você cria uma função que precisa retornar um valor, é importante usar a palavra-chave return para garantir que o valor seja realmente retornado.

Espero ter ajudado e bons estudos!

Caso este post tenha lhe ajudado, por favor, marcar como solucionado ✓.

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