-
Notifications
You must be signed in to change notification settings - Fork 0
/
38-runLength.js
27 lines (26 loc) · 1 KB
/
38-runLength.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//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 arr = [];
var count = 1;
//loop through each letter in string
for(var i = 0; i < str.length; i++){
//if current character is equal to next character, add 1 to count
if(str[i] === str[i+1]){
count++;
}
//else if current character is not equal to next character
else if(str[i] !== str[i+1]){
//push count to arr
arr.push(count);
//reset count to 1
count = 1;
//push current character to arr
arr.push(str[i]);
}
}
//join arr to string and return it
return arr.join("");
}