풀이:
function solution(record) {
let answer = [];
let user = {};
for (let i=0; i<record.length; i++) {
let status = record[i].split(" ");
if (status[0] === "Enter" || status[0] === "Change") {
user[status[1]] = status[2];
}
}
for (let i=0; i<record.length; i++) {
let status = record[i].split(" ");
if (status[0] === "Enter") {
answer.push(`${user[status[1]]}님이 들어왔습니다.`);
}
else if (status[0] === "Leave") {
answer.push(`${user[status[1]]}님이 나갔습니다.`);
}
}
return answer;
}
유저라는 객체에다가 유저 아이디와 닉네임을 담을곳을 만듬.
받은 길이많큼 도는 반복문에, 받은 record 배열을 하나하나 자르고 status 에다가 저장.
만약 나갔다가 닉네임을 바꾸고 들어오면 다시 갱신을 해야되서, 갱신해줌.
다 알맞게 닉네임을 최신상태로 바꿔준후, 이제 들어오거나 나가면 그렇다라고 문자열을 answer 에 차례대로 넣어줌.
그리고 answer 라는 배열 출력.
다른사람 풀이:
function solution(record) {
let ret = []
const uids = new Map()
record.forEach(entry => {
let [command, uid, nick] = entry.split(' ')
if (command === 'Enter' || command === 'Change') uids.set(uid, nick)
})
record.forEach(entry => {
let [command, uid] = entry.split(' ')
if (command === 'Enter') ret.push(`${uids.get(uid)}님이 들어왔습니다.`)
if (command === 'Leave') ret.push(`${uids.get(uid)}님이 나갔습니다.`)
})
return ret
}