jeudi 9 novembre 2017

Random number within range with no uniques (bingo)

Ok so im trying to create sorf of a bingo number generator, where i generate a unique number and display it alone. I also want that number + all the next numbers im generating to display at the bottom in a line.

My issue here is that whenever it hits a duplicate it loops until it finds a unique number, but displays that number as many times as it looped in the bottom row. So if i generate the numbers [4, 6, 2] and then another 4 it would keep looping until it found another number thats not already in the array. If it hits the numbers in the array 2 times then found a 5, it would display as [4, 6, 2, 5, 5, 5,]. Is there anything i can do to not make it display the first 2 5's?

window.onload = startup;

function startup(){ 
  document.getElementById("button").onclick = newNumber1;
}

var number = 0;
var bingoNumber = [];
var i;

function newNumber1() {
    number = Math.floor((Math.random() * 9) + 1);
    clickME();
}


function clickME() {
for (i=0; i < bingoNumber.length; i++) {
    if (number === bingoNumber[i]){ 
    newNumber1();   
        }
}
bingoNumber.splice(0, 0, number);
document.getElementById("display").innerHTML = bingoNumber[0];
document.getElementById("row").innerHTML = bingoNumber;
}




Aucun commentaire:

Enregistrer un commentaire