Skip to content

Commit a1fca7c

Browse files
committed
7.1
1 parent bddace0 commit a1fca7c

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class ColorConsole {
2+
log() {}
3+
}
4+
5+
class RedConsole extends ColorConsole {
6+
log(text) {
7+
console.log(`\x1b[31m${text}\x1b[0m`);
8+
}
9+
}
10+
11+
class BlueConsole extends ColorConsole {
12+
log(text) {
13+
console.log(`\x1b[34m${text}\x1b[0m`);
14+
}
15+
}
16+
17+
class GreenConsole extends ColorConsole {
18+
log(text) {
19+
console.log(`\x1b[32m${text}\x1b[0m`);
20+
}
21+
}
22+
23+
export function consoleColorFactory(color) {
24+
switch (color) {
25+
case 'red':
26+
return new RedConsole();
27+
case 'blue':
28+
return new BlueConsole();
29+
case 'green':
30+
return new GreenConsole();
31+
default:
32+
throw new Error(`Invalid color: "${color}"`);
33+
}
34+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { consoleColorFactory } from "./consoleColorFactory.mjs";
2+
3+
((colors) => {
4+
for (let color of colors) {
5+
try {
6+
consoleColorFactory(color).log(`Hello, ${color}!`);
7+
} catch(e) {
8+
console.log(e.message);
9+
}
10+
}
11+
})(['red', 'blue', 'green', 'invalid', ,]);

0 commit comments

Comments
 (0)