All right. 1
So here comes Challenge 20. Currently on our home page it's quite cluttered because every single blog post 2
no matter how long it is one paragraph or five paragraphs, 3
it all gets listed here. 4
But now that we've managed to create a separate page for each and every single one of our blog posts 5
we don't want to list it in full on our home page anymore. 6
The goal of this challenge is to use Google and use Stack Overflow and use all the resources you have 7
on the Internet to figure out how you can change the code so that our home page now truncates the content 8
of each blog posts to only 100 characters. And then at the end there's just these three ... 9
and now we only have a preview of each blog post. 10
Pause the video and I want you to practice this really truly important programming skill of figuring 11
things out by yourself and try to complete this challenge. 12
So here's my first hint for you. 13
A journey of a thousand miles starts with a single step. 14
The programmer version of that is that the journey of solving any programming problem starts with a single 15
search. 16
So let's go ahead and try to succinctly explain to Google what it is that we want to know. 17
Now the goal that we want to achieve is to be able to truncate our string down to 100 characters using 18
Javascript. 19
So if we exclude all of the specific parts of our problem say the 100 characters right? Because somebody 20
else might have posted that they wanted to limit it to 10 characters. 21
So to widen our search let's try something like 22
truncate string Javascript. And the first answer we get on Stack Overflow 23
let's check it out. 24
Let's see what that query is to see if it matches our own. 25
So they want to truncate a dynamically loaded string using straight Javascript and all they care about 26
is just the number of characters. 27
That sounds exactly like what we want to do as well. 28
And the answer they explain is to use the substring method and they actually very kindly link to the 29
documentation as well as providing a code example. 30
So let's say that you have a string ABCDEFG and you want to truncate it down to say only 3 characters 31
long. Then you would say the string 32
call a method .substring on it and then inside the parentheses you specify a start position and an 33
end position where you want to cut up that string. 34
So now the outcome of my truncated string is now just ABC because it goes from 0 to length which is 3. 35
So from 0 to 3 then then that includes 0, 1, 2, 3 only has ABC right? 36
So now using this 37
can you apply it to the challenge and solve it for yourself?