Skip to content

Instantly share code, notes, and snippets.

@QuatoHub
Created September 6, 2021 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuatoHub/f5e70c9cde6b86defcf4f4bcb75afcc8 to your computer and use it in GitHub Desktop.
Save QuatoHub/f5e70c9cde6b86defcf4f4bcb75afcc8 to your computer and use it in GitHub Desktop.
function countAllLetters(str) {
let obj = {}; // 변수에 빈 객체 할당
for(let i = 0; i < str.length; i++){ // 반복문을 통해 인자로 받은 문자열 순회
if (obj[str[i]] === undefined) { // i번째 문자가 기존 객체의 키로 존재하지 않으면
obj[str[i]] = 0 // i번째 문자와 0을 새로운 키값으로 생성
}
obj[str[i]]++ // 해당 문자가 반복될 때 마다 1씩 값을 추가한다.
}
return obj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment