Skip to content

Instantly share code, notes, and snippets.

@Swendude
Created August 11, 2022 08:28
Show Gist options
  • Save Swendude/f98b4af3b310f690287b709d7e3e306f to your computer and use it in GitHub Desktop.
Save Swendude/f98b4af3b310f690287b709d7e3e306f to your computer and use it in GitHub Desktop.
const characters = require("./characters.json");
// **A** Write a function that selects the **4th** character of your characters array and returns it.
const selectFourthCharacter = () => characters[3];
// **B** Write a function use an array method to locate the character object with id `78` and return it.
const findById = () => characters.find((c) => c.id === 78);
/*
**C** Write a function recieves 1 parameter "blood" returns an array with only the characters that have that blood type.
Example usage:
byBlood("Half-blood")
*/
const findByBlood = (blood) => characters.filter((c) => c.blood === blood);
// **BONUS**
// **D** Write a function returns an array that contains only the `quotes` of each character (so strings, not object)
const getQuotes = () => characters.map((c) => c.quote);
// **E** Write a function that recieves 1 parameter "species" and returns an array with all characters that are NOT of that species.
const notThisSpecies = (species) =>
characters.filter((c) => c.species !== species);
/*
**F** Write a function that recieves 1 param called "searchTerm" return an array of all characters whose name **_includes_** this string
For example, if called like:
includesInName("agr") // should return => [{ name: "Hagrid" }] (the full object, not only name like in this example)
Tip here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
*/
// Typo here to check
const includesInName = (searcTerm) =>
characters.filter((c) => c.name.includes(searcTerm));
module.exports = {
selectFourthCharacter,
findById,
findByBlood,
getQuotes,
notThisSpecies,
includesInName,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment