다음 문자열을 포함하는 JavaScript 함수를 작성해야 합니다 -
const str = 'This string will be used to calculate frequency distribution';
배열에 있는 다양한 요소의 빈도 분포를 나타내는 개체를 반환해야 합니다.
예시
다음은 코드입니다 -
const str = 'This string will be used to calculate frequency
distribution';
const frequencyDistribution = str => {
const map = {};
for(let i = 0; i < str.length; i++){
map[str[i]] = (map[str[i]] || 0) + 1;
};
return map;
};
console.log(frequencyDistribution(str)); 출력
다음은 콘솔의 출력입니다 -
{
T: 1,
h: 1,
i: 6,
s: 4,
' ': 8,
t: 5,
r: 3,
n: 3,
g: 1,
w: 1,
l: 4,
b: 2,
e: 5,
u: 4,
d: 2,
o: 2,
c: 3,
a: 2,
f: 1,
q: 1,
y: 1
}