I thought of a neat solution to this. Not sure how efficient it is, but I thought it was neat.
If you look at your result array, you'll see results from "0000" to "2222". These are the numbers from 0 to 80. In base 3!
So, using that, I whipped up a function to create all numbers in that range as an array.
function createCards(length, max){
var cards = [],
// How many array elements
total = Math.pow(max, length),
// This will help with left padding
// since JavaScript doesn't have str_repeat('0', length)
pad = (''+Math.pow(10, length)).substring(1),
// temp loop variables
index, value;
for(index= 0; index < total; index++){
// Convert number into its new base
value = ''+index.toString(max);
// Left pad it so each string is the same length
value = pad.substring(0, pad.length - value.length) + value;
// Push into the array
cards.push(value.split('').map(function(a){
// Convert result to ints
return +a;
}));
}
return cards;
}
It's called using createCards(4,3)
.