-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
491 lines (380 loc) · 14.4 KB
/
app.js
File metadata and controls
491 lines (380 loc) · 14.4 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
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 },
],
}
//Exercise 1
//console.dir(pokemon, { maxArrayLength: null });
//Exercise 2
//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=['Easy','Med','Hard','VeryHard'];
console.log(`Exercise 3 : ${game.difficulty}`);
console.log()
/*
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:
*/
const starterPokemon=pokemon[24];
// console.log(starterPokemon)
// console.log()
game.party.push(starterPokemon);
console.log(`Exercise 4 :`)
console.log(game.party)
console.log()
/*
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:
*/
const p1=pokemon[43];
const p2=pokemon[68];
const p3=pokemon[98];
// console.log(p1)
// console.log(p2)
// console.log(p3)
game.party.push(p1,p2,p3)
console.log("Exercise 5 ")
console.log(game.party)
console.log()
/*
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 key in game.gyms){
if(game.gyms[key].difficulty <3 )
game.gyms[key].completed = true;
else
continue;
}
console.log("Exercise 6")
console.log(game.gyms)
console.log()
/*
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:
*/
console.log("Exercise 7:")
const evolve=pokemon[25];
game.party.splice(0,1,evolve);
console.log(game.party)
console.log()
/*
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:
*/
console.log("Exercise 8:")
for(let c=0;c<game.party.length;c++){
console.log(game.party[c].name)
}
console.log()
/*
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:
*/
console.log("Exercise 9");
for(let c=0;c<pokemon.length;c++){
if(pokemon[c].starter ===true)
console.log(pokemon[c])
}
console.log()
/*
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:
*/
console.log("Exercise 10:")
game.catchPokemon=catchPokemon =(pokemonObj)=>{game.party.push(pokemonObj)};
// console.log(game)
game.catchPokemon(pokemon[98]);
console.log(game.party)
console.log()
/*
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:
*/
console.log("Exercise 11:")
game.catchPokemon=catchPokemon =(pokemonObj)=>{
game.party.push(pokemonObj);
game.items[1].quantity--;
};
game.catchPokemon(pokemon[149]);
console.log(game.items)
console.log()
/*
Exercise 12
1. Similar to Exercise 6, now complete gyms with a difficulty below 6. How will you approach this?
(change the value of `complete` in the qualifying objects from false to true).
Solve Exercise 12 here:
*/
console.log("Exercise 12:")
for(let key in game.gyms){
if(game.gyms[key].difficulty <6 )
game.gyms[key].completed = true;
else
continue;
}
console.log(game.gyms)
console.log()
/*
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:
*/
console.log("Exercise 13:")
game.gymStatus = gymStatus = ()=>{
let gymTally={completed:0,incomplete:0}
for(let c=0;c<game.gyms.length;c++){
let currentV=game.gyms[c].completed;
if(currentV === true){
gymTally.completed+=1;
}
else{
gymTally.incomplete+=1;
}
}
console.log(gymTally);
};
game.gymStatus();
console.log();
/*
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:
*/
console.log("Exercise 14:")
game.partyCount = partyCount = () =>{
return (game.party.length)
}
console.log("Number of pokémon's in the party = "+game.partyCount())
console.log()
/*
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:
*/
console.log("Exercise 15:")
for(let key in game.gyms){
if(game.gyms[key].difficulty <8 )
game.gyms[key].completed = true;
else
continue;
}
console.log(game.gyms)
console.log()
/*
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:")
console.dir(game)
console.log()
console.log("======== level up ========");
console.log()
/*
Exercise 17
1. Arrange the Pokémon in `game.party` by their HP. The one with the highest HP should come first.
2. You'll need to use the `.sort()` method. How does the compare function work in sorting numbers?
Solve Exercise 17 here:
*/
console.log("Exercise 17:")
pokemon.sort((a, b) => b.hp - a.hp);
console.log(pokemon)
console.log()
/*
Exercise 18
Add a new property to the `game` object called `collection` and initialize its value to an empty array.
Copy the `catchPokemon` method you wrote in Exercise Twelve and paste it below. Modify it so that:
- Ensure that no more than six Pokemon can be in the party at any time.
Excess Pokemon should be placed in the `game.collection` array.
- It's up to you how to distribute Pokemon in a situation where more than six
would be placed into the `game.party` array.
Again, for this exercise, it's okay to have a negative number of pokeballs.
After updating the method, use it by calling it and passing 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 18 here:
*/
console.log("Exercise 18:")
game.collection=[];
game.catchPokemon=catchPokemon =(pokemonObj)=>{
if(game.party.length === 6){
game.collection.push(pokemonObj)
game.items[1].quantity--;
}
else{
game.party.push(pokemonObj);
game.items[1].quantity--;
}
};
game.catchPokemon(pokemon[21]); // its sorted :\ , i can't choice one from data.js file
console.log(game.items);
console.log(game.collection[0])
console.log()
/*
Exercise 19
Copy the `catchPokemon` method that you just wrote above, and paste it below. The time has come to make it so that we cannot catch a Pokemon when we do not have any pokeballs to catch it with.
Modify the method so that if there are no pokeballs a message will be displayed that there are not enough pokeballs to catch the desired Pokemon.
Also, ensure that the Pokemon isn't added to the `game.party` or the `game.collection`.
Solve Exercise 19 here:
*/
console.log("Exercise 19:")
game.catchPokemon=catchPokemon =(pokemonObj)=>{
if( (!game.party.find((poke)=> poke.number === pokemonObj.number) && !game.collection.find((poke)=> poke.number === pokemonObj.number))){
if(game.items[1].quantity > 0 ){
if(game.party.length === 6){
game.collection.push(pokemonObj)
game.items[1].quantity--;
}
else{
game.party.push(pokemonObj);
game.items[1].quantity--;
}
}
else{
console.log("there are not enough pokeballs to catch the desired Pokemon");
}
}else{
console.log("you already have same type of Pokemon in the party or collection");
}
};
game.catchPokemon(pokemon[10])
//game.catchPokemon(pokemon[10]) //for testing case where Pokemon in party or collection
console.log(game)
console.log()
/*
Exercise 20
Copy the `catchPokemon` method that you just wrote above, and paste it below. Modify is so that you can just pass in the name of a Pokemon instead of an entire object, and the method will look up the Pokemon from the data set for you.
The string passed in should be allowed to be any case (for example, if the string 'PiKacHU' is passed to the function, it should match to 'Pikachu' in the data set).
If there is not a match, then return a string noting that the selected Pokemon does not exist. Ensure you do not decrement the pokeball count if an invalid Pokemon name is passed in, and also ensure that the Pokemon isn't added to the `game.party` or the `game.collection`.
Solve Exercise 20 here:
*/
console.log("Exercise 20:")
game.catchPokemon=catchPokemon =(pokeName)=>{
const evryCase= pokeName.toLowerCase();
let addPoke=false;
for(let c=0;c<pokemon.length;c++){
if(evryCase === pokemon[c].name.toLowerCase()){
if( (!game.party.find((poke)=> poke.name === pokeName) && !game.collection.find((poke)=> poke.name === pokeName))){
if(game.items[1].quantity > 0 ){
addPoke=true;
if(game.party.length === 6){
game.collection.push(pokemon[c])
game.items[1].quantity--;
}
else{
game.party.push(pokemon[c]);
game.items[1].quantity--;
}
console.log(pokemon[c]) //for the look up
}
else{
console.log("there are not enough pokeballs to catch the desired Pokemon");
}
}else{
console.log("you already have same type of Pokemon in the party or collection");
}
}
}
if(addPoke === false)
console.log("selected Pokemon does not exist")
};
game.catchPokemon("Bulbasaur");
console.log()
/*
Exercise 21
Dynamically construct an object with the existing `pokemon` data sorted by the different pokemon types. The object will have this structure:
{
grass: [
{ number: 1, name: 'Bulbasaur', type: 'grass', hp: 45, starter: true },
{ number: 2, name: 'Ivysaur', type: 'grass', hp: 60, starter: false },
{ number: 3, name: 'Venusaur', type: 'grass', hp: 80, starter: false },
* more grass type Pokemon objects...
],
fire: [
{ number: 4, name: 'Charmander', type: 'fire', hp: 39, starter: true },
* more fire type Pokemon objects...
],
water: [
* water type Pokemon objects...
],
* etc... until there is an array for every Pokemon type!
}
Log the object when it's constructed.
Solve Exercise 21 here:
*/
console.log("Exercise 21:")
const pokeSortByType = {};
for (let c = 0; c < pokemon.length; c++) {
const type = pokemon[c].type;
if(!pokeSortByType[type]) { // checking if the type is exist, if not add the type as array that will hold pokemon's of the same type
pokeSortByType[type] = [];
}
pokeSortByType[type].push(pokemon[c]);
}
console.log(pokeSortByType)