-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
329 lines (239 loc) · 8.62 KB
/
app.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
const music = {
currentTrack: 'Just Ken',
volume: 70, // <-----
};
music.currentPlaylist = ['Just Ken', 'Hey Blondie', 'What Was I Made For', 'Dance The Night'];
console.log(music.currentPlaylist) // ['Just Ken', 'Hey Blondie', 'What Was I Made For', 'Dance The Night']
console.log(music.currentTrack);
music.liked = ['yes']
console.log(music);
*/
const pokemon = require('./data.js')
const game = {
party: [],
gyms: [
{ location: "Pewter City", completed: false, difficulty: 1 },
{ location: "Cerulean City", completed: false, difficulty: 2 },
{ location: "Vermilion City", completed: false, difficulty: 3 },
{ location: "Celadon City", completed: false, difficulty: 4 },
{ location: "Fuchsia City", completed: false, difficulty: 5 },
{ location: "Saffron City", completed: false, difficulty: 6 },
{ location: "Cinnabar Island", completed: false, difficulty: 7 },
{ location: "Viridian City", completed: false, difficulty: 8 },
],
items: [
{ name: "potion", quantity: 4 },
{ name: "pokeball", quantity: 8 },
{ name: "rare candy", quantity: 99 },
],
}
//console.dir(pokemon, { maxArrayLength: null })
// console.dir(pokemon, { maxArrayLength: null })
console.log('Exercise 2:',pokemon[58].name)
//console.log(game)
/*
Exercise 3
1. Add a new property to the `game` object. Let's call it "difficulty".
2. Choose a value for "difficulty" that you think fits the game. Ex: "Easy", "Med" or "Hard". How would you assign it?
Solve Exercise 3 here:
*/
game.difficulty = ('Med')
console.log('Exercise 3:',game.difficulty);
/*
Exercise 4
1. Select a starter Pokémon from the `pokemon` array. Remember, a starter Pokémon's `starter` property is true.
2. Add this Pokémon to the `game.party` array. Which array method will you use to add them?
Solve Exercise 4 here:
*/
for (let mons of pokemon) {
if (mons.starter === true && mons.name === ('Squirtle')) {
game.party.push(mons);
}
}
console.log('Exercise 4:',game.party)
/*
Exercise 5
1. Choose three more Pokémon from the `pokemon` array and add them to your party.
2. Consider different attributes like 'type' or 'HP' for your selection. Which array method will you use to add them?
Solve Exercise 5 here:
*/
//maxHPparty = []
for (let mons of pokemon) {
if (mons.hp > 130) {
game.party.push(mons)
}
}
//game.party.push(pokemon[1], pokemon[2],pokemon[3])
//console.log(maxHPparty);
//console.log();
console.log('Exercise 5: game party:',game.party);
/*
weakMons = []
for (mons of pokemon) {
if (mons.hp < 30) {
weakMons.push(mons)
}
}
console.log(weakMons);
*/
/*
Exercise 6
1. Set the `completed` property to true for gyms with a difficulty below 3.
2. Think about how you'd loop through the `gyms` array to check and update the `completed` property.
Solve Exercise 6 here:
*/
for (let gym of game.gyms) {
if (gym.difficulty < 3){
gym.completed = true
}
}
console.log('Exercise 6',game.gyms);
/*
Exercise 7
1. Evolve the starter Pokémon you added to your party earlier. Each starter Pokémon evolves into a specific one.
2. How would you replace the current starter Pokémon in your party with its evolved form?
Hint:
- Pokemon 1: Bulbasaur evolves into Pokemon 2: Ivysaur
- Pokemon 4: Charmander evolves into Pokemon 5: Charmeleon
- Pokemon 7: Squirtle evolves into Pokemon 8: Wartortle
- Pokemon 25: Pikachu evolves into Pokemon 26: Raichu
More Hints: The existing starter Pokemon will be *replaced* in your party with the Pokemon it evolved into. When working with an array of objects, the splice() array method is ideal for replacing one element with another.
Solve Exercise 7 here:
*/
pokemon.splice(0,1, 'Ivysaur')
console.log('Exercise 7:',pokemon[0]);
/*
Exercise 8
1. Print the name of each Pokémon in your party.
2. Consider using a loop or an array method to access each Pokémon's name.
Solve Exercise 8 here:
*/
partyNames = []
for (let mon of game.party) {
partyNames.push(mon.name)
}
console.log('Exercise 8:',partyNames);
/*
Exercise 9
1. Can you print out all the starter Pokémon from the `pokemon` array?
2. Think about how you can identify a starter Pokémon and then log their names.
Solve Exercise 9 here:
*/
starterMons = []
for (let mons of pokemon) {
if (mons.starter === true) {
starterMons.push(mons)
}
}
console.log('Exercise 9:', starterMons);
/*
Exercise 10
Create a method called `catchPokemon` and add it to the `game` object. You should not need to edit the original game object directly. This method should:
- Accept an object as a parameter called `pokemonObj`
- Add the `pokemonObj` to the `game.party` array.
- not return anything
After writing this method, call it and pass in a Pokemon object of your choice from the `pokemon` data to catch it.
Solve Exercise 10 here:
*/
game.catchPokemon = function (pokemonObj) {
this.party.push(pokemonObj)
}
game.catchPokemon(pokemon[75])
console.log('Exercise 10', game.party);
/*
Exercise 11
1. Copy the `catchPokemon` method that you just wrote above, and paste it below. Modify it so that it also decreases the number of pokeballs in your inventory each time you catch a Pokémon.
2. How will you find and update the quantity of pokeballs in the `game.items` array?
Tips:
For this exercise, it's okay to have a negative number of pokeballs.
After updating the method, call it and pass in a Pokemon object of your choice from the `pokemon` data to catch it.
Also, log the `game.items` array to confirm that the pokeball quantity is being decremented.
Solve Exercise 11 here:
*/
/*
game.catchPokemon = function (pokemonObj) {
this.party.push(pokemonObj)
for (let i = 0; i < this.items.length; i++) {
if (this.items.name === 'pokeball') {
this.items[i].quantity--;
break;
}
}
}
console.log(this.items);
*/
game.catchPokemon = function (pokemonObj) {
// Add the Pokemon to the party
this.party.push(pokemonObj);
// Find the Pokeball in game.items and decrement the quantity
for (let i = 0; i < this.items.length; i++) {
if (this.items[i].name === 'pokeball') {
this.items[i].quantity--; // Decrease the number of Pokeballs
break;
}
}
// Log the updated items array to check the Pokeball count
console.log('Exercise 11',this.items);
};
/*
Exercise 13
1. Create a `gymStatus` method in `game` to tally completed and incomplete gyms.
2. How will you iterate through the `gyms` array and update the tally? Remember to log the final tally.
This method should:
- Not accept any arguments.
- Initially create a constant `gymTally`, which is an object that has two
properties: `completed` and `incomplete`, both of which are initially set to 0.
- Iterate through the objects in the `game.gyms` array and update the
properties on `gymTally` as follows:
- `completed` should count how many gyms in the array have a value of `true`
for their `completed` property.
- `incomplete` should count how many gyms in the array have a value of
`false` for their `completed` property.
- Log the value of `gymTally`.
- The method should not return anything.
For example, if five gym objects have a value of `true` on their `completed` property and three gym objects have a value of `false` on their `completed` property, the logged value would be: `{ completed: 5, incomplete: 3 }`.
Solve Exercise 13 here:
*/
const gymTally = { completed: 0, incomplete: 0 }
game.gymStatus = function() {
this.gyms.forEach( gym => {
if(gym.completed) gymTally.completed++
else gymTally.incomplete++
})
}
game.gymStatus()
console.log('Exercise 13:',gymTally)
/*
Exercise 14
1. Add a `partyCount` method to `game` that counts the number of Pokémon in your party.
This method should:
- Not accept any arguments.
- Count the number of Pokemon in the party.
- return the found number of Pokemon in the party.
Solve Exercise 14 here:
*/
game.partycount = function () {
return this.party.length
}
console.log(game.partycount());
/*
Exercise 15
1. Now, complete gyms with a difficulty below 8. Reflect on how this is similar to or different from the previous gym exercises.
(change the value of `complete` in the qualifying objects from false to true).
Solve Exercise 15 here:
*/
for (let gym of game.gyms) {
if (gym.difficulty < 8){
gym.completed = true
}
}
console.log('Exercise 15',game.gyms);
/*
Exercise 16
1. Log the entire `game` object to the console.
Take a moment to review the changes you've made
throughout the exercises.
Solve Exercise 16 here:
*/
console.log('Exercise 16',game);