Last active
August 29, 2015 14:15
-
-
Save HyShai/1f63fc8d54fb5bba5056 to your computer and use it in GitHub Desktop.
Solve the Trello dev challenge (https://trello.com/jobs/developer) recursively using ES6 features.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function unhash(hash, str="") { //default parameter values currently only supported by Firefox | |
var base = 7; | |
var multiplier = 37; | |
var letters = "acdegilmnoprstuw"; | |
var index = hash % multiplier; | |
str = letters[index] + str; | |
hash = Math.trunc(hash / multiplier); | |
//Math.trunc is a dumb Math.floor - splits by the decimal point | |
//so works for negative numbers also | |
//(obviously doesn't make a difference here, | |
//but it's recommended over Math.floor to get the quotient) | |
if (hash === base) | |
return str | |
return unhash(hash, str) | |
} | |
unhash(956446786872726) === "trellises" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//this is how babel transpiled the code to ES5 - wow | |
function unhash(_x2) { | |
var _arguments = arguments; | |
_function: while (true) { | |
var hash = _x2; | |
str = base = multiplier = letters = index = undefined; | |
var str = _arguments[1] === undefined ? "" : _arguments[1]; | |
//default parameter values currently only supported by Firefox | |
var base = 7; | |
var multiplier = 37; | |
var letters = "acdegilmnoprstuw"; | |
var index = hash % multiplier; | |
str = letters[index] + str; | |
hash = Math.trunc(hash / multiplier); | |
//Math.trunc is a dumb Math.floor - splits by the decimal point | |
//so works for negative numbers also | |
//(obviously doesn't make a difference here, | |
//but it's recommended over Math.floor to get the quotient) | |
if (hash === base) { | |
return str; | |
}_arguments = [_x2 = hash, str]; | |
continue _function; | |
} | |
} | |
unhash(956446786872726) === "trellises"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment