채팅방에 들어오고 나가거나, 닉네임을 변경한 기록이 담긴 문자열 배열 record가 매개변수로 주어질 때, 모든 기록이 처리된 후, 최종적으로 방을 개설한 사람이 보게 되는 메시지를 문자열 배열 형태로 return 하도록 solution 함수를 완성하라.
function solution(record) {
const answer = [];
const user = {};
// user 객체에 uid를 key값으로 두고 value(닉네임)을 계속해서 update하는 형식으로 구현
record.forEach((e) => {
const split = e.split(' ');
if (split[0] !== 'Leave') {user[split[1]] = split[2];}
else return;
});
// swtich문을 통해서 반환문 추출
record.forEach((e) => {
const split = e.split(' ');
switch (split[0]) {
case 'Enter':
return answer.push(`${user[split[1]]}님이 들어왔습니다.`);
case 'Leave':
return answer.push(`${user[split[1]]}님이 나갔습니다.`);
default:
break;
}
});
return answer;
}
const recordObj = record.reduce((acc, cur, i) => {
const split = cur.split(' ');
acc[split[1]] = split[2];
return acc;
}, {});
function solution(record) {
var answer = [];
const users = {}
record.map(history => {
const [action, id, name] = history.split(' ')
if(action !== 'Leave') users[id] = name
})
record.map(history => {
const [action, id, name] = history.split(' ')
if(action === 'Enter') answer.push(`${users[id]}님이 들어왔습니다.`)
if(action === 'Leave') answer.push(`${users[id]}님이 나갔습니다.`)
})
return answer;
}