课程: JavaScript Practice: String Manipulation

Solution: Transform user input to lowercase - JavaScript教程

课程: JavaScript Practice: String Manipulation

Solution: Transform user input to lowercase

- [Instructor] In this challenge, we're given some user input that contains upper and lowercase letters. It represents a city, so we can more or less assume that a letter capitalized at the end of the word, or maybe a lowercase letter at the very beginning might be a typo on the part of the user. The solution for this challenge is actually pretty straightforward. We have the city string that is passed to our transform city to lowercase function on line 10. For this challenge, we just need to return the updated string in all lowercase and can reach for the string method, toLowerCase. So let's go ahead and start with return on line 12. And we're going to take our city and apply the toLowerCase method. One thing I want to point out about this method, actually a few things. One is that you can see toLowerCase is in what's called camel case, so the t is lowercase and then the L and the C are uppercase. That's one thing to keep in mind. I know a few times when I've used this myself, I've accidentally made the word lowercase, capital L, and then everything else lowercase. So just keep that in mind with this method. The next thing is that the toLowerCase method does not take any arguments. So all you have to do is just apply it to whatever string that you want to make lowercase and voila, that's it. So let's go ahead and run it. And excellent work. You can see that it returned the city Fuquay Varina, which is my hometown, in all lowercase letters. Excellent work.

内容