课程: JavaScript Practice: String Manipulation
Solution: Concat first and last name - JavaScript教程
课程: JavaScript Practice: String Manipulation
Solution: Concat first and last name
- [Instructor] For our final strings challenge in this course, we are going to take two strings and combine them into one. This type of task is a very common one that you may have encountered on your own already. Like many other areas of JavaScript, there are actually more than one way to combine several strings together, but since we're discussing JavaScript string methods, let's use the string method concat. I encourage you to give concat a try first, and then for an extra challenge, after you completed the lesson, see what other methods you could use to complete this challenge. The string method concat can take one or more strings as arguments to construct the new string. Let's go ahead and get started on line 12 in our combineFirsAndLast function. This function is passed two arguments, firstName and lastName. Let's start on line 12 by writing return firstName.concat. We want to start with firstName because we want to apply concat to the first part of the string that we want to return. So if we were going to do Berkman Barry, then we would start with lastName, apply concat to that string, and then pass in firstName. So here we have firstName.concat and our argument is going to be lastName. Now let's run this and see if it returns what we might expect. So here on our first try, we see that it's not exactly what we might've expected. The string is returned with no space between first and last name, and part of the challenge is to return the string with that space. So the first argument that we'll actually want to pass to concat is a string with a space, and then we'll separate these arguments by a comma. So let's say we had, for example, firstName, middleName, lastName. We would just continue to add those strings here in the proper order, of course, but you can pass in to concat as many arguments as need be. So let's go ahead and run this again, and now, awesome job. We have our string, Barry Berkman, our first and last name with that space between like we were expecting, awesome job.
内容
-
-
-
Solution: Determine password length2 分钟 36 秒
-
Solution: Trim a user's new password2 分钟 8 秒
-
Solution: Check phone number format1 分钟 54 秒
-
Solution: Transform user input to lowercase1 分钟 39 秒
-
Solution: Display a page heading in uppercase1 分钟 3 秒
-
Solution: Does the message include "fun"?2 分钟 15 秒
-
Solution: Split your email list2 分钟 8 秒
-
Solution: Slice your account number display1 分钟 59 秒
-
Solution: Replace the typo1 分钟 44 秒
-
Solution: Concat first and last name2 分钟 21 秒
-
-