Last active
December 26, 2016 18:40
-
-
Save leobetosouza/08359eab209a3fc790eca2219a37248b to your computer and use it in GitHub Desktop.
Look and Say JavaScript
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 lookNsay (n) { | |
const o = { | |
last : null, | |
count : 0, | |
_res : '', | |
get res () { | |
return this._res + this.count + this.last | |
} | |
} | |
return + n.toString().split('').reduce((acc, x) => { | |
if (acc.last === x) { | |
acc.count++ | |
} else { | |
if (acc.last) { | |
acc._res = acc.res | |
} | |
acc.count = 1; | |
acc.last = x | |
} | |
return acc | |
}, o).res | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment