-
Notifications
You must be signed in to change notification settings - Fork 529
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
Harry Potter express API #536
base: master
Are you sure you want to change the base?
Conversation
…ery params using if else statements and tried adding name in a separate route
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.
Good job with your first API Fanny! You've met the requirements, but I'd like to challenge you to think of a way to chain your filters 👀
app.get("/harryPotterCharacters", (req, res) => { | ||
const house = req.query.house; // Query parameter for house | ||
const year = req.query.yearIntroduced; // Query parameter for yearIntroduced | ||
const role = req.query.role; // Query parameter for role | ||
|
||
// Filter by house | ||
if (house) { | ||
const filteredCharacters = harryPotterCharactersData.filter(character => | ||
character.house.toLowerCase() === house.toLowerCase() | ||
); | ||
res.json(filteredCharacters); | ||
} | ||
// Filter by role | ||
else if (role) { | ||
const characterRole = harryPotterCharactersData.filter(character => | ||
character.role.toLowerCase() === role.toLowerCase() | ||
); | ||
res.json(characterRole); | ||
} | ||
// Filter by yearIntroduced | ||
else if (year) { | ||
const charactersIntroducedInYear = harryPotterCharactersData.filter(character => | ||
character.yearIntroduced === +year | ||
); | ||
res.json(charactersIntroducedInYear); | ||
} | ||
else { | ||
res.json(harryPotterCharactersData); // No filters = return all characters | ||
|
||
} | ||
}); |
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.
Nice job making use of query params to filter ⭐ Could you think of a way to improve this code so that the user can filter on two things at once? E.g. /characters?role=student&year=1997
Netlify link
https://project-express-api-qyuo.onrender.com/