I have an array of items between 1 to 8. I want to select an item from that array and have it in a new array such that the item occurs either 0, 2 or 4 times in the new array.
const firstArr = [4, 2, 1, 7, 8, 3, 5, 6]; //array of items between 1 to 8
const secondArr = [1, 1, 7, 6, 7, 7, 8, 7, 1, 3, 1, 6] //should contain a number
// in firstArr and that number must occur 0, 2, 4 times.
The max length of secondArr
must be 12.
I tried something like:
while(secondArr.length <= 12){
//select random index from firstArr and fill secondArr until the number at index is even
}
But this almost goes into an infinite loop.