-
Notifications
You must be signed in to change notification settings - Fork 0
chore(02): correction step 2 - algorithmics #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 02_algorithmics
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,6 @@ export class PokemonService { | |
| }); | ||
| } | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
| async findAveragedStatsForTypes(types: string[]): Promise<PokemonStats> { | ||
| // Tips: Use this type | ||
|
|
||
|
|
@@ -120,14 +119,81 @@ export class PokemonService { | |
| // - Step 5: Get each pokemon's stats | ||
| // - Step 6: Calculate and return the average stats | ||
|
|
||
| return { | ||
| hp: 0, | ||
| attack: 0, | ||
| defense: 0, | ||
| specialAttack: 0, | ||
| specialDefense: 0, | ||
| speed: 0, | ||
| type GetPokemonTypeResponse = { | ||
| pokemon: { | ||
| pokemon: { | ||
| name: string; | ||
| url: string; | ||
| }; | ||
| }[]; | ||
| }; | ||
|
|
||
| // Step 1: Use route GET `type/${pokemonType}` to retrieve all pokemons of a given type | ||
| const pokemonWithCommonTypes = await Promise.all( | ||
| types.map((pokemonType) => | ||
| this.httpClient | ||
| .get(`type/${pokemonType}`) | ||
| .then( | ||
| (response) => response.body as unknown as GetPokemonTypeResponse, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pour éviter le const pokemonWithCommonTypes = await Promise.all(
types.map((pokemonType) =>
this.httpClient
.get<GetPokemonTypeResponse>(`type/${pokemonType}`)
.then((response) => response.body),
),
);et on peut extraire le .then(({ body }) => body), |
||
| ), | ||
| ), | ||
| ); | ||
|
|
||
| // Step 2: Remove any non-first generation pokemon from the array of pokemons | ||
| const firstGenerationPokemons = pokemonWithCommonTypes.map(({ pokemon }) => | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might have: On pourrait remplacer le |
||
| pokemon | ||
| .map(({ pokemon }) => ({ | ||
| id: parseInt( | ||
| pokemon.url | ||
| .replace('https://pokeapi.co/api/v2/pokemon', '') | ||
| .replaceAll('/', ''), | ||
| ), | ||
| name: pokemon.name, | ||
| })) | ||
| .filter((item) => item.id <= FIRST_GENERATION_POKEMON_LAST_ID), | ||
| ); | ||
|
|
||
| // Step 3: Remove any duplicate in the array of pokemons | ||
| const pokemonsWithoutDuplicates = firstGenerationPokemons | ||
| .flat() | ||
| .reduce((acc, current) => { | ||
| if (!acc.some((item) => item.id === current.id)) { | ||
| acc.push(current); | ||
| } | ||
|
|
||
| return acc.sort(); | ||
| }, []); | ||
|
|
||
| // Step 4: Get each pokemon's stats | ||
| const sameTypePokemonStats = await Promise.all( | ||
| pokemonsWithoutDuplicates.map(({ name }) => this.findPokemon(name)), | ||
| ); | ||
|
|
||
| // Step 5: Calculate average for each stats and return | ||
| const numberOfSameTypePokemons = sameTypePokemonStats.length; | ||
|
|
||
| return sameTypePokemonStats.reduce( | ||
| (acc: PokemonStats, pokemon) => ({ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. là aussi, on peut extraire
|
||
| hp: acc.hp + pokemon.stats.hp / numberOfSameTypePokemons, | ||
| attack: acc.attack + pokemon.stats.attack / numberOfSameTypePokemons, | ||
| defense: acc.defense + pokemon.stats.defense / numberOfSameTypePokemons, | ||
| specialAttack: | ||
| acc.specialAttack + | ||
| pokemon.stats.specialAttack / numberOfSameTypePokemons, | ||
| specialDefense: | ||
| acc.specialDefense + | ||
| pokemon.stats.specialDefense / numberOfSameTypePokemons, | ||
| speed: acc.speed + pokemon.stats.speed / numberOfSameTypePokemons, | ||
| }), | ||
| { | ||
| hp: 0.0, | ||
| attack: 0.0, | ||
| defense: 0.0, | ||
| specialAttack: 0.0, | ||
| specialDefense: 0.0, | ||
| speed: 0.0, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| private extractStatFromResponse( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Pourquoi
pokemondanspokemon? 🤔 Est-ce qu'on ne peut pas faire autrement ? 🤔Ce serait bien si on pouvait avoir:
Si on change dans cette PR, il faudra modifier aussi dans l'énoncé de la 1ère PR d'algo 🙂