-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.js
More file actions
69 lines (62 loc) · 1.81 KB
/
App.js
File metadata and controls
69 lines (62 loc) · 1.81 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
import { Console } from "@woowacourse/mission-utils";
import { MissionUtils } from "@woowacourse/mission-utils";
class InputError extends Error {
constructor(message) {
super(message);
this.name = "[ERROR]";
}
}
class App {
async play() {
const names = await this.getCarname();
const times = await this.getTimes();
const race = Array(names.length).fill(0);
let max = 0;
Console.print("\n실행 결과");
for (let i = 0; i < times; i++) {
names.forEach((name, index, array) => {
race[index] =
MissionUtils.Random.pickNumberInRange(0, 9) >= 4
? race[index] + 1
: race[index];
max = race[index] > max ? race[index] : max;
Console.print(`${name} : ${"-".repeat(race[index])}`);
});
Console.print("");
}
Console.print(
`최종 우승자 : ${names
.filter((name) => race[names.indexOf(name)] >= max)
.join(", ")}`
);
}
async getCarname() {
try {
const input = await Console.readLineAsync(
"경주할 자동차 이름을 입력하세요.(이름은 쉼표(,) 기준으로 구분)\n"
);
const names = Array.from(input.split(",").map((name) => name.trim()));
if (names.length < 2) {
throw new InputError(
"이름을 올바르게 입력해 주세요. (쉼표로 구분, 2개 이상의 이름)\n"
);
}
return names;
} catch (error) {
throw error;
}
}
async getTimes() {
try {
const input = await Console.readLineAsync("시도할 횟수는 몇 회인가요?\n");
const times = parseInt(input);
if (isNaN(input)) {
throw new InputError("[ERROR] 숫자가 잘못된 형식입니다.");
}
return times;
} catch (error) {
throw error;
}
}
}
export default App;