<meta charset="utf-8">
<input/>
<br><br>
<button>Compare com o meu segredo</button>
<script>
var input = document.querySelector("input");
var button = document.querySelector("button");
function getRandomNumber() {
return Math.round(Math.random() * 5);
}
function getSecretsList(quantity) {
var secrets = [];
var accumulator = 1;
while (accumulator <= quantity ) {
var foundDuplicatedNumber = false;
var randomNumber = getRandomNumber();
if ( randomNumber != 0 ) {
for (var index = 0; index < secrets.length; index++) {
if ( secrets[index] == randomNumber ) {
foundDuplicatedNumber = true;
break;
}
}
if (foundDuplicatedNumber == false) {
secrets.push(randomNumber);
accumulator++;
}
}
}
return secrets;
}
var secrets = getSecretsList(3);
function revealSecrets() {
var foundSecret = false;
for (var index = 0; index < secrets.length; index++) {
if (secrets[index] == parseInt(input.value)) {
foundSecret = true;
alert("Voce acertou");
break;
}
}
if (foundSecret == false) {
alert("Voce errou");
}
input.value = "";
input.focus();
}
button.onclick = revealSecrets;
</script>