课程: JavaScript Practice: String Manipulation
Solution: Trim a user's new password - JavaScript教程
课程: JavaScript Practice: String Manipulation
Solution: Trim a user's new password
- [Instructor] In this challenge, we're given a password string that has empty spaces at both the beginning and the end of it. For this challenge, we're going to remove both spaces at the beginning and the end of the string. If we think about a user creating their password, they might accidentally include spaces at the beginning or the end or maybe even both. The password gets created and stored with the extra spaces and then when they try to log in, they didn't realize they'd added all those extra spaces and their password doesn't work. To prevent something like this from happening, we can use the string method trim that removes spaces at the beginning and end of a string. This method does not take any arguments. Here we are provided a password and we need to return the trimmed version. First, let's log out the original string's length. So on line 12, we are going to console.log the generatedPassword.length. All right, we'll run the test code here and this gives us 35. So now we know this original password that we had with both empty strings on the end is 35 characters in length. Now, let's check the length of the password after it's trimmed. We're going to, just in the same console log, right after Password, we're going to add .trim. All right, and then keep length and let's see how long this password is now that we've used the method trim. Now we see that it's 33. So this isn't the solution to our challenge. We want to return the new trimmed password. So let's get rid of what we've done here on line 12 and we're going to return generatedPassword.trim. All right, and if we test the code, we see, yes, that all tests have passed and our code returned generatedPassword with the strings removed on each end. Great work.
内容
-
-
-
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 秒
-
-