-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjs3.js
87 lines (69 loc) · 2.11 KB
/
js3.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
function zoo([...params]) {
const zoo = {}
const areas = {}
let a = 5
let b = a++
let c = ++a
let arr = ['z', 's', 'a', 'd']
arr.sort((a,b) => b.localeCompare(a))
console.log(arr)
for (command of params) {
const [type, instruction] = command.split(': ')
if (type === 'EndDay') {
break
}
data = instruction.split('-')
if (type === 'Add') {
let name = data[0]
let amount = Number(data[1])
let area = data[2]
if (areas[area]) {
if (!areas[area].includes(name)) {
areas[area].push(name)
}
} else {
areas[area] = [name]
}
if (zoo[name]) {
zoo[name] += amount
} else {
zoo[name] = amount
}
}
if (type === 'Feed') {
let name = data[0]
let amount = Number(data[1])
if (zoo[name]) {
zoo[name] -= amount
}
if (zoo[name] <= 0) {
for (zone of Object.entries(areas)) {
if (areas[zone[0]].includes(name)) {
areas[zone[0]] = areas[zone[0]].filter(member => member !== name)
if (areas[zone[0]].length === 0) {
delete areas[zone[0]]
}
}
}
delete zoo[name]
console.log(`${name} was successfully fed`)
}
}
}
console.log('Animals:')
for (list of Object.entries(zoo)) {
console.log(` ${list[0]} -> ${list[1]}g`)
}
// console.log(Object.entries(zoo)?.map(list => console.log(` ${list[0]} ->${list[1]}g`)))
console.log('Areas with hungry animals:')
for (area of Object.entries(areas)) {
console.log(` ${area[0]}: ${area[1].length}`)
}
}
zoo(
(["Add: Adam-4500-ByTheCreek",
"Add: Maya-7600-WaterfallArea",
"Add: Maya-1230-WaterfallArea",
"Feed: Jamie-2000",
"EndDay"])
)