I want to write a function to compress the characters of a string such as 'bbbab' would be converted to 'b3ab' and I'm getting stuck
it's only printing "b3"
here's the code so far
let string = 'bbbab';
let letters = string.split("");
let currentAlphabetLetter = letters[0];
let count = 0;
let newString = [];
string.split("").forEach(letter => {
if (letter === currentAlphabetLetter) {
count++;
} else {
if (count > 0) {
newString.push(`${currentAlphabetLetter}${count}`);
} else {
newString.push(`${letter}`);
}
count = 0;
currentAlphabetLetter = letter;
}
})
console.log(newString);