Check check it out http://jsfiddle.net/GpAUP/ Here is the meat of the code: var maxSize = 3*3*3*3; for(var i = 0; i < maxSize; ++i){ var a = Math.floor(i/27) % 3; var b = Math.floor(i/9) % 3; var c = Math.floor(i/3) % 3; var d = i % 3; $("body").append(a + "," + b + "," + c + "," + d ); $("body").append("<br />"); } essentially using modulus and a little linear math. You want `cards.push([a,b,c,d])` instead of appending to body. You can change the ordering if you want it a different way.