All right. 1
So here's the answer to this challenge. 2
Now in order to solve this challenge you have to remember that programmers don't memorize things. 3
Instead we always search for what it is that we need and the skill is being able to apply it to your 4
current problem. 5
Good old Google. 6
I'm going to start off by searching inside Google for what it is that I want and it tends to be verb 7
object and the programming language that I use. 8
So in this case I want to truncate a string using Javascript. 9
This is a good format to use whenever you're trying to solve problems. 10
Don't have anything that's really specific in here. 11
Say if I just said truncate string to 100 characters, then I would limit my search to only the people 12
who explained how to truncate a string to 100 characters right? 13
Somebody might have wanted to do it for 20 characters or 5 characters. So don't have anything specific 14
in there, just have a verb, an object 15
and the programming language and that tends to work pretty well. 16
So let's explore the first answer. 17
And it seems like this person also just want to truncate something using pure Javascript down to a set 18
number of characters. 19
Now the first answer explains to you that you can use the substring method and they very kindly link to 20
the documentation. And you can see that here if you have a variable that say hello world which is a string 21
then you can use the substring method on that string in order to cut it down. 22
So in this case if we're saying that we want to cut it down from 1 to 4, then remember that programming 23
starts from 0 24
so we would take the first character of our Hello world 25
so at this position, and then we would end up the fourth character. 26
So if I now hit Try it you can see that I just get ell . And if I change this to say 0 and 3 then 27
I will start from the beginning and I'll only take the first three characters. 28
So now if I hit run and hit Try it, then I only get Hel. 29
So let's apply this substring method to our code to make it do what we want. 30
The part that we want to truncate is our post.content and we want to do it inside our home.ejs 31
where we got access to each and every post's content. Right at the end here 32
I'm going to tag on that method substring and then I need to specify a from and a to. 33
So the from is going to be from the very beginning, 34
so the zeroth character and the end is going to be at the 100th character, 35
so 0 - 100. And then at the end of all of this string, I'm going to append or add onto it space and then 36
... 37
And this should indicate to the reader that this is truncated 38
and if they wanted to read more than they have to click on something. 39
So let's hit save. And we head back to our home page and we hit refresh 40
then you should see that all of our blog posts are now truncated down to only 100 characters and we 41
just have that ... at the end to show that there's actually more. To complete this entire module 42
there is only one more challenge. 43
So I'll see over there.