Created
September 28, 2023 14:56
-
-
Save JoshuaRotimi/cde97bce805e338d3786120ebdc3efe5 to your computer and use it in GitHub Desktop.
Fighting Types
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fetchStrengths = async (url) => { | |
| try { | |
| const strengths = await fetch(url); | |
| const allStrengths = await strengths.json(); | |
| const damageCaused = allStrengths.damage_relations.double_damage_from.map( | |
| (item) => item.name | |
| ); | |
| const damageDone = allStrengths.damage_relations.double_damage_to.map( | |
| (item) => item.name | |
| ); | |
| return `Weak against ${damageCaused.join( | |
| ", " | |
| )}. Strong against ${damageDone.join(", ")}.`; | |
| } catch (e) { | |
| console.log("Could not find Pokemon Info"); | |
| } | |
| }; | |
| const typeMatchUp = async (userType) => { | |
| try { | |
| const res = await fetch(`https://pokeapi.co/api/v2/type/`); | |
| const allTypes = await res.json(); | |
| const types = allTypes.results; | |
| const exists = types.find((item) => item.name === userType.toLowerCase()); | |
| if (exists) { | |
| return await fetchStrengths(exists.url); | |
| } | |
| return `This is not a valid Pokémon type, she's weak against everything.`; | |
| } catch (e) { | |
| console.log("Pokemon types failed"); | |
| } | |
| }; | |
| async function main() { | |
| try { | |
| const result = await typeMatchUp("dark"); | |
| console.log(result); | |
| } catch (error) { | |
| console.error("An error occurred:"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment