Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- /**
- * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
- * @param {string} value
- * @return {string}
- */
- function rle(value) {
- const result = [(value.length > 0 ? value[0] : '')];
- for(let i = 1; i <= value.length; i++) {
- if (result[result.length - 1] != value[i]) {
- if (i - result.length + 1 > 1) {
- result[result.length - 1] = result[result.length - 1] + (i - result.length + 1);
- }
- result[i] = value[i];
- }
- }
- return result.join('');
- }
- console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">/**
- * Реализовать RLE-сжатие: AAAB -> A3B, BCCDDDAXXXX -> BC2D3AX4
- * @param {string} value
- * @return {string}
- */
- function rle(value) {
- const result = [(value.length > 0 ? value[0] : '')];
- for(let i = 1; i <= value.length; i++) {
- if (result[result.length - 1] != value[i]) {
- if (i - result.length + 1 > 1) {
- result[result.length - 1] = result[result.length - 1] + (i - result.length + 1);
- }
- result[i] = value[i];
- }
- }
- return result.join('');
- }
- console.log(rle('AVVVBBBVVXDHJFFFFDDDDDDHAAAAJJJDDSLSSSDDDD'));
- </script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement