Problem overview
The method creatingIds receives a first name first and a last name last and returns a user ID: the first 2 letters of the first name followed by the last 3 letters of the last name. For "John" and "Smith" the ID is "Joith". For "Jo" and "Doe" both names are used whole, giving "JoDoe". Letters keep their case, so "ada" and "Lovelace" give "adace".
Approach
Both parts are slices of a string. slice(start, end) returns the characters from index start up to, but not including, index end. If end is left out, the slice runs to the end of the string.
- The first 2 letters of
firstarefirst.slice(0, 2): indexes 0 and 1. - The last 3 letters of
laststart at indexlast.length - 3and run to the end:last.slice(last.length - 3).
For "Smith" the length is 5, so the start index is 2 and the slice is "ith".
Step by step
- Take the first two letters of
firstwithslice(0, 2). - Take the last three letters of
lastwithslicefromlast.length - 3, with no end index. - Join the two slices with
+and return the result.
The solution in TypeScript
class Solution {
creatingIds(first: string, last: string): string {
return first.slice(0, 2) + last.slice(last.length - 3);
}
}
slice never changes the original string; it returns a new one. It also doesn't fail when the range is larger than the string: it just returns what exists.
A negative start index counts from the end, so last.slice(-3) is a shorter way to write the same thing. The two forms differ only when the string is shorter than 3 characters, which the lesson rules out. For "Do", slice(-3) gives "Do", but slice("Do".length - 3) is slice(-1), which gives only "o".
Tracing the example
For first = "John", first.slice(0, 2) is "Jo". For last = "Smith", the length is 5, so last.length - 3 is 2 and last.slice(2) is "ith". The result is "Jo" + "ith", that is "Joith".
The same two slice calls handle every valid input, because the first name always has at least 2 letters and the last name at least 3. Working with lengths, not with fixed positions, is what lets the same code serve "Smith" and "Lovelace".
Why the end index is left out
The two slices are written differently on purpose. For the front of a string you know where to stop (index 2), so you give both numbers. For the back of a string you know where to start (three before the end) and want everything after it, so you give only the start. A slice with a start index and no end always runs to the last character, whatever the length is.
Written with the end index, the second slice would be last.slice(last.length - 3, last.length), which gives the same result with more to read.
Both calls return new strings and leave first and last as they were. The function also doesn't need to know whether the letters are upper case or lower case, since slicing copies characters unchanged. Names with the exact minimum length are the boundary case: "Jo".slice(0, 2) is the whole name, and "Doe".slice(0) is the whole name too.
Edge cases and common mistakes
- Off-by-one in the end index. The end index of a slice is excluded, so
slice(0, 2)gives two letters, not three. - Names of exactly the minimum length.
"Jo"and"Doe"give"Jo"and"Doe"whole, so the ID is"JoDoe". - Changing the case. The ID keeps each letter as written; nothing calls
toUpperCaseortoLowerCase. - Slicing the wrong name. The first part comes from
firstand the last part fromlast. Swapping them gives a different ID.
Testing
"John","Smith"gives"Joith"."Jo","Doe"gives"JoDoe"."ada","Lovelace"gives"adace".
