This community-built FAQ covers the “Code Challenge” exercise from the lesson “Code Challenges”.
Paths and Courses
This exercise can be found in the following Codecademy content:
Web Development
Learn Express
FAQs on the exercise Code Challenge
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply ( ) below!
Agree with a comment or answer? Like ( ) to up-vote the contribution!
Need broader help or resources ? Head here .
Looking for motivation to keep learning? Join our wider discussions .
Learn more about how to use this guide.
Found a bug ? Report it!
Have a question about your account or billing? Reach out to our customer support team !
None of the above? Find out where to ask other questions here !
_31337
April 22, 2020, 1:14pm
2
cleaner, without util functions
const puddingFlavors = ['chocolate', 'banana', 'butterscotch', 'pistachio'];
app.delete('/puddings/:flavor', (req, res, next) => {
const index = puddingFlavors.indexOf(req.params.flavor);
if(index !== -1){
puddingFlavors.splice(index, 1);
res.status(204).send();
} else {
res.status(404).send();
}
})
3 Likes
Why is this answer wrong:
app.delete('/puddings/:flavor', (req, res, next){
const flavor = req.params.flavor;
if (findPuddingIndex(flavor) !== -1){
deletePuddingAtIndex(findPuddingIndex(flavor));
res.status(204).send();
} else {
res.status(404).send();
}
})
Compared to the solution:
app.delete('/puddings/:flavor', (req, res, next) => {
const index = findPuddingIndex(req.params.flavor);
if(index !== -1){
deletePuddingAtIndex(index);
res.status(204).send();
} else {
res.status(404).send();
}
})
1 Like
My solution was also considered wrong. I completed it without the helper functions.
I saved the value of req.params.flavor to a variable called flavor.
I checked to see if that flavor existed inside puddingFlavors array, within the IF statement.
I saved the index of specific flavor to a variable called index.
Lastly I deleted it from the puddingFlavors array.
app.delete('/puddings/:flavor', (req, res, next) => {
const flavor = req.params.flavor;
if (puddingFlavors[flavor]) {
let index = puddingFlavors.indexOf(flavor);
puddingFlavors.splice(index, 1);
res.status(204).send();
} else {
res.status(404).send();
}
})
Can anyone explain how I would figure out that I should use ‘req.params.flavor’ instead of ‘req.params.name’ like the previous exercises. Am I missing something obvious?
The route being supplied is /puddings/:flavor
, not /puddings/:name
. That is why the req.params reference is req.params.flavor
I can go cleaner with the helper functions:
app.delete('/puddings/:flavor', (req, res, next) => {
if (findPuddingIndex(req.params.flavor) !== -1){
deletePuddingAtIndex(findPuddingIndex(req.params.flavor));
return res.status(204).send();
}
return res.status(404).send();
})