FAQ: Code Challenges - Code Challenge

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 (reply) below!

Agree with a comment or answer? Like (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!

Why is the following code marked wrong?

app.get('/battlefields/:name', (req, res, next) => {
  const battlefieldName = req.params.name;
  if (battlefields[battlefieldName]) {
    res.send(battlefields[battlefieldName]);
} else {
  res.status(404).send();
});

Isn’t that the same as the given solution:

app.get('/battlefields/:name', (req, res, next) => {
  const battlefieldName = req.params.name;
  const battlefield = battlefields[battlefieldName];
  if (battlefield){
    res.send(battlefield);
  } else {
    res.status(404).send();
  }
});

I did exactly the same thing - to me it should of accepted it

You forgot the closing bracket of your else satement.

 const battlefieldName = req.params.name;
 const battlefield = battlefields[battlefieldName];

Can anyone explains what does these two lines of code mean?

2 Likes

Hello, Why is this code don’t let me pass 5/10?


app.get('/battlefields/:name', (req, res, next) => {
  const battlefieldName = req.params.name;
  const check = battlefields.hasOwnProperty(battlefieldName);
  if (check) {
    res.status(200).send(battlefields[battlefieldName]);
  } else {
    res.status(404).send();
  }
});

Nevermind :smiley: fixed. put status 202 instead 200.

Hi, I’m wondering if anyone has actually tried to enter this code challenge into an app.js file and then run it through Express locally (using nodemon or node JS)?

I have Express installed properly, but even when I copy and paste the file over exactly into my local machine and running it using node JS, I keep getting the error “Cannot GET /” .

I’m assuming there’s something wrong with the pathway, but I then again, don’t know what that error would be, since the pathway is for the endpoint only…

Has anyone else tried to run this or the other code challenges on their local machines? Have you been able to get this (and others) to run properly? Thanks in advance… Rob

How come dot notation does not work in this instance?

app.get('/battlefields/:name', (req, res, next) => {
  const battlefieldName = req.params.name;
  const battlefield = battlefields.battlefieldName;
  if (battlefield) {
    res.send(battlefield);
  } else {
    res.status(404).send();
  }
});

Dot Notation vs. Bracket Notation

In conclusion, dot notation is the most common way to access elements in JavaScript. However, there are certain situations where bracket notation is more appropriate. When you want to use a variable to access a property, or if a property has special characters in its name, you should use bracket notation.