Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created July 18, 2018 09:38
Show Gist options
  • Select an option

  • Save lastday154/86dd74289be2187447dc78a2c37f5022 to your computer and use it in GitHub Desktop.

Select an option

Save lastday154/86dd74289be2187447dc78a2c37f5022 to your computer and use it in GitHub Desktop.
shorten string
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
function shorten(s) {
const len = s.length;
let res = '';
for(let i=0; i < len; i++ ) {
let count = 1;
let chars = '';
while(s[i] == s[i+1]) {
chars = s[i];
count++;
i++;
}
if (count == 1) {
chars = s[i];
}
res += count;
res += chars;
}
return res;
}
console.log(shorten("3114"));
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment