Prompt not running [closed]

-2
<script>
    function numAleatorio(tamanho)
{
    var nums = '0123456789';
    var aleatorio = '';
    for (var i = 0; i < tamanho; i++) {
        var rnum = Math.floor(Math.random() * nums.length);
        aleatorio += letras.substring(rnum, rnum + 1);
    }
    var palpite = numAleatorio(1)
    var x = prompt("Escreva um número de 0 a 9","0");
    var numero = parseInt(x);
    if (numero > 9){
        alert("Tá trapaceando né?")
    }
    if (numero == palpite){
        alert("o meu palpite é "+ palpite)
        alert("acertei? haha!")
    }
    else 
    {
        alert("aaah você ganhou...")
    }
}</script>
    
asked by anonymous 13.12.2018 / 00:46

1 answer

0

One of the causes that may be preventing the execution of the code has already been pointed out by @Isac: the prompt method returns a string. So, if you want to test as a number, you can use the parseInt function to convert the prompt string to integer, as the name itself says.

The other cause that may also be preventing the code from running is that when you compared the equality between numero and palpite , you used the assignment operator = , instead of the operator of equal == .

I hope I have helped!

    
13.12.2018 / 02:07