Problem overview
The method creatingIds receives a first name first and a last name last. The ID is the first 2 letters of the first name followed by the last 3 letters of the last name, with the letters keeping their case.
For "John" and "Smith" the ID is "Joith": "Jo" from the first name and "ith" from the last. For "Jo" and "Doe" the names are exactly 2 and 3 letters long, so both are used whole and the ID is "JoDoe". For "ada" and "Lovelace" it is "adace".
Approach
Each half of the ID is a slice of a name. slice(start, end) returns the characters from index start up to, but not including, index end. So first.slice(0, 2) gives the characters at indexes 0 and 1: two letters.
For the end of a word, give slice only a start index, and it runs to the end of the string. The last 3 letters of a name of length L start at index L - 3. In "Lovelace" the length is 8, so the start is 5 and slice(5) gives "ace".
Step by step
- Take the first two letters of
firstwithslice(0, 2). - Take the last three letters of
last, starting atlast.length - 3and running to the end. - Join the two pieces with
+and return them.
Checking the slice arithmetic
For "John", slice(0, 2) returns "Jo", while slice(0, 3) would return "Joh". For "Smith", the length is 5, so last.length - 3 is 2, and slice(2) returns "ith". Putting the pieces side by side gives "Jo" + "ith", which is "Joith".
The solution in JavaScript
class Solution {
creatingIds(first, last) {
return first.slice(0, 2) + last.slice(last.length - 3);
}
}
On the three examples it returns:
"John", "Smith" -> "Joith"
"Jo", "Doe" -> "JoDoe"
"ada", "Lovelace" -> "adace"
JavaScript also accepts a negative start in slice, which counts from the end: "Lovelace".slice(-3) is "ace" too. Both forms work, and the lesson's version spells out the arithmetic.
Short strings and negative starts
A slice never throws when it asks for more than the string has: "a".slice(0, 2) is just "a". The lesson guarantees at least 2 letters in the first name and 3 in the last, so each slice returns the full amount.
The ID could also be built with a template literal, `${first.slice(0, 2)}${last.slice(-3)}`, which gives "Joith" for the first example. It is the same idea written differently: two slices, joined.
Reading the statement as two slices
An ID built from "the first 2 letters" of one string and "the last 3 letters" of another is two independent slices, so each can be written and checked on its own. First check that first.slice(0, 2) returns the right two letters for a few names. Then check last.slice(last.length - 3). Only then join them. Splitting a task like this makes an off-by-one error easy to find: if the result has the wrong length, one of the two slices is wrong, and you can tell which by printing them separately.
Edge cases and common mistakes
- Using 3 as the end of the first slice.
slice(0, 3)would give three letters. The end index is not included, so two letters need an end of 2. - Names of exactly the minimum length. For
"Doe",last.length - 3is 0, so the slice starts at the beginning and returns the whole name. - Changing the case. The ID uses the letters as written, so
"ada"stays lower case.
Testing
"AL"and"XYZ"give"ALXYZ": both names are already at their minimum lengths and are used whole."Anna"and"Bee"give"AnBee".
