Start learning free

LESSON 79 OF 289 · STRINGS - CHALLENGES

Burrrrp in TypeScript: Building a String with repeat()

Practice Read the full explanation

Given a number n, return the sound of a burp with n letters r: "bu", then n letters r, then "p".

 

Example 1:

Input: n = 4
Output: "burrrrp"
Explanation: "bu", then 4 letters r, then "p". That's the burp in this lesson's title.

Example 2:

Input: n = 1
Output: "burp"
Explanation: The shortest burp has a single r.

Example 3:

Input: n = 7
Output: "burrrrrrrp"
Explanation: "bu", then 7 letters r, then "p".

 

Constraints:

  • n is a whole number from 1 to 20.
  • The result is all lowercase.
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Burrrrp in TypeScript, explained

    Problem overview

    The method burrrrp receives a number n from 1 to 20 and returns a burp with n letters r: "bu", then n times r, then "p". With n = 4 the result is "burrrrp". With n = 1 it is "burp", and with n = 7 it is "burrrrrrrp".

    Approach

    Only the middle changes with n. The beginning, "bu", and the end, "p", are always the same. The repeat method builds the middle: "r".repeat(n) returns a string with n letters r in a row. The three pieces are joined with +.

    Step by step

    1. Start with "bu".
    2. Add "r".repeat(n) for the long part of the sound.
    3. End with "p" and return the whole string.

    The solution in TypeScript

    class Solution {
        burrrrp(n: number): string {
            return "bu" + "r".repeat(n) + "p";
        }
    }
    

    The + operator works from left to right: "bu" + "r".repeat(n) is a string, and then "p" is added to it. The call to repeat happens first, because a method call binds tighter than +.

    Tracing the example

    For n = 4, "r".repeat(4) is "rrrr". Adding "bu" in front gives "burrrr", and adding "p" at the end gives "burrrrp".

    Other ways to write it

    A template string gives the same result: `bu${"r".repeat(n)}p` is "burrrrp" for n = 4. The lesson uses + so that the three pieces are visible one after another.

    The number n is declared as a number, but TypeScript's types disappear when the code is compiled. At run time repeat converts its argument to a number itself: a value such as "3" would still repeat three times. Inside the lesson, the types keep you from passing text by mistake.

    Building strings from pieces

    Many string exercises have the same shape: some text is fixed, some depends on the input, and the answer is the pieces joined in order. Here:

    • the fixed start is "bu";
    • the variable middle is "r".repeat(n);
    • the fixed end is "p".

    Written as one expression, the whole function is a single return. A string method such as repeat returns a new string, so its result can be used right away in a larger expression.

    The result's length is n + 3: two letters for "bu", n for the r's and one for the "p". With n = 20, the largest input, that is 23 characters. Counting the pieces like this is a quick way to check an answer by eye before running the lesson's tests: for n = 7, the expected "burrrrrrrp" has 10 characters.

    Reading the lesson's types

    The signature burrrrp(n: number): string says the method takes a number and returns a string. TypeScript has one number type for whole numbers and decimals alike, so n could in principle be 2.5, and repeat would round it down to 2. The lesson's input is always a whole number from 1 to 20, so you don't need to handle that case.

    The method is part of a class, Solution, and it doesn't use any state: the result depends only on n. That is why the body is one line and why calling it twice with the same n gives the same string.

    Edge cases and common mistakes

    • Typing the r's by hand. A fixed string can't follow n. Use repeat.
    • Repeating the wrong piece. "bur".repeat(n) would repeat the "bu" too.
    • n = 0. The lesson starts at 1, but "r".repeat(0) is the empty string, so burrrrp with 0 returns "bup".
    • The largest input. With n = 20 the result has 20 letters r between the u and the p.

    Testing

    • 4 gives "burrrrp".
    • 1 gives "burp".
    • 7 gives "burrrrrrrp".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free