Start learning free

LESSON 76 OF 289 · STRINGS - CHALLENGES

Creating IDs in TypeScript: Slicing Names

Practice Read the full explanation

A website creates a user ID from each person's name: the first 2 letters of the first name, followed by the last 3 letters of the last name.

Given the first name first and the last name last, return the ID.

 

Example 1:

Input: first = "John", last = "Smith"
Output: "Joith"
Explanation: "Jo" are the first 2 letters of "John" and "ith" are the last 3 letters of "Smith".

Example 2:

Input: first = "Jo", last = "Doe"
Output: "JoDoe"
Explanation: "Jo" has exactly 2 letters and "Doe" exactly 3, so both names are used whole.

Example 3:

Input: first = "ada", last = "Lovelace"
Output: "adace"
Explanation: The letters keep their case: the ID doesn't change any letter to upper or lower case.

 

Constraints:

  • first has at least 2 letters and last has at least 3 letters.
  • Both names are made of English letters only, lowercase or uppercase.
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Creating Ids in TypeScript, explained

    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 first are first.slice(0, 2): indexes 0 and 1.
    • The last 3 letters of last start at index last.length - 3 and 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

    1. Take the first two letters of first with slice(0, 2).
    2. Take the last three letters of last with slice from last.length - 3, with no end index.
    3. 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 toUpperCase or toLowerCase.
    • Slicing the wrong name. The first part comes from first and the last part from last. Swapping them gives a different ID.

    Testing

    • "John", "Smith" gives "Joith".
    • "Jo", "Doe" gives "JoDoe".
    • "ada", "Lovelace" gives "adace".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free