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!

I initially tried to set up my object reference with dot-notation rather than bracket notion but it triggered a test error when I ran it.


if (apples.id) {
req.variety = apples.id;

Instead of:

if (apples[id]) {
req.variety = apples[id];

Since the “id” in this case doesn’t appear to be a string, why didn’t it work?

hiya @spacetyrant did that code work when you used brackets?

Bracket notation did work, but I’m trying to understand why dot notation did not. Was it due to how the npm test was written, or is there something I’m missing?

I think it could be because we want to access an index on the list of apples objects, for each kind of apple, rather than a property (key-value pair) on the apples object. According to this stack overflow answer, each index has to be enclosed by its own brackets: https://stackoverflow.com/questions/47204733/js-objects-dot-notation-inside-bracket-notation-lists-inside-bracket-notation

PS I tried something like this in the code editor but CC is marking it as incorrect. Is this how you formatted your code?

appleRouter.param('variety', (req, res, next, variety) => {
  if (!apples[id]) {
  } else {
    req.variety = apples[id];

appleRouter.get('/:variety/description', (req, res, next) => {

appleRouter.get('/:variety/price-range', (req, res, next) => {

appleRouter.get('/:variety/color', (req, res, next) => {
1 Like

I think we have to use brackets instead of dot notation because.
Since the variable apples is a Object and whenever accessing a Object we can use dot Notation only if we are using the exact value if we are accessing through a variable then in that case we cannot use dot Notation
For Example

// Here is a demo code 
const apples = {
  mcintosh: {
    description: 'Classic, juicy, bright',
    priceRange: 'medium',
    color: 'green and red'
  // this will work fine
  console.log(apples.mcintosh.description)  // 1
  // In this case we cannot use dot notation
  let appleType = 'mcintosh';
  console.log(apples[appleType].description) // 2
  console.log(apples.appleType.description) // 3 -> ERROR

I have run this code
In the above Code 1 and 2 will work fine. But 3 will give a error i.e because appleType is a variable hence it should be used with brackets

This is the reason why we are using brackets,
I hope that solves your problem
If anything is wrong then excuse me



CC is marking it incorrect because req.variety must be set to apples[variety]. If you set req.variety to apples[id] then you must change (req, res, next, variety) to (req, res, next, id) because you are changing the parameter. Otherwise you’re trying to send a parameter variety that doesn’t exist

appleRouter.param('variety', (req, res, next, variety) => {
  if (!**apples[id]**) {
  } else {
    **req.variety = apples[id];**

Corrected code below

appleRouter.param('variety', (req, res, next, variety) => {
  if (!apples[variety]) {
  } else {
    req.variety = apples[variety];

Or alternatively, using parameter as id which should also work as a solution if I am not mistaken :slightly_smiling_face:

appleRouter.param('variety', (req, res, next, **id**) => {
  if (!apples[**id**]) {
  } else {
    req.variety = apples[**id**];

Can someone explain where the variety parameter comes from in this example? (req, res, next, variety)

What piece if data is being evaluated in if (!apples[variety]) ?