Skip to content

Commit a3f2928

Browse files
committed
refactor balloon code, shave off ten lines
1 parent ffe9229 commit a3f2928

File tree

1 file changed

+11
-27
lines changed

1 file changed

+11
-27
lines changed

chapter_fifteen_balloon/script.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,19 @@
11
const balloon = document.querySelector('p');
22
balloon.style.fontSize = '16px';
33

4-
function inflate() {
5-
let currentSize = Number(balloon.style.fontSize.match(/\d+/g));
6-
balloon.style.fontSize = `${currentSize + 5}px`;
7-
}
8-
9-
function deflate() {
10-
let currentSize = Number(balloon.style.fontSize.match(/\d+/g));
11-
balloon.style.fontSize = `${currentSize - 5}px`;
12-
}
13-
14-
function explode() {
15-
balloon.innerText = "💥";
16-
}
17-
184
function controlSize(e) {
5+
let size = Number(balloon.style.fontSize.match(/\d+/g));
6+
197
if (e.key == `ArrowUp`) {
20-
inflate();
21-
} else if (e.key = 'ArrowDown') {
22-
deflate();
23-
}}
8+
balloon.style.fontSize = `${size + 5}px`
249

25-
window.addEventListener('keydown', controlSize);
26-
27-
window.addEventListener('keydown', () => {
28-
let currentSize = Number(balloon.style.fontSize.match(/\d+/g))
29-
if (currentSize > 240) {
30-
explode();
31-
window.removeEventListener('keydown', controlSize);
10+
if (size > 340) {
11+
balloon.innerText = "💥";
12+
window.removeEventListener('keydown', controlSize);
13+
}
14+
} else if (e.key = 'ArrowDown') {
15+
balloon.style.fontSize = `${size - 5}px`;
3216
}
33-
})
34-
17+
}
3518

19+
window.addEventListener('keydown', controlSize);

0 commit comments

Comments
 (0)