Notice
Recent Posts
Recent Comments
«   2024/05   »
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 31
Archives
Today
Total
관리 메뉴

코린이 탈출기

[2019 KAKAO BLIND RECRUITMENT][JAVASCRIPT] 오픈채팅방 본문

문제 풀이

[2019 KAKAO BLIND RECRUITMENT][JAVASCRIPT] 오픈채팅방

명란파스타 2020. 9. 5. 14:38

문제 바로가기

 

코딩테스트 연습 - 오픈채팅방

오픈채팅방 카카오톡 오픈채팅방에서는 친구가 아닌 사람들과 대화를 할 수 있는데, 본래 닉네임이 아닌 가상의 닉네임을 사용하여 채팅방에 들어갈 수 있다. 신입사원인 김크루는 카카오톡 오

programmers.co.kr

 

<문제 Logic>

1. record_str에 각 record를 space bar로 parsing한 배열을 저장한다.

2. user 배열에는 id를 key값으로 가지고 name을 value 값으로 가지는 object를 저장한다.

3. new_condition에는 id와 condition("Enter"시 "들어왔습니다.", "Leave"시 "나갔습니다." 저장)을 순서대로 넣어준다.

4. "Enter"나 "Change"시 user 배열 변경.

5. 모든 record를 다 읽은 후에 new_condition 배열을 순회하면서 id 값을 user의 key값으로 하는 value, 즉 현재 사용자의 이름을 가져온다. 이 이름과 new_condition의 condition을 합쳐주면 최종 answer가 완성된다.

 

1. user       2. new_condition

 

 

 

 

 

 

 

 

 

 

 

 

 

-전체 코드-

<!DOCTYPE html>

<body>
    <script>
        function solution(record) {
            var answer = [];
            var condition_str = [];
            var user = [];

            for (var i = 0; i < record.length; i++) {
                var record_str = record[i].split(" ");
                console.log(record_str);

                switch (record_str[0]) {
                    case "Enter":
                        user[record_str[1]] = record_str[2];
                        var new_condition = new Object();
                        new_condition.id = record_str[1];
                        new_condition.condition = "들어왔습니다.";
                        condition_str.push(new_condition);
                        break;
                    case "Leave":
                        var new_condition = new Object();
                        new_condition.id = record_str[1];
                        new_condition.condition = "나갔습니다.";
                        condition_str.push(new_condition);
                        break;
                    case "Change":
                        user[record_str[1]] = record_str[2];
                        break;
                }
            }
            // console.log(user);
            // console.log(condition_str);

            for (var i = 0; i < condition_str.length; i++) {

                var new_str = "";
                new_str += user[condition_str[i]["id"]];
                new_str += "님이 ";
                new_str += condition_str[i]['condition'];
            }

            //console.log(answer);
            return answer;
        }

        var record = ["Enter uid1234 Muzi", "Enter uid4567 Prodo", "Leave uid1234", "Enter uid1234 Prodo", "Change uid4567 Ryan"];
        solution(record);

    </script>
</body>