Skip to content

Instantly share code, notes, and snippets.

@leobetosouza
Last active December 26, 2016 18:40
Show Gist options
  • Save leobetosouza/08359eab209a3fc790eca2219a37248b to your computer and use it in GitHub Desktop.
Save leobetosouza/08359eab209a3fc790eca2219a37248b to your computer and use it in GitHub Desktop.
Look and Say JavaScript
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