DevYoon

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ (Javascript) ๋ณธ๋ฌธ

PS/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์„ฑ๊ฒฉ ์œ ํ˜• ๊ฒ€์‚ฌ (Javascript)

gimewn 2022. 9. 14. 19:04

link ๐Ÿ”— https://school.programmers.co.kr/learn/courses/30/lessons/118666

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

โœ๏ธ ์„ฑ๊ฒฉ ํƒ€์ž…์„ ๊ฐ์ฒด๋กœ ์ƒ์„ฑํ•˜๊ณ , ์„ค๋ฌธ ๊ฒฐ๊ณผ์— ๋”ฐ๋ฅธ ์ ์ˆ˜๋ฅผ ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ๋”ํ•ด์ฃผ์—ˆ๋‹ค.

โœ๏ธ Object.entries()๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด์˜ key, value๋ฅผ ๋ฐฐ์—ด๋กœ ์ „ํ™˜ํ•˜๊ณ , 2๊ฐœ์”ฉ ๋‚˜๋ˆ„์–ด ๋น„๊ตํ•ด์ฃผ์—ˆ๋‹ค.

 

function solution(survey, choices) {
    var answer = '';
    personal = {
        'R':0,
        'T':0,
        'C':0,
        'F':0,
        'J':0,
        'M':0,
        'A':0,
        'N':0,
    };
    
    score = [0, 3, 2, 1, 0, 1, 2, 3]; // ๋งค์šฐ ๋น„๋™์˜ ~ ๋งค์šฐ ๋™์˜
    
    survey.map((type, index) => {
        // choices์˜ ๊ฐ’์ด 4๋ณด๋‹ค ์ž‘์œผ๋ฉด => ์˜ˆ๋ฅผ ๋“ค์–ด, RT ์ค‘ R์— ํ”Œ๋Ÿฌ์Šค
        if(choices[index] < 4){ 
            personal[type[0]] += score[choices[index]]
        }else{
            // choices์˜ ๊ฐ’์ด 4๋ณด๋‹ค ํฌ๋ฉด๋ฉด => ์˜ˆ๋ฅผ ๋“ค์–ด, RT ์ค‘ T์— ํ”Œ๋Ÿฌ์Šค
            personal[type[1]] += score[choices[index]]
        }
    });
    
    // personal์„ key, value ๋ฐฐ์—ด๋กœ ์ „ํ™˜
    result = Object.entries(personal);
    
    
    for (let i = 1; i<8; i+=2){
        // 2๊ฐœ์”ฉ ๋‚˜๋ˆ ์„œ ๋น„๊ต, ๋” ๊ฐ’์ด ํฐ ์ชฝ์„ answer์— ์ถ”๊ฐ€
        if(result[i][1] > result[i-1][1]){
            answer += result[i][0]
            
        }else{
            // ๋งŒ์•ฝ ๊ฐ’์ด ๊ฐ™๋‹ค๋ฉด, ์‚ฌ์ „์ƒ ์•ž์— ์˜ค๋Š” ์ชฝ์ด ์ถ”๊ฐ€๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ i-1์„ answer์— ์ถ”๊ฐ€
            answer += result[i-1][0]
        }
    };
    return answer;
}