Skip to content

Instantly share code, notes, and snippets.

@sergueyarellano
Created June 23, 2016 07:42
Show Gist options
  • Save sergueyarellano/602d9ce1a2c230f992898114bf8301b4 to your computer and use it in GitHub Desktop.
Save sergueyarellano/602d9ce1a2c230f992898114bf8301b4 to your computer and use it in GitHub Desktop.
function compressString (str) {
return (function compressAux(str, index, acc) {
return index > str.length -1 ?
'' :
str.charAt(index) === str.charAt(index + 1) ?
compressAux(str, ++index, ++acc) :
str.charAt(index) + (acc !== 1 ? acc : '') + compressAux(str, ++index, 1);
})(str, 0, 1);
}
compress("aaabbcccd"); // "a3b2c3d"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment