{
const str = 'BBBYHKIIIPPDDDDDDXXWWWWWWXADFGJJJJ';
const countRepeat = str =>
[...str].reduce((acc, char, idx, self) => {
if (char === self[idx + 1]) {
acc.repeat += 1
} else {
acc.result += `${self[!acc.repeat ? idx : idx - 1]}${!acc.repeat ? '' : acc.repeat}`
acc.repeat = 0
}
return acc
}, { repeat: 0, result: ''}).result
console.log(countRepeat(str))
// B2YHKI2P1D5X1W5XADFGJ3