I'm trying to generate a card deck for the game SET, or, for those who don't know what that is, I'm trying to fill an array with unique elements of the form [a, b, c, d]
, where 0 <= a, b, c, d <= 2
(81 elements). So, I want [[0, 0, 0, 0,], [0, 0, 0, 1], ... , [2, 2, 2, 2]]
(the order doesn't matter). This is the code I have so far:
var deck = [],
count = [0, 0, 0, 0];
for (var i = 0; i < 81; i++) { // go through all combos
deck.push(count); // append the current modification of count to deck
// increment count by 1, carrying "tens" if necessary
for (var j = 3; count[j] === 2; j--) {
// if the "digit" is 2, make it 0 since it overflows
count[j] = 0;
}
// j is the first "digit" of count that isn't already 2, so we add 1 to it
count[j]++;
}
Instead, what this seems to do is fill the deck
array with the last modification of count
; if the upper bound on i
is 81, this is [0, 0, 0, 0]
since it rolls over all the way around, and if you change the bound to anything lower it will respond accordingly. Why does this happen? What mistake have I made?