const s = "aaabbadddff";
const getCount = s => {
if (s !== "") {
let result = "";
let tmp = s[0];
let count = 0;
for (let i = 0; i < s.length; i++) {
if (s[i] === tmp) {
count++;
} else {
result += `${tmp}${count}`;
tmp = s[i];
count = 1;
}
}
result += `${tmp}${count}`;
return result;
}
return "";
};
console.log(getCount(s));
const cstr = "a3b2a1d3f2";
const getChar = (s, index) => {
if (s === "") {
return "";
}
let sum = 0;
let j = 0;
for (let i = 0; i < cstr.length / 2; i++) {
sum += Number(cstr[i * 2 + 1]);
if (sum >= index + 1) {
j = i;
break;
}
}
if (sum < index + 1) {
return "";
}
return s[j * 2];
};
console.log(getChar(cstr, 0));