Created
September 28, 2023 21:54
-
-
Save fj1/42a536865a47bfea704a00e61ffc8cce to your computer and use it in GitHub Desktop.
Cassidoo #319
This file contains 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
/** | |
* https://buttondown.email/cassidoo/archive/i-love-mistakes-because-its-the-only-way-you/ | |
* | |
* Using the PokéAPI (or your own local setup): https://pokeapi.co/ | |
* write a function that takes in a Pokémon type, | |
* and returns what that type is weak against, and strong against. | |
* Here is the listing of types: https://pokeapi.co/api/v2/type/ | |
* and here's an example JSON for the fighting type: https://pokeapi.co/api/v2/type/2 | |
* | |
* Examples: | |
* | |
* > typeMatchup('fighting') | |
* > "Weak against flying, psychic, and fairy. Strong against normal, rock, steel, ice, and dark." | |
* | |
* > typeMatchup('cassidy') | |
* > "This is not a valid Pokémon type, she's weak against everything." | |
* // You can put whatever you want in this response, heh. | |
*/ | |
const fetchAllTypes = async () => { | |
const pokemonTypesUrl = "https://pokeapi.co/api/v2/type/"; | |
let pokemonTypes = []; | |
try { | |
const response = await fetch(pokemonTypesUrl); | |
const { results = [] } = await response.json(); | |
pokemonTypes = results.map(({name}) => name); | |
} catch (error) { | |
console.error(`🚨 Error: ${error}`); | |
} | |
pokemonTypes.length > 0 | |
? console.log(`✅ The Pokemon types are ${pokemonTypes.join(", ")}.`) | |
: console.log(`🚨 Did not fetch any Pokemon types.`); | |
return pokemonTypes; | |
}; | |
const fetchType = async (input) => { | |
const typeUrl = `https://pokeapi.co/api/v2/type/${input}`; | |
let doubleDamageFrom = []; | |
let doubleDamageTo = []; | |
try { | |
const response = await fetch(typeUrl); | |
const result = await response.json(); | |
({ | |
damage_relations: { | |
double_damage_from: doubleDamageFrom, | |
double_damage_to: doubleDamageTo, | |
}, | |
} = result); | |
} catch (error) { | |
console.error(`🚨 Error: ${error}`); | |
} | |
return { doubleDamageFrom, doubleDamageTo }; | |
} | |
const capitalize = string => `${string[0].toUpperCase()}${string.slice(1)}`; | |
const getStringFromTypes = array => { | |
const types = array.map(({ name }) => name); | |
let string = ''; | |
if (array.length === 1) { | |
string = types.join(); | |
} else if (array.length === 2) { | |
string = types.join(' and '); | |
} else if (array.length > 2) { | |
const commaSeparatedString = types.join(", "); | |
const indexOfLastSpace = commaSeparatedString.lastIndexOf(" "); | |
string = `${commaSeparatedString.slice( | |
0, | |
indexOfLastSpace | |
)} and ${commaSeparatedString.slice(indexOfLastSpace + 1)}`; | |
} | |
return string; | |
} | |
const getOpponentString = damage => | |
damage.length > 0 | |
? getStringFromTypes(damage) | |
: "nothing"; | |
const typeMatchup = async (input) => { | |
const types = await fetchAllTypes(); | |
// the api includes an 'unknown' pokemon type | |
if (input === "unknown" || (types.length > 0 && !types.includes(input))) { | |
console.log(`🚨 ${capitalize(input)} is not a valid Pokémon type.`); | |
} else if (types.includes(input)) { | |
const typeInfo = await fetchType(input); | |
if (typeInfo) { | |
const weakAgainst = getOpponentString(typeInfo.doubleDamageFrom); | |
const strongAgainst = getOpponentString(typeInfo.doubleDamageTo); | |
console.log( | |
`${capitalize( | |
input | |
)} is weak against ${weakAgainst} 🙃. Strong against ${strongAgainst} 💪.` | |
); | |
} else { | |
console.error("🚨 No type info"); | |
} | |
} | |
} | |
typeMatchup("fighting"); // "Weak against flying, psychic, and fairy. Strong against normal, rock, steel, ice, and dark." | |
typeMatchup("meow"); // invalid msg | |
typeMatchup("unknown"); // invalid msg |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment