My project includes a function that generates three random numbers from the basket and maps each to a button. The idea is that each time you press a button you will get three new numbers and the number you pressed will be removed from the array. However, whenever the startTurn function is called, it spits out the same 3 numbers.
const repeat = (arr, n) => Array(n).fill(arr).flat();
let basket = repeat(["red", "orange", "yellow", "green", "blue", "purple"], 3);
function newGame() {
basket = repeat(["red", "orange", "yellow", "green", "blue", "purple"], 3);
console.log(basket);
startTurn();
}
const button1 = document.querySelector('#button1');
const button2 = document.querySelector('#button2');
const button3 = document.querySelector('#button3');
const button4 = document.querySelector('#new-game');
let turncount = 0;
let choice = [];
function startTurn(){
while(choice.length <= 3){
let r = Math.floor(Math.random() * (basket.length)) ;
if(choice.indexOf(r) === -1) choice.push(r);
}
button1.style.backgroundColor = basket[choice[0]];
button2.style.backgroundColor = basket[choice[1]];
button3.style.backgroundColor = basket[choice[2]];
console.log(basket.length);
console.log(basket[choice])
}
2
Answers
The problem is caused by this
Since your
choice
array is declared at Global Scope, once it gets filled up, that’s it. It will no longer be able to access thatwhile
loop.As every time
startTurn()
is called, the same oldchoice
array with initial 3 generations will keep on repeating.If you want it randomized each time and still have
choice
accessible at Global Scope, then empty choice each time before the loop starts.Here’s a barebones working example
Here is a rewrite of your JavaScript part using a simple shuffle for each game. I reduced the colors such that they will not be repeated in the buttons.