Last active
December 1, 2023 10:28
-
-
Save nijjwal/8eefb52ef639e7c4dfd9 to your computer and use it in GitHub Desktop.
For this challenge you will determine the Run Length Encoding of a 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
<script> | |
/* | |
Have the function RunLength(str) take the str parameter being passed | |
and return a compressed version of the string using the Run-length | |
encoding algorithm. This algorithm works by taking the occurrence of | |
each repeating character and outputting that number along with a single | |
character of the repeating sequence. For example: "wwwggopp" would return | |
3w2g1o2p. The string will not contain any numbers, punctuation, or symbols. | |
*/ | |
function RunLength(str) { | |
var ctr=0, | |
output='', | |
data = str[0]; | |
for(var i=0; i<str.length; i++) | |
{ | |
if(str[i] == data) | |
{ | |
ctr++; | |
} | |
else | |
{ | |
output += ctr+data; | |
data = str[i]; | |
ctr = 1; | |
} | |
} | |
output += ctr+data; | |
return output; | |
} | |
</script> |
jaga2507
commented
May 24, 2022
error in this code : empty character literal at line number 13
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment