Cumulative Project: The Scoop: Questions

Hello all, as the title pretty much suggests, I’m at cumulative project “the scoop” on Codecademy’s javascript course. link

I have been stuck at it for several days, i learned a lot by rereading some of the courses and searching on the internet, but some parts I sadly still don’t get.(Which in my defense aren’t always thaught during the courses, also there’s no tutorial video for this one).

I haven’t actually started the exercise, i want to understand the existing code first, that’s the key to solving this project I think. I will post and number my questions on the existing code under this text so it stays coherent.

Question 1: all of the functions look like getUser(url, request) etc etc but what are “url” and “request”?
Question 1.1: What’s Url? I thought something like thescoop.com/users/1 or is is it just users/1 without the first part?
Question 1.2: What’s the request? Is it something as in the lessons like this:
GET /articles/23
Accept: text/html, application/xhtml ?
Questions 1.3: If this is correct and someone has time to type this out, what would the request look like with a body?

Questions part 2, getUser function:
In this function we see a variable like this:
const username = url.split(’/’).filter(segment => segment)[1];
Individually I understand what each part does, but I’m lost at the bigger picture.
Question 2.1: Can someone tell me what the result of this would be with a random url?
Question 2.2: Most of all I don’t understand what filter(segment => segment) does.
It’s a filter yes and an arrow function, but what does it do?

Part 3: in getArticles we see some code like this:
articles: Object.keys(database.articles)

    .map(articleId => database.articles[articleId])

    .filter(article => article)

    .sort((article1, article2) => article2.id - article1.id)

Question 3: Can someone tell me what this returns or what each part of the code after object.keys does?

Part 4 is a similar issue: in several functions we see code like this:
const id = Number(url.split(’/’).filter(segment => segment)[1]);
I understand each part’s individual action but i can’t see what the total will produce.
So finally question 4 would be: what does this do and can someone illustrate with random url?

So, this was it, any help would be greatly appreciated.

Hello, @digital0677595534.

I don’t have the time right now to get into all of your questions, but I can help you with the .filter(segment => segment) right quick.

const url = 'https://discuss.codecademy.com/t/cumulative-project-the-scoop-questions/458067';

//create an array from the url using filter to remove 'falsy' values
const id = url.split('/').filter(segment => segment);
console.log('With filter(segment => segment):');
console.log(id);

//create an array from the url string without filtering
const id2 = url.split('/');
console.log('\nWithout filter:');
console.log(id2);

//Empty strings have a bool value of false
console.log('\nBool value of an empty string:');
console.log(Boolean(''));

Output:

With filter(segment => segment):
[ ‘https:’,
discuss.codecademy.com’,
‘t’,
‘cumulative-project-the-scoop-questions’,
‘458067’ ]

Without filter:
[ ‘https:’,
‘’,
discuss.codecademy.com’,
‘t’,
‘cumulative-project-the-scoop-questions’,
‘458067’ ]

Bool value of an empty string:
false

Notice that without using filter(segment => segment) we have an extra element in our array. It contains and empty string ''. The function in our .filter() method is checking every element of the array, and returning all truthy values. The empty string created because of the // following https: gets filtered out.

1 Like

Thank you a lot.
It’s a little technical thing but i wouldn’t have found it on my own.
Now that line makes sense and part 2 is solved entirely.

1 Like

Hi everyone,

I am starting the same project and as well stuck to understand the already written code :slight_smile:
All clear for question 2 on my side, thanks @midlindner!
For the remaining question I still struggle, maybe can you help further or @digital0677595534, maybe you have now more clarity on the code?

Thanks a lot!

I think I understand it more or less now, although understanding it has become less necessary due to the lessons after this.

Which parts do you not understand?

Hi! Thanks for answering.

The part I am not understanding are:

  • what is the URL + the request and the their purpose? How are they populated ? This is my main question because after I think I could make some console.log to understand better the other part of the code
  • is their a sort of hieracchie in route object? Meaning are ‘/users/:username’ and ‘/users’ linked for instance ?
  • for functions function getOrCreateUser and createArticle why do we assign the username / requestArticle with the request. body + the component of the body? Why not only one or the other?

It would be a huge help if you’re able to answer me :slight_smile:

Let’s start by saying I’m definitely no expert and you probably need someone who understands it better than I do.
Second, if you are doing the course “Create Back-end app with javascript”, I wouldn’t worry too much either, since the whole idea of getting/putting/deleting info from your database gets explained much better in the lessons that follow, compared to this exercise that was needlessly confusing in my opinion.

I’ll try to answer your questions though as good as i can:
-what is the url? Doesn’t really matter I think what it is exactly, by which I mean it could “www.example.com” or “www.anotherexample.com”+“your path”, with your path being ‘/users’ or ‘/users/:username’, depending on what you’re working on.
So its purpose would be it contains information to handle your request.
www.example.com/users/user123 tells you you want to do something with user123.

-what is the request? Honestly i don’t know yet exactly, all I do know it contains more info, like when you make post request, the info you are trying to add is in the request.body.

-are ‘/users/:username’ and ‘/users’ linked? yes they are, I don’t know what more I can say about this.

-for the third thing: I don’t know why they use “&&” operator exactly, but I can tell you how it works, it basically the opposite of how a “||” would work in the same statement.

for || : if you have let article = x || y;
your article will be equal to x, as long as x is “thruthy” (so not null or non-existent or false), otherwise it will be y.

the && works the other way around:
if you have let article = x && y, this means your article will be x if x is not not truthy, if it is though, your article equals y.

From your example:
const username = request.body && request.body.username;
If all works well in this example, your ‘username’ equals request.body.username, if for some reason there is a problem with your request and your request.body is null(so you most likely made a request but forgot to add info or added wrong info), your “username” will equal request.body, which would be null.

It’s not the best explanation you will read here, but I hope I could help you a little.