Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,270 --> 00:00:01,970
All right so here comes the answer.
2
00:00:03,490 --> 00:00:08,350
Now we know that we can tap into whatever value the user typed into here
3
00:00:08,410 --> 00:00:14,470
after that forward slash by tapping into req.params. the name of that parameter which is
4
00:00:14,470 --> 00:00:15,850
postName right?
5
00:00:16,059 --> 00:00:22,970
So let's go ahead and instead of console logging it, let's save that into a variable.
6
00:00:22,990 --> 00:00:30,600
Now let's say something like var requestedTitle is equal to req.params.postName.
7
00:00:30,640 --> 00:00:37,090
The next thing we need to do is we need to compare this requestedTitle against all the titles that
8
00:00:37,090 --> 00:00:40,690
we have inside our posts array
9
00:00:40,710 --> 00:00:41,200
right?
10
00:00:41,470 --> 00:00:48,370
So we know that posts is an array that contains a bunch of post objects.
11
00:00:48,370 --> 00:00:56,890
So each object has two key value pairs, a title and a content and the title inside this object is what
12
00:00:56,890 --> 00:00:58,350
we need to tap into.
13
00:00:58,450 --> 00:01:05,260
But because we only have access to this posts array in order to access each individual post we have
14
00:01:05,260 --> 00:01:07,520
to loop through the array.
15
00:01:07,960 --> 00:01:12,790
Fortunately we already know how to do that and how to do that quite easily.
16
00:01:12,790 --> 00:01:21,600
We can use our trusty forEach method in order to loop through each of the posts inside the post array.
17
00:01:21,610 --> 00:01:28,270
So if you remember the syntax was we specify the thing that we wanted to loop through which is our post
18
00:01:28,360 --> 00:01:30,460
array with an S at the end.
19
00:01:30,490 --> 00:01:31,810
This is the plural form.
20
00:01:32,110 --> 00:01:40,150
And then we call forEach on that array. And then we open up a set of parentheses and we put in an anonymous
21
00:01:40,150 --> 00:01:48,820
function. Inside the parameters of the function we specify the singular form of our array object.
22
00:01:48,820 --> 00:01:51,920
So that in this case is a single post
23
00:01:51,970 --> 00:01:58,720
right? Without the 's'. And now we open up a set of curly brackets and inside here we now have access to
24
00:01:58,780 --> 00:02:02,670
each and individual post inside our post array .
25
00:02:02,980 --> 00:02:10,780
That means that we can store this other variable which is called storedTitle and this is going to be
26
00:02:10,840 --> 00:02:15,310
equal to the post.title.
27
00:02:15,370 --> 00:02:23,530
So what our loop does currently is it will loop through all of the posts inside our post array
28
00:02:23,770 --> 00:02:30,630
and for each post, it's going to save it into a variable called storeTitle.
29
00:02:30,640 --> 00:02:36,670
Now some of your developer instincts should be tingling by now as we mentioned we should always start from
30
00:02:36,670 --> 00:02:40,710
const and then work upwards to let and var as needed.
31
00:02:40,720 --> 00:02:46,060
So let's go ahead and change that to const and change that to const. And you'll notice that we actually
32
00:02:46,060 --> 00:02:51,900
don't change the value of either of these inside the curly braces.
33
00:02:52,210 --> 00:02:55,100
These both work fine for our purposes.
34
00:02:55,570 --> 00:03:01,690
So now we have the requested title and each stored title Inside our array.
35
00:03:01,720 --> 00:03:05,460
Now it's time to check for each post
36
00:03:05,620 --> 00:03:10,910
whether if the storedTitle matches the requestedTitle so we can use an IF statement for that.
37
00:03:11,230 --> 00:03:19,780
And we check to see if storedTitle === the requestedTitle.
38
00:03:19,930 --> 00:03:25,350
Then in that case we're going to log match found.
39
00:03:26,020 --> 00:03:32,980
And now this is all the code that we need to loop through each of our posts, store each of the titles
40
00:03:33,070 --> 00:03:38,710
inside a constant called storeTitle and then we check if the storedTitle matches with the requested
41
00:03:38,710 --> 00:03:39,450
title
42
00:03:39,520 --> 00:03:42,040
and if so, we log match found.
43
00:03:42,040 --> 00:03:43,620
So let's go ahead and check it out.
44
00:03:44,700 --> 00:03:52,020
So once you resave our app.js you might notice that our home page is again devoid of any blog posts.
45
00:03:52,040 --> 00:03:55,410
Now in future modules, we're going to figure out how to fix this.
46
00:03:55,500 --> 00:04:01,020
But in order to do that we need to learn about databases and that's not the focus of this module.
47
00:04:01,020 --> 00:04:04,110
So let's just work with it as it is for now.
48
00:04:04,380 --> 00:04:11,820
And let's head over to compose and create a new post that again has the title of Test and then some
49
00:04:11,820 --> 00:04:14,110
Lorem Ipsum as the content.
50
00:04:14,130 --> 00:04:16,820
So now we know that we have this post called Test.
51
00:04:17,040 --> 00:04:21,920
And no matter how much we refresh, it still saved currently in our server.
52
00:04:22,500 --> 00:04:29,190
Let's go ahead to posts/Test and I'm going to spell it exactly the same way in terms
53
00:04:29,190 --> 00:04:36,660
of capitalization as the title of my post. And once I hit enter, you can see in your console that we get
54
00:04:36,660 --> 00:04:37,980
match found.
55
00:04:37,980 --> 00:04:40,910
If I go ahead and add another post...
56
00:04:46,480 --> 00:04:52,070
and I write Day 1, then you can see again we get match found.
57
00:04:52,270 --> 00:04:54,290
That's the solution to this challenge.
58
00:04:54,310 --> 00:04:59,920
If you've struggled with this challenge depending on which part you found difficult I recommend reviewing
59
00:04:59,920 --> 00:05:03,000
the lessons that covered those aspects.
60
00:05:03,010 --> 00:05:05,520
There's a couple of things that might have tripped you up around here.
61
00:05:05,710 --> 00:05:13,600
One is using the forEach to loop through our array which we covered previously in challenge 14.
62
00:05:13,830 --> 00:05:19,930
Or it might be that you got tripped up by the Express routing parameters which we covered just before
63
00:05:19,930 --> 00:05:22,120
challenge 16.
64
00:05:22,120 --> 00:05:27,870
So either pause the video and re-explore those topics that you find difficult or if this was a breeze,
65
00:05:27,940 --> 00:05:30,260
then continue on to the next challenge.
6773
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.