All right.
So how did that go?
Well I hope this was challenging but at the same time fun.
So the problem essentially boils down to, how do we selectively capitalize the first letter but not any
of the other letters.
So, as I said, let's break this problem down.
So, step one, we need to be able to create a variable that stores the name that the user enters via a prompt.
Now, step two, we need to be able to capitalize the first letter of their name, and then step three,
we use the capitalized version of their name to greet them using an alert.
Okay,
so let's try and tackle this one by one.
Let's create a var that we can call just name, and we can use it to store the name that they give us
via a prompt.
So let's delete this line, and
let's use our Console to verify that we're doing it right every step of the way.
So I'm going to put this into the Console and I'm going to paste it in here and hit Enter.
So now that line of code gets run and it's asking me for my name.
So I'm going to give it my name but all in lowercase and I'm going to hit OK.
So now if I check inside the console what is the current value of name,
then you can see it's lower case angela.
Okay, so the next part, which is the most challenging part,
how can you capitalize only the first letter of their name?
Well, if you look at this problem just by itself it seems like we can’t achieve it using only what we've
learned,
but we can actually break down this problem.
What if part a of this, we actually isolate the first character, and then we turn the first character
to uppercase, Step c we isolate the rest of the name,
and finally we concatenate the first character with the rest of the characters.
So now this seems a little bit more feasible,
right?
We've initially tried to break down the problem as we saw fit but then we realized that one of these
steps actually needed to be further broken down.
And now we can tackle it.
So isolate the first character. We know how to do that.
We can say var firstChar = name.slice, and the numbers we're going to put in here are
0 and 1 because we want to take the character beginning at the zeroth position up to but not including
the first position.
So that's going to be the first character of the string. So we can again verify this by copying it and
pasting it into our Console, hitting Enter,
and let's just check to see what firstChar is currently equal to.
So it's equal to the lower case a. Perfect.
So the next step, Step b, is we want to turn the first character to upper case.
So this is very easy as well.
Let’s create a var called upperCaseFirstChar, and it's now equal to firstChar, which is the lower case a
at the moment, .toUpperCase, and remember we need those two empty parentheses at the end.
Okay, so let's see if this worked by pasting this into our Console.
So what is the current value of upperCaseFirstChar?
It's capitalized A. Brilliant.
So now how do we isolate the rest of the name?
Let's create a variable called restOfName, and we will need to use the slice function again.
So we're going to take the name that they gave us, we're going to slice the characters so that we get
everything other than the first character.
Now how do we do that?
Well, the first part is easy.
We can start off at position 1.
So that excludes the first character which is at 0.
Now what about the last one?
It's pretty easy if we know that the name is Angela because we can count and we can see this is 1,
2, 3, 4, 5, 6 characters.
So, in order to get all the characters after the letter A,
all we need to do is to write 6 in here. Now, because we're getting the name through a prompt,
and we don't know what the user is going to enter,
then if we just put in 6,
it’s not always going to work.
It's only going to work if the name has 6 characters. But we've learned something really useful in
this case.
We know that we can get the number of characters through the property that is length. So we can simply
say name.length, and we'll be able to get the length of whatever name it is that the user enters,
so we will always be able to get the last position to put into our slice in order to get the rest of
the name.
So let's paste this into here as well and let's Run it and let's see what restOfName is equal to.
And there we go.
It's equal to ngela.
Beautiful name, right?
So now finally we can add our uppercase first character to the rest of the name in order to get our
capitalized name. We can use concatenation for this. We can say upperCaseFirstChar + restOfName
is now going to be equal to the name that is capitalized.
Paste that in here and let's check it out.
There we go.
We've now got the capitalized Angela. Pretty cool, right?
And now all we have to do is to put the capitalized version into the alert and greet the user by saying,
"Hello ,"
+ capitalisedName.
So now let's Run our code and it asks us for our name, so we'll give it angela, lowercase, hit OK,
and it changes our name to capitalized Angela and greets us through an alert.
So that might have seemed pretty complicated for achieving something quite simple.
And if you look on Stack Overflow or Google, there are simpler ways of doing it,
but this exercise is really, really crucial in starting to build an essential skill that programmers
have, which is taking a big problem, chunking it down, breaking it into smaller pieces, and whenever you get
stuck on those smaller pieces, then trying to break that problem down even further.
Now some of you guys might have realized that we actually need one extra thing to make our code completely
foolproof.
Because, what if the user, instead of just writing, you know, lower case angela, which we've addressed, what
if they wrote lower case to begin with, but then towards the end they added in some capitalized letters?
Our current code doesn't address this,
and so it still looks a bit weird.
Now this is completely optional.
You can either pause the video now and see if you can fix this bug or you can follow along with me and
I'll show you how to do this.
This is one of the reasons why testing your code is really important, because there's always these edge
cases that you might not have thought about but that might happen when you have thousands of users
using your website.
So now that we've achieved turning the first letter into uppercase, fixing this one small user experience
issue is going to be a walk in the park, because all we need to do is just to add another small step
in here.
We can call this one d and this one e, and we can say change the rest of the name to lowercase. So
we can take this variable we've already got, restOfName is now equal to the previous version of
restOfName.toLowerCase().
And now this changes that variable to make all of those characters lower case, and uses it in the concatenation
of the capitalized name.
So now if we run our code and we put in what we had before which is lower case ang, upper case ELA,
hit
OK.
And, voila, we get our perfect capitalized name no matter what the user decided to enter.
So, I hope you had fun with me in this module learning all about strings in JavaScript.
In the next module, we're going to learn more about numbers, doing maths with JavaScript and a lot, lot
more.
So I'll see you on the next module.