-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.js
52 lines (46 loc) · 1.31 KB
/
main.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
import { jonghun } from "./jonghun.js";
import { junGi } from "./jungi.js";
import { KJY } from "./KJY.js";
import { soohwan } from "./soohwan.js";
import { taehoon } from "./태훈.js";
function myNameVS(teamNames, teamFunctions) {
const counts = {};
let stop = false;
teamNames.forEach((name) => (counts[name] = 0));
const stopSignal = () => stop;
setTimeout(() => {
stop = true;
}, 5000);
const promises = teamFunctions.map((func, index) => {
return new Promise((resolve) => {
try {
func(() => {
if (!stop) {
counts[teamNames[index]]++;
if (counts[teamNames[index]] % 100 === 0) {
setTimeout(() => {}, 1);
}
}
}, stopSignal);
} catch (e) {
console.error(
`${teamNames[index]} 님의 함수에서 에러가 발생했습니다:`,
e,
);
} finally {
setTimeout(() => resolve(), 5000);
}
});
});
Promise.all(promises).then(() => {
teamNames.forEach((name) => {
console.log(`${name} 님은 ${counts[name]}번 이름을 출력했습니다.`);
});
``;
const winner = teamNames.reduce((prev, current) =>
counts[prev] > counts[current] ? prev : current,
);
console.log(`승자는 ${winner} 님입니다!`);
});
}
myNameVS(["김지연", "김태훈", "박수환", "전준기", "최종훈"], [KJY, taehoon, soohwan, junGi, jonghun]);