알고리즘/JavaScript | AG
[객체] 각 키의 값은 해당 문자가 주어진 문자열에서 몇번 나오는가?
화이트그래머
2020. 6. 18. 14:55
문제설명
문자열이 주어졌을때, "countAllCharacters" 함수는 주어진 문자열의 각각의 문자를 키로 가지는 객체를 반환합니다. 각 키의 값은 해당 문자가 주어진 문자열에서 몇번 나오는지를 나타냅니다.
조건
- 만약 빈 문자열이 주어졌다면, "countAllCharacters" 함수는 빈 객체를 반환해야 합니다.
입출력예시
let output = countAllCharacters('banana');
console.log(output); // --> {b: 1, a: 3, n: 2}
문제
function countAllCharacters(str) {
}
*풀이
function countAllCharacters(str) {
let obj = {};
for(let i = 0; i < str.length; i++){
if(obj[str[i]] === undefined){
obj[str[i]] = 0;
}
obj[str[i]]++;
}
obj result;
}