Prompt still not running

Asked

Viewed 64 times

-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>
  • What do you mean you don’t execute ? By the way prompt reads a string and not a number

  • I modified the code, but it still doesn’t work

1 answer

0


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

The other cause that may also be preventing code execution, is that when you went to compare whether there is equality between the numero and the palpite, you used the assignment operator =, instead of the equality operator ==.

I hope I’ve helped!

  • is not working, or, I used the wrong parseint.

  • Where are you calling this function?

  • below the prompt.

  • I say the function numAleatorio

  • before the prompt, assigning to the guess variable.

  • But, by the code that is there, the variable palpite is within the function

  • Now I understand the error, thank you and forgive me for the inconvenience.

  • No problem! :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.