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!

Hi there,
I’m wondering why my code isn’t working? What is the difference between using const countries = req.query (method from solution) and putting req.query directly in countries array?

code:

const express = require('express');
const app = express();

const PORT = process.env.PORT || 4001;

const currencies = {
  diram: {
    countries: ['UAE', 'Morocco'],
  },
  real: {
    countries: ['Brazil'],
  },
  dinar: {
    countries: ['Algeria', 'Bahrain', 'Jordan', 'Kuwait'],
  },
  vatu: {
    countries: ['Vanuatu'],
  },
  shilling: {
    countries: ['Tanzania', 'Uganda', 'Somalia', 'Kenya'],
  },
};

//my code - not passing test:

app.put('/currencies/:name/countries', (req, res, next) => {
  const currencyName = req.params.name;
  currencies[currencyName].countries = req.query;
  res.send(currencies[currencyName]);
})

//solutions code - working fine:

app.put('/currencies/:name/countries', (req, res, next) => {
  const currencyName = req.params.name;
  const countries = req.query;
  currencies[currencyName] = countries;
  res.send(currencies[currencyName]);
});


app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

1 Like

Is the req.query the currency object which contains an array with the countries to update as a property? Could the req.query update the currency name as well?

I felt a bit mislead when comparing my attempt with the solution when I saw the constant named countries.

1 Like

In your code, you are putting the value of req.query into currencies[currencyName].countries, while at the solution code, the value of req.query is going to currencies[currencyName].

You can write directly:

currencies[currencyName] = req.query

Hey @factoradic

I’m trying to understand the logic in the answer for this exercise.

Question: Write a route to handle PUT requests to /currencies/:name/countries .

The :name param represents the currency name in the currencies object. The updated array of countries that use this currency will be sent in a query.

This route handler should replace the countries array of the correct single-currency object with an updated array from the query. It should send the updated array as a response.

Answer:

const currencies = {
  diram: {
    countries: ['UAE', 'Morocco'],
  },
  real: {
    countries: ['Brazil'],
  },
  dinar: {
    countries: ['Algeria', 'Bahrain', 'Jordan', 'Kuwait'],
  },
  vatu: {
    countries: ['Vanuatu'],
  },
  shilling: {
    countries: ['Tanzania', 'Uganda', 'Somalia', 'Kenya'],
  },
};

app.put('/currencies/:name/countries', (req, res, next) => {
 // The name of the currency is the name parameter in our request object, and 
 // req.params is an object that holds properties mapped to the named route “parameters”
// Route parameters are named URL segments that are used to capture the values specified at their position in the URL 
  const currencyName =  req.params.name; 
  // The new countries to update are in the body of the request object, which contains a property for each query string parameter in the route. This is what is sent in our query. 
  const countries = req.query;  
//Access the currencies object using bracket notation
//currencies object[name sent in our request body] = list of countries sent in our query 

  currencies[currencyName] = countries; 

 //Send in the response the new updated list of countries 
  res.send(currencies[currencyName]); 

}) 

If countries is our query, would an example route for this request look like this?

/currencies/:pesos/countries?=Mexico 

and the request object look like this?

{
params: {
name: "pesos"
},
query: {
countries: "Mexico"
}
}
1 Like

req.query is the list of countries sent with our request body with which to update our currency object.

I supposed req.query could also update the currency name as well, if the currency name was a query, but that would be a different exercise

Another misleading lesson from Codecademy…

“The updated array of countries that use this currency will be sent in a query.”

Given the above explanation, and the following solution :

currencies[currency] = req.query;

req.query must be an object, not an array:

currencies[currency] = {countries: ["x", "y", "z"]}
4 Likes

You are correct. If you check out test.js for this problem, the object they are using for the test is:

let updateTestObject = {
  countries: ['test', 'test'],
};
1 Like

Thanks for this! it helped me :wink:

1 Like

So what we’re actually doing is replacing the whole object value of specified currency (/:name) since req.query returns an object not an array. Like most of you I tried accessing the array specifically with:

currencies[typeOfMoney].countries = update;

My answer:

app.put('/currencies/:name/countries', (req, res, next) => {
  let typeOfMoney = req.params.name;
  let update = req.query;
  if (currencies[typeOfMoney]) {
    currencies[typeOfMoney] = update;
    res.send(currencies[typeOfMoney]);
  } else {
    res.status(404).send();
  }
})

The problem with this challenge is I (we?) haven’t learned how to test our code yet.
I am not familiar with test.js module yet. Unlike in the browser, we can’t use console.log() to test our code. Yet…

Hi, for purposes of actually testing this code in a real environment, 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…

Can someone please explain what this part does?

currencies[currencyName] = countries;

When I first read this challenge I thought the aim was, for one currency, to update the list of countries by adding a country name. Now looking at the solution it seems the aim was to add an extra currency and give that a new list of countries? or am I still confused?

I have and it doesn’t work. I read on another thread that this isn’t a good lesson. I’m really not happy with this express course

I’m so sick of the bloated BS in these lessons. ---->currencies[currencyName] = countries;<----- I’m sure there’s a reason this does whatever it does but I can’t seem to find a way to mutate an object using this syntax in any form and since the actual WAY THE FUNCTIONS WORK IS NEVER EXPLAINED, I just have to copy the solution code so I can move on without actually understanding anything.

1 Like

Can PUT requests create a new entry like a POST request? I thought in the previous lessons we would first check if the updated item exists and if not send a 404 err?