I am doing a bingo and I would like to create the cardboard with 15 numbers from 1 to 100 without repeating them. Javascript Any kind of help is appreciated
I am doing a bingo and I would like to create the cardboard with 15 numbers from 1 to 100 without repeating them. Javascript Any kind of help is appreciated
The first function returns a number that we do not have in the array, the second one generates an array with 100 elements obtained with the function above, I hope it works for you.
function getRnd(numberArr) {
let newNumber;
while (!newNumber || numberArr.includes(newNumber)) {
newNumber = Math.floor((Math.random() * 100) + 1);
}
return newNumber;
}
function generateNumbers() {
const numberArr = [];
while (numberArr.length !== 100) {
const n = getRnd(numberArr);
numberArr.push(n);
}
return numberArr;
}
console.log(generateNumbers());