몸과 마음이 건전한 SW 개발자

프로그래머스 [Lv. 0] 문자 개수 세기 {언어 : JavaScript} 본문

개발 언어 입문/자바스크립트

프로그래머스 [Lv. 0] 문자 개수 세기 {언어 : JavaScript}

스위태니 2024. 3. 7. 14:38

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/181902

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

정답 코드

function solution(my_string) {
    const answer = Array(52).fill(0);
    // console.log("A".charCodeAt()) // 65
    // const p1 = String.fromCharCode(65, 66, 67) // ABC
    // console.log("A".codePointAt()) // 65
    // console.log("a".codePointAt()) // 97
    my_string.split("").forEach((e) => {
        const codeP = e.codePointAt();
        if (codeP >= 97) {
            answer[codeP-71] += 1
        } else {
            answer[codeP-65] += 1
        }
    })
    return answer;
}

배운점

  • "문자".charCodeAt()
  • "문자".codePointAt()
  • "문자열".fromCharCode(아스키코드 숫자)