Start learning free

LESSON 74 OF 285 · STRINGS - CHALLENGES

Creating IDs in JavaScript: Slicing Strings

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

    JAVASCRIPT · STRINGS - CHALLENGES

    Creating Ids in JavaScript, explained

    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

    1. Take the first two letters of first with slice(0, 2).
    2. Take the last three letters of last, starting at last.length - 3 and running to the end.
    3. 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 - 3 is 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".
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free