3
I’m solving the following coderbyte Problem.com:
Using the Javascript language, 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.
To do the Runlength algorithm, I wrote the following code:
function RunLength(str) {
str = str.split("");
var counter = 1;
for (var i=1; i<=str.length; i++) {
if (str[i] === str[i-1]) {
counter ++;
} else {
str.splice(i-1, counter-1, counter.toString());
counter = 1;
}
}
return str
}
The idea was to splice the counter variable, which counts how many times a letter was repeated, after each of the repeated letters, and remove the occurrences of the repeated letter.
Your question seems to have come truncated.
– user25930
I didn’t get it. How so cracked?
– Kvera
Truncada. "I know counter manipulation is ..." does not have a predicate.
– user25930
Corrected! Thank you.
– Kvera