-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.esnext.video.js
More file actions
113 lines (80 loc) · 2.04 KB
/
Copy pathscript.esnext.video.js
File metadata and controls
113 lines (80 loc) · 2.04 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
'use strict'
const fetchOption = {
headers: {
'Content-Type' : 'application/json'
},
mode: 'cors'
};
$('form').on('submit', function(e) {
e.preventDefault();
let types = $('input[type=text]').val().replace(/\s/g,'');
types = types.split(',');
let trainerTypeCalls = types.map( elem => {
return fetch(`http://pokeapi.co/api/v2/type/${elem}/`, fetchOption)
});
getPromiseData(trainerTypeCalls)
.then( result => {
console.log(result);
getDoubleDamagePokemon(result);
});
});
function getDoubleDamagePokemon(pokemonTypes) {
pokemonTypes = pokemonTypes
.map( types => {
return types.damage_relations.double_damage_from
})
.reduce(flatten, [])
.map( type => {
return fetch(type.url, fetchOption)
});
getPromiseData(pokemonTypes)
.then(results => {
console.log(results);
buildTeam(results);
});
}
function buildTeam(pokemons) {
let team = [];
pokemons = pokemons.map( pokemon => {
return pokemon.pokemon;
})
.reduce(flatten, [])
.map( pokemon => pokemon.pokemon );
for(let i = 0; i < 6; i++) {
team.push( getRandomPokemon(pokemons) );
}
team = team.map(pokemon => {
return fetch(pokemon.url,fetchOption);
});
getPromiseData(team)
.then(pokemonData => {
displayPokemon(pokemonData);
});
}
function getRandomPokemon(pokemonArray) {
return pokemonArray[ Math.floor(Math.random() * pokemonArray.length) ];
}
const flatten = (a,b) => [...a,...b];
function getPromiseData(promisesArray) {
return new Promise((resolve,reject) => {
Promise.all(promisesArray)
.then(res => {
return res.map( type => type.json() );
})
.then(res => {
Promise.all(res)
.then(resolve);
})
.catch(reject);
});
}
function displayPokemon(pokemon) {
// loop through and display the pokemon!
pokemon.forEach( poke => {
var $container = $('<div>').addClass('pokemon');
var $image = $('<img>').attr('src',`http://pokeapi.co/media/img/${poke.id}.png`);
var $title = $('<h2>').text(poke.name);
$container.append($image,$title);
$('.poke-container').append($container);
});
}