Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created April 30, 2015 16:06
Show Gist options
  • Save JavaScript-Packer/2dce3880b2867463c853 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/2dce3880b2867463c853 to your computer and use it in GitHub Desktop.
JavaScript simple golf coding example taking advantage of Bracket Notation using online golfing tool http://www.scriptcompress.com/golf.htm
//BEFORE: below is 394 byte sample that we want to make smaller with one simple technique
/*
var r={d4:function(){return Math.floor(4*Math.random())+1},
d6:function(){return Math.floor(6*Math.random())+1},d8:function(){return Math.floor(8*Math.random())+1},
d10:function(){return Math.floor(10*Math.random())+1},d12:function(){return Math.floor(12*Math.random())+1},
d20:function(){return Math.floor(20*Math.random())+1}};alert("rolling D4 : "+r.d4()),alert("rolling D20 : "+r.d20());
*/
//AFTER: 340 bytes, over 50 bytes saved
var M=Math,R="random",F="floor",r={d4:function(){return M[F](4*M[R]())+1},
d6:function(){return M[F](6*M[R]())+1},d8:function(){return M[F](8*M[R]())+1},
d10:function(){return M[F](10*M[R]())+1},d12:function(){return M[F](12*M[R]())+1},
d20:function(){return M[F](20*M[R]())+1}};alert("rolling D4 : "+r.d4()),alert("rolling D20 : "+r.d20());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment