Skip to content
Snippets Groups Projects
Commit f14d6169 authored by AIT LAMINE Ilias's avatar AIT LAMINE Ilias
Browse files

Merge branch 'ilias_branch' into 'main'

feat: show five pokemons .

See merge request !2
parents 245e8eee 5ffa1708
No related branches found
No related tags found
1 merge request!2feat: show five pokemons .
...@@ -8,17 +8,45 @@ ...@@ -8,17 +8,45 @@
<body> <body>
<h1>Pokémon List</h1> <h1>Pokémon List</h1>
<ul id="pokemon-list"></ul> <ul id="pokemon-list"></ul>
<script> <script>
const pokemons = ["Pikachu", "Bulbasaur", "Charmander", "Squirtle"]; const baseApiUrl = "https://pokeapi.co/api/v2/pokemon"
const taille = [2, 4,8,10,7];
const poids = [3,3,9,10,2000]; /**
const listElement = document.getElementById("pokemon-list"); * Fetch the pokemon list with the weight and height
*/
function fetchPokemonList(callback, limit = 400) {
url = baseApiUrl + "?limit=" + limit;
const request = async() => {
const response = await fetch(url);
const json = await response.json();
let promisesArray = json["results"].map(result => {
return fetch(result.url).then(response => response.json());
});
return Promise.all(promisesArray);
};
request().then( (data)=> {
let pokemons = [];
for (let i=0; i < data.length; ++i) {
pokemons.push({"name":data[i].name, "weight": data[i].weight, "height": data[i].height});
}
callback(pokemons);
});
}
for (let i = 0; i < pokemons.length; i++) { function displayPokemons(pokemons) {
const li = document.createElement("li"); const listElement = document.getElementById("pokemon-list");
li.textContent = pokemons[i] + ' ' + poids[i] + 'kg ' + taille[i] + 'm'; listElement.innerHTML = ""; // Clear existing list
listElement.appendChild(li);
}; pokemons.forEach(pokemon => {
const li = document.createElement("li");
li.textContent = `${pokemon.name} - ${pokemon.weight}kg, ${pokemon.height}m`;
listElement.appendChild(li);
});
}
// Fetch and display Pokémon
fetchPokemonList(displayPokemons, 318);
</script> </script>
</body> </body>
</html> </html>
\ No newline at end of file
const baseApiUrl = "https://pokeapi.co/api/v2/pokemon"
/**
* Fetch the pokemon list with the weight and height
*/
function fetchPokemonList(callback, limit = 20) {
url = baseApiUrl + "?limit=" + limit;
const request = async() => {
const response = await fetch(url);
const json = await response.json();
let promisesArray = json["results"].map(result => {
return fetch(result.url).then(response => response.json());
});
return Promise.all(promisesArray);
};
request().then( (data)=> {
let pokemons = [];
for (let i=0; i < data.length; ++i) {
pokemons.push({"name":data[i].name, "weight": data[i].weight, "height": data[i].height});
}
callback(pokemons);
});
}
exports.fetchPokemonList = fetchPokemonList;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment