Notice
Recent Posts
Recent Comments
Link
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
Archives
Today
Total
관리 메뉴

Kimyeongkyung

[2023.11.29] 가위 바위 보 본문

[JS] 프로그래머스 코딩테스트 Lv.0

[2023.11.29] 가위 바위 보

yeongk0825 2023. 11. 29. 02:08

가위는 2 바위는 0 보는 5로 표현합니다. 가위 바위 보를 내는 순서대로 나타낸 문자열 rsp가 매개변수로 주어질 때, rsp에 저장된 가위 바위 보를 모두 이기는 경우를 순서대로 나타낸 문자열을 return하도록 solution 함수를 완성해보세요.

 

제한사항

  • 0 < rsp의 길이 ≤ 100
  • rsp와 길이가 같은 문자열을 return 합니다.
  • rsp는 숫자 0, 2, 5로 이루어져 있습니다.

 

답안
const solution = (rsp) => {
    const slice = rsp.split('');
    const resultArray = [];
    
    slice.map((item)=>{
    //문자열을 숫자로 변환(10진수)
    const num = parseInt(item, 10);
        if(num === 2){
            resultArray.push('0');
        }
        else if(num === 0){
            resultArray.push('5');
        }
        else if(num === 5){
          resultArray.push('2');
        }
    });
    
    return resultArray.join('');
}​