Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right so here comes the answer.
Now we know that we can tap into whatever value the user typed into here
after that forward slash by tapping into req.params. the name of that parameter which is
postName right?
So let's go ahead and instead of console logging it, let's save that into a variable.
Now let's say something like var requestedTitle is equal to req.params.postName.
The next thing we need to do is we need to compare this requestedTitle against all the titles that
we have inside our posts array
right?
So we know that posts is an array that contains a bunch of post objects.
So each object has two key value pairs, a title and a content and the title inside this object is what
we need to tap into.
But because we only have access to this posts array in order to access each individual post we have
to loop through the array.
Fortunately we already know how to do that and how to do that quite easily.
We can use our trusty forEach method in order to loop through each of the posts inside the post array.
So if you remember the syntax was we specify the thing that we wanted to loop through which is our post
array with an S at the end.
This is the plural form.
And then we call forEach on that array. And then we open up a set of parentheses and we put in an anonymous
function. Inside the parameters of the function we specify the singular form of our array object.
So that in this case is a single post
right? Without the 's'. And now we open up a set of curly brackets and inside here we now have access to
each and individual post inside our post array .
That means that we can store this other variable which is called storedTitle and this is going to be
equal to the post.title.
So what our loop does currently is it will loop through all of the posts inside our post array
and for each post, it's going to save it into a variable called storeTitle.
Now some of your developer instincts should be tingling by now as we mentioned we should always start from
const and then work upwards to let and var as needed.
So let's go ahead and change that to const and change that to const. And you'll notice that we actually
don't change the value of either of these inside the curly braces.
These both work fine for our purposes.
So now we have the requested title and each stored title Inside our array.
Now it's time to check for each post
whether if the storedTitle matches the requestedTitle so we can use an IF statement for that.
And we check to see if storedTitle === the requestedTitle.
Then in that case we're going to log match found.
And now this is all the code that we need to loop through each of our posts, store each of the titles
inside a constant called storeTitle and then we check if the storedTitle matches with the requested
title
and if so, we log match found.
So let's go ahead and check it out.
So once you resave our app.js you might notice that our home page is again devoid of any blog posts.
Now in future modules, we're going to figure out how to fix this.
But in order to do that we need to learn about databases and that's not the focus of this module.
So let's just work with it as it is for now.
And let's head over to compose and create a new post that again has the title of Test and then some
Lorem Ipsum as the content.
So now we know that we have this post called Test.
And no matter how much we refresh, it still saved currently in our server.
Let's go ahead to posts/Test and I'm going to spell it exactly the same way in terms
of capitalization as the title of my post. And once I hit enter, you can see in your console that we get
match found.
If I go ahead and add another post...
and I write Day 1, then you can see again we get match found.
That's the solution to this challenge.
If you've struggled with this challenge depending on which part you found difficult I recommend reviewing
the lessons that covered those aspects.
There's a couple of things that might have tripped you up around here.
One is using the forEach to loop through our array which we covered previously in challenge 14.
Or it might be that you got tripped up by the Express routing parameters which we covered just before
challenge 16.
So either pause the video and re-explore those topics that you find difficult or if this was a breeze,
then continue on to the next challenge.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.