프로그래머스 LEVEL 2(모음 사전)

image

  • 사용 언어 : javascript

  • 해결 날짜 : 2022-09-30

  • 해결 방법 :
    • 각 모음 별 곱해야 할 숫자와, 자릿수 별 곱할 값을 정의
    • word를 돌며 현재 char의 곱해야 할 숫자 * 현재 자릿수의 곱해야할 값 + 1을 answer에 더함
  • 회고 :
    • 처음에 자릿수 별 곱할 값이 [625, 125, 25, 5, 1]로 생각함
    • 5자리가 고정이 아니므로 자릿수 별 곱할 값은 625 + 125 + 25 + 5 + 1, 125 + 25 + 5 + 1, 25 + 5 + 1, 5 + 1, 1로 [781, 156, 31, 6, 1]임
  • 코드

    function solution(word) {
        const alpha = {'A': 0, 'E': 1, 'I': 2, 'O': 3, 'U': 4};
        const multiply_value = [781, 156, 31, 6, 1];
        let answer = 0;
        for (let i = 0; i < word.length; i++) {
            const current = word.charAt(i);
            const value = alpha[current];
            answer += multiply_value[i] * value + 1;
        }        
        return answer;
    }
    
  • 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges