Javascript code does not execute prompt

Asked

Viewed 335 times

0

The prompt is not appearing:

<script>
function numero(int num) {
  return Math.round(Math.random() * num);
};
var chute = prompt("Qual e o numero? ");
var numeroPensado = numero(10);
if(chute == numeroPensado) {
    document.write("Acertou! " + numeroPensado + " era o numero");
} else {
    document.write("Erro! " + numeroPensado +  " era o numero");
}
</script>

2 answers

5


Javascript has no annotation of data types in the variables and parameters, so the int causes the problem. You have to take this out. If you want to use types, you have to choose Typescript.

function numero(num) {
    return Math.round(Math.random() * num);
}
var chute = prompt("Qual e o numero? ");
var numeroPensado = numero(10);
if (chute == numeroPensado) {
    document.write("Acertou! " + numeroPensado + " era o numero");
} else {
    document.write("Erro! " + numeroPensado + " era o numero");
}

Or more simply:

var numeroPensado = Math.round(Math.random() * 10);
document.write((prompt("Qual e o numero? ") ==  numeroPensado ? "Acertou! " : "Erro! ") + numeroPensado + " era o numero");

I put in the Github for future reference.

  • I ended up forgetting because the syntax is different.

3

The mistake is in function numero(int num) int is not required.

<script>
function numero(num) {
  return Math.round(Math.random() * num);
};
var chute = prompt("Qual e o numero? ");
var numeroPensado = numero(10);
if(chute == numeroPensado) {
    document.write("Acertou! " + numeroPensado + " era o numero");
} else {
    document.write("Erro! " + numeroPensado +  " era o numero");
}
</script>

Browser other questions tagged

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