-
Notifications
You must be signed in to change notification settings - Fork 0
/
오픈채팅방.js
57 lines (44 loc) · 1.27 KB
/
오픈채팅방.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Solution 1
// 일반 객체를 활용한 솔루션
function solution1(record) {
const result = [];
const splittedRecord = record.map((log) => log.split(' '));
const users = {};
splittedRecord.forEach((log) => {
const [action, id, nickName] = log;
if (action === 'Enter' || action === 'Change') {
users[id] = nickName;
}
});
splittedRecord.forEach((log) => {
const [action, id] = log;
if (action === 'Enter') {
result.push(`${users[id]}님이 들어왔습니다.`);
} else if (action === 'Leave') {
result.push(`${users[id]}님이 나갔습니다.`);
}
});
return result;
}
// Solution 2
// Map 객체를 활용한 솔루션
function solution2(record) {
const result = [];
const splittedRecord = record.map((log) => log.split(' '));
const users = new Map();
splittedRecord.forEach((log) => {
const [action, id, nickName] = log;
if (action === 'Enter' || action === 'Change') {
users.set(id, nickName);
}
});
splittedRecord.forEach((log) => {
const [action, id] = log;
if (action === 'Enter') {
result.push(`${users.get(id)}님이 들어왔습니다.`);
} else if (action === 'Leave') {
result.push(`${users.get(id)}님이 나갔습니다.`);
}
});
return result;
}