Start learning free

LESSON 77 OF 289 · STRINGS - CHALLENGES

Stuttering Function in TypeScript: Slice and Join

Practice Read the full explanation

Given a word word, return it said with a stutter: its first two characters, then a hyphen -, then the whole word.

 

Example 1:

Input: word = "dog"
Output: "do-dog"
Explanation: The first two characters are "do". After them come a hyphen and the whole word, "dog".

Example 2:

Input: word = "Hello"
Output: "He-Hello"
Explanation: The characters keep their case: the stutter starts with an upper-case H, as the word does.

Example 3:

Input: word = "ab"
Output: "ab-ab"
Explanation: A word with only two letters: its first two characters are the whole word.

 

Constraints:

  • word has at least 2 letters.
  • word is made of English letters only, lowercase or uppercase: no spaces, digits or punctuation.
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Stuttering Function in TypeScript, explained

    Problem overview

    The method stutterize receives a word and returns it as if said with a stutter: its first two characters, a hyphen, then the whole word. "dog" becomes "do-dog". The case is kept, so "Hello" becomes "He-Hello". With the two-letter word "ab", the first two characters are the whole word, and the result is "ab-ab".

    Approach

    The result is made of three pieces, always in the same order: a short piece taken from the word, a fixed "-", and the word itself. The slice(start, end) method of a string returns the characters from start up to, but not including, end. So word.slice(0, 2) returns the characters at indexes 0 and 1.

    Step by step

    1. Take the first two characters with word.slice(0, 2).
    2. Add the hyphen as the string "-".
    3. Add the whole word and join the three pieces with +. Return the result.

    The solution in TypeScript

    class Solution {
        stutterize(word: string): string {
            return word.slice(0, 2) + "-" + word;
        }
    }
    

    Because the first piece is a slice, the original word is untouched, and it can be used again as the last piece. A string in TypeScript is never modified by slice.

    Tracing the example

    For word = "dog", the three pieces are "do", "-" and "dog". Joined from left to right, "do" + "-" is "do-", and adding the word gives "do-dog".

    Other ways to write it

    A template string expresses the same idea in one piece: `${word.slice(0, 2)}-${word}` also gives "do-dog". The lesson uses + because each of the three pieces is easy to see. The two forms produce the same string, so either passes the lesson's tests.

    The method needs no loop and no condition: the word's length doesn't change the shape of the answer, only the text in it. That is typical of string-building exercises. Find the pieces, put them in order, and join them.

    Why slice doesn't fail on short words

    The method slice stops at the end of the string when the end index is too large. For "ab", slice(0, 2) reaches exactly the end and returns "ab". Even for the one-letter "a", slice(0, 2) returns "a" and no error occurs.

    Compare reading single characters with square brackets. word[0] + word[1] also gives the first two characters of a word with two letters or more, but it needs two reads and joins them with +. For a word of one letter, word[1] is undefined and the join gives the text "aundefined". A slice says what you mean, "the first two characters", and handles the short case gracefully.

    The - in the result is an ordinary one-character string, like any other piece. It has no special meaning in TypeScript when it is inside quotes.

    Edge cases and common mistakes

    • Ending the slice at 1 or 3. The end index is excluded: slice(0, 1) gives one character and slice(0, 3) gives three.
    • A two-letter word. "ab".slice(0, 2) is "ab", so the result is "ab-ab".
    • A one-letter word. The lesson guarantees at least two letters. For a one-letter word "a", slice(0, 2) would still work and return "a", because slice stops at the end of the string, giving "a-a".
    • Changing the case. The stutter keeps the letters as they are, so the upper-case H of "Hello" stays upper case in both places.

    Testing

    • "dog" gives "do-dog".
    • "Hello" gives "He-Hello".
    • "ab" gives "ab-ab".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free