アンバサダーの feedback-ai に以下のものが紹介されていたので、そのまま実行してみました。
In Javascript, write a script that requires axios, then creates an async function with a loop inside that uses axios with a template literal to call the PokeAPI five times in order to get the first five Pokemon. Console log each pokemon's name, height, and weight. Return the actual code. Start by writing "```" and end by writing "```". Then, write a new line, and then execute that code and format the results as a table.
作成されたスクリプトと実行結果
async function getPokemon() { for (let i = 1; i <= 5; i++) { const { data } = await axios.get(`https://pokeapi.co/api/v2/pokemon/${i}`); console.log(` Name: ${data.name} Height: ${data.height} Weight: ${data.weight} `); } } getPokemon();
Name | Height | Weight |
Bulbasaur | 7 | 69 |
Ivysaur | 10 | 130 |
Venusaur | 20 | 1000 |
Charmander | 6 | 85 |
Charmeleon | 11 | 190 |
2回目
async function getPokemon() { for (let i = 1; i <= 5; i++) { const { data } = await axios.get( `https://pokeapi.co/api/v2/pokemon/${i}` ); console.log(data.name); console.log(data.height); console.log(data.weight); } } getPokemon();
name | height | weight |
Bulbasaur | 7 | 69 |
Ivysaur | 10 | 130 |
Venusaur | 20 | 1000 |
Charmander | 6 | 85 |
Charmeleon | 11 | 190 |