Created
July 18, 2018 09:38
-
-
Save lastday154/86dd74289be2187447dc78a2c37f5022 to your computer and use it in GitHub Desktop.
shorten string
This file contains hidden or 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
| <!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