4
I’m a complete beginner, first of all.
A final challenge was proposed in the Alura system where you have to ask 6 numbers for a user and draw 6, then compare the arrays to see how much were hit. It’s a Mega Sena game.
I want to draw 6 random numbers that DO NOT repeat. What I’ve been able to do so far is:
<meta charset="UTF-8">
<script>
var frase = function(texto) {
document.write(texto + "<br>");
}
var sorteados = [];
for(i = 0; i < 6 ; i++) {
var sorteado = parseInt(Math.ceil(Math.random()*60));
sorteados.push();
frase(sorteado);
}
</script>
I would like to insert a if
before each number insertion to check if it already exists in the array, but I have no idea how to do it. I guarantee I’ve searched a lot, but nothing answers my question or is extremely complicated for a novice user like me.
This answers the question, but it is worth noting that if a number already exists it will not insert, but continue the iteration in the loop resulting in a number less at the end of the array.
– Luis Henrique
Thanks for the answer. .
– Guillermo Mila
Oops! sorry. Updated. I forgot to deal with the function that writes the text! The function
frase
should be within the verification whether or not that number already exists, withinuniqueNumber
– Wallace Maxters