-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (39 loc) · 855 Bytes
/
index.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
const n = 20
function drawHourglass(sandStart) {
let j = n
let sand = 0
for (let i = 0; i < n; i++) {
j -= 1
if (i === 0 || i === n - 1) {
const line = Array(n).fill('#')
console.log(line.join(''))
continue
}
const line = Array(n).fill(' ')
line[0] = '#'
line[n - 1] = '#'
line[i] = '#'
line[j] = '#'
if (sand < (n - 2) / 2 && sandStart === i) {
if (j >= i) {
line.fill('#', i, j)
} else {
line.fill('#', j, i)
}
sand++
sandStart++
}
console.log(line.join(''))
}
}
let sandStart = 1
const timer = setInterval(() => {
if (sandStart > ((n - 2) / 2) + 1) {
console.log('Timer finished')
clearInterval(timer);
return
}
console.log('Timer:', sandStart - 1, ' seconds')
drawHourglass(sandStart)
sandStart++
}, 1000)