function solution(record) {
const action = [];
const obj = {};
for (const str of record) {
const tmp = str.split(" ");
if (tmp[2]) {
obj[tmp[1]] = tmp[2];
}
if (tmp[0] === "Enter" || tmp[0] === "Leave") {
action.push(tmp);
}
if (tmp[0] === "Chage") {
obj[tmp[1]] = tmp[2];
}
}
const answer = [];
for (let [command, userId] of action) {
if (command === "Enter") {
answer.push(`${obj[userId]}님이 들어왔습니다.`);
} else {
answer.push(`${obj[userId]}님이 나갔습니다.`);
}
}
return answer;
}
간단한 구현 문제이다.
입력받은 문자열을 split으로 나눈다.
객체에 아이디에 따른 닉네임 즉, 데이터를 저장해둔다.
행동과 관련된 command(enter, leave)는 다른 배열에 따로 저장해둔 뒤 나중에 구현해준다.