-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
139 lines (122 loc) · 4.57 KB
/
app.js
File metadata and controls
139 lines (122 loc) · 4.57 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 💾DATA ------------------------------------------------------------
let animals = [
{
name: 'Ozzy',
picture: '🦇',
location: '🌲forest'
},
{
name: 'Oslo',
picture: '🦧',
location: '🌲forest'
},
{
name: 'Dr. Strange',
picture: '🦒',
location: '🌵desert'
},
{
name: 'Tiny',
picture: '🦏',
location: '💦water'
},
{
name: 'Scampi',
picture: '🦐',
location: '💦water'
},
{
name: 'Kowalski',
picture: '🐧',
location: '⛄arctic'
},
{
name: 'Thud',
picture: '🦃',
location: '🌋mtdoom'
},
]
// 🧠⚡ LOGIC (functions that change data)---------------------------------
function calculateAnimalCountByLocation(desiredLocation) {
console.log('🧮', desiredLocation)
// debugger
let animalCount = 0
animals.forEach((animal) => {
// debugger
if (animal.location == desiredLocation) {
console.log('in ', animal)
animalCount += 1
}
})
console.log('🔢', animalCount);
return animalCount // return gives a *result* BACK to the function "caller"
}
function PARTY() {
let partyLocations = ['🌲forest', '💦water', '🌋mtdoom', '🌵desert', '⛄arctic']
animals.forEach((animal) => {
let randomPosition = Math.floor(Math.random() * 5)
animal.location = partyLocations[randomPosition]
})
drawAnimalsForLocation('🌲forest')
drawAnimalsForLocation('🌵desert')
drawAnimalsForLocation('💦water')
drawAnimalsForLocation('⛄arctic')
drawAnimalsForLocation('🌋mtdoom')
}
// 🎨🖌️ VISUALIZATIONS (functions that "draw" data to the screen)--------
let forestAnimalsElement = document.getElementById('🌲forest-animals')
let desertAnimalsElement = document.getElementById('🌵desert-animals')
let waterAnimalsElement = document.getElementById('💦water-animals')
let arcticAnimalsElement = document.getElementById('⛄arctic-animals')
function drawForestAnimals() {
// create a "sub" array of animals that only have "forest" animals
let forestAnimals = animals.filter((animal) => animal.location === '🌲forest')
console.log('🌲', forestAnimals);
// iterate over the sub array and draw it to the page
forestAnimals.forEach((animal) => {
console.log(animal.picture)
forestAnimalsElement.innerHTML += `<span title="${animal.name}">${animal.picture}</span>`
})
// NOTE this doesn't work well with arrays that may change in size
// forestAnimalsElement.innerHTML += `<span>${forestAnimals[0].name}</span>`
// forestAnimalsElement.innerHTML += `<span>${forestAnimals[1].name}</span>`
}
function drawDesertAnimals() {
// step 1 - get the animals in the desert
let desertAnimals = animals.filter((animal) => animal.location === '🌵desert')
console.log('🌵', desertAnimals)
// step 2 - iterate over desert animals
desertAnimals.forEach((animal) => {
console.log(animal.picture)
desertAnimalsElement.innerHTML += `<span title="${animal.name}">${animal.picture}</span>`
})
// step 2.5 - inside loop, add animals to page
}
// desiredLocation will be one of the locations are animals are at, this will be passed when the function runs = drawAnimalsForLocation('🌲forest') - draws the forest animals to the forest element
function drawAnimalsForLocation(desiredLocation) {
console.log('🖌️ drawing for locations', desiredLocation)
let desiredAnimals = animals.filter((animal) => animal.location == desiredLocation)
console.log('✈️', desiredAnimals)
console.log('getting element:', desiredLocation + '-animals')
// NOTE we don't know which element we want to draw into when defining the function so we have to get it when the function runs
let desiredElement = document.getElementById(desiredLocation + '-animals')
let countElement = document.getElementById(desiredLocation + '-count')
// debugger
desiredElement.innerHTML = '' // dont forget to empty the element before you draw into it
desiredAnimals.forEach((animal) => {
console.log(animal.picture)
desiredElement.innerHTML += `<span title="${animal.name}">${animal.picture}</span>`
})
// debugger
// taking the desiredLocation from our own parameter and passing it along to out calculate function
// we then capture the *return* result from our calculate and put it on the page
let animalCount = calculateAnimalCountByLocation(desiredLocation)
countElement.innerText = `${animalCount}`
}
// drawForestAnimals()
// drawDesertAnimals()
drawAnimalsForLocation('🌲forest')
drawAnimalsForLocation('🌵desert')
drawAnimalsForLocation('💦water')
drawAnimalsForLocation('⛄arctic')
drawAnimalsForLocation('🌋mtdoom')