Capitalize Challenge
We have arrived at our first challenge. Every once in a while I'm going to ask you to figure out a problem or do something using what we have already learned. I will not ask you to do a challenge that involves doing something that we have not yet gone over.
If you feel stuck and can not figure it out on your own, that is absolutely fine. I will walk you through the solution(s), so that you can understand how to solve the problem.
Instructions:
Take the variable myString and capitalize the first letter of the word using some of the methods that we talked about in the last video. Put the result in a variable called myNewString.
Create multiple solutions if you would like.
Expected Result:
const myString = 'developer';
console.log(myNewString); // 'Developer'
Hints:
- You can use the
charAt()method as well asstring[index]to get the character at a specific index. - The
.toUpperCase()method will make the entire string uppercase substring()orslice()will return a specific portion of a string
Click For Solution
There are many ways to do this. Let's take a look at a few
// Solution 1
const myNewString = myString.charAt(0).toUpperCase() + myString.substring(1);
// Solution 2 (Uses string[0] instead of string.charAt(0))
const myNewString = myString[0].toUpperCase() + myString.substring(1);
// Solution 3 (Uses template literal and slice())
const myNewString = `${myString[0].toUpperCase()}${myString.slice(1)}`;
In all of these, we get the first character of the string, then we use the substring() or slice() method to get the rest of the string. We then use the toUpperCase() method to capitalize the first character and then we concatenate the result with the rest of the string.