Может как-то так?
function orderedCount2(str) {
const symbols = new Map();
for (const c of str) {
const currCount = symbols.get(c) || 0;
symbols.set(c, currCount + 1);
}
const totalCount = str.length;
const orderedSymbols = {};
for (const [symbol, count] of symbols.entries()) {
orderedSymbols[symbol] = (count / totalCount) * 100;
}
return orderedSymbols;
}
input:
orderedCount("hello world")
output
{
h: 9.090909090909092,
e: 9.090909090909092,
l: 27.27272727272727,
o: 18.181818181818183,
' ': 9.090909090909092,
w: 9.090909090909092,
r: 9.090909090909092,
d: 9.090909090909092
}