Skip to content

Instantly share code, notes, and snippets.

@nijjwal
Last active December 1, 2023 10:28
Show Gist options
  • Save nijjwal/8eefb52ef639e7c4dfd9 to your computer and use it in GitHub Desktop.
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.
<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
Copy link

``

@Shubham7211
Copy link

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