Skip to content

Instantly share code, notes, and snippets.

@ondras
Forked from 140bytes/LICENSE.txt
Last active August 31, 2015 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ondras/42966b6be042ca83b21b to your computer and use it in GitHub Desktop.
Save ondras/42966b6be042ca83b21b to your computer and use it in GitHub Desktop.

140byt.es

A tweet-sized, fork-to-play, community-curated collection of JavaScript.

How to play

  1. Click the Fork button above to fork this gist.
  2. Modify all the files to according to the rules below.
  3. Save your entry and tweet it up!

Keep in mind that thanks to the awesome sensibilities of the GitHub team, gists are just repos. So feel free to clone yours and work locally for a more comfortable environment, and to allow commit messages.

Rules

All entries must exist in an index.js file, whose contents are

  1. an assignable, valid Javascript expression that
  2. contains no more than 140 bytes, and
  3. does not leak to the global scope.

All entries must also be licensed under the WTFPL or equally permissive license.

For more information

See the 140byt.es site for a showcase of entries (built itself using 140-byte entries!), and follow @140bytes on Twitter.

To learn about byte-saving hacks for your own code, or to contribute what you've learned, head to the wiki.

140byt.es is brought to you by Jed Schmidt, with help from Alex Kloss. It was inspired by work from Thomas Fuchs and Dustin Diaz.

"".__proto__.roll = function() { // shortut to String.prototype
for (
r = /(\d+)d(\d+)(.*)/.exec(this), // parse: r[1] = amount, r[2] = dice, r[3] = [adjustment]
x = +r[3]; // initial value = adjustment
r[1]--; // number of rolls
x += 1 + ~~(r[2]*Math.random()) // ~~ = Math.floor
);
return x
}
"".__proto__.roll=function(x,r){for(r=/(\d+)d(\d+)(.*)/.exec(this),x=+r[3];r[1]--;x+=1+~~(r[2]*Math.random()));return x}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2015 Ondrej Zara <ondrej.zara@gmail.com>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "diceRoll",
"description": "Tabletop/RPG dice roller",
"keywords": [
"dice",
"roll",
"game",
"string",
"prototype"
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Roll 1d6+100: <b></b></div>
<div>Roll 5d20-3: <b></b></div>
<script>
"".__proto__.roll=function(x,r){for(r=/(\d+)d(\d+)(.*)/.exec(this),x=+r[3];r[1]--;x+=1+~~(r[2]*Math.random()));return x}
document.querySelectorAll("b")[0].innerHTML = "1d6+100".roll();
document.querySelectorAll("b")[1].innerHTML = "5d20-3".roll();
</script>
@atk
Copy link

atk commented Jul 20, 2015

Interesting string prototype overloading, even though it's not neccessary; but you leak x and r into the global scope, which is against the rules; so a valid submission would be

"".__proto__.roll=function(x,r){for(r=/(\d+)d(\d+)(.*)/.exec(this),x=+r[3];r[1]--;x+=1+~~(r[2]*Math.random()));return x}

or without the prototype overloading:

function(x,r){for(r=/(\d+)d(\d+)(.*)/.exec(x),x=+r[3];r[1]--;x+=1+~~(r[2]*Math.random()));return x}

As you can see, I golfed a few bytes away for you.

@ondras
Copy link
Author

ondras commented Aug 29, 2015

Thanks! Sorry for a late reply, my vacation just ended. Will adjust the code with respect to those leaking globals.

@ondras
Copy link
Author

ondras commented Aug 31, 2015

Updated as per @atk's suggestions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment