Start learning free

LESSON 77 OF 285 · STRINGS - CHALLENGES

Burrrrp in JavaScript: 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

    JAVASCRIPT · STRINGS - CHALLENGES

    Burrrrp in JavaScript, explained

    Problem overview

    The method burrrrp receives a number n and returns a burp with n letters r: "bu", then n times "r", then "p". The result is all lower case.

    With n = 4 the answer is "burrrrp". With n = 1 it is the shortest burp, "burp". With n = 7 it is "burrrrrrrp".

    Approach

    Only the r's change with n. The start, "bu", and the end, "p", are always the same. The string method repeat(count) writes a string count times in a row, so "r".repeat(n) gives exactly the middle part. The three pieces are joined with +.

    Step by step

    1. Start with the fixed beginning, "bu".
    2. Add "r".repeat(n) for the variable middle.
    3. Add the fixed ending, "p", and return the result.

    Building the string piece by piece

    For n = 4, "r".repeat(4) is "rrrr". Joined between the two fixed pieces, "bu" + "rrrr" + "p" is "burrrrp". Because repeat returns a string, the result of the whole expression is a string, and + only joins text: there is no addition of numbers anywhere, so n is never added to a string by accident.

    The same function covers every size. With n = 2 it gives "burrp", and with n = 20, the largest value the lesson uses, it gives a string of 23 characters: the 2 of "bu", the 20 r's and the final "p".

    The solution in JavaScript

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

    On the examples it returns:

    4 -> "burrrrp"
    1 -> "burp"
    7 -> "burrrrrrrp"
    

    Numbers and strings

    The operator + joins text, but if one side is a number it converts the number to text: "bu" + 4 + "p" is "bu4p". That is the mistake to avoid when a number like n should control a count: pass it to repeat, don't add it to the string.

    A template literal builds the same result: `bu${"r".repeat(n)}p` gives "burrrrp" for n = 4.

    Thinking about the problem

    Look at the examples side by side: "burp", "burrp", "burrrrp". Everything is the same except the number of r's. Pulling out what changes and writing the rest as fixed text is a habit worth building: the code then reads like the sentence in the statement, "bu, then n letters r, then p". A function that needs to produce a repeating pattern of any length can usually be written in this form.

    When a result is the wrong length, count the r's in the output and compare with n. A difference of exactly one points to an off-by-one in the count, and a difference of a different size points to a mistake in how the pieces are joined.

    Edge cases and common mistakes

    • Off-by-one in the count. The burp has n r's in total. Writing "r".repeat(n - 1) or adding an extra "r" by hand gives the wrong length.
    • Repeating the whole word. ("bu" + "r" + "p").repeat(n) repeats the bu and the p too. Only the "r" repeats.

    Testing

    • n = 1 gives "burp".
    • n = 2 gives "burrp".
    • n = 20 gives "burrrrrrrrrrrrrrrrrrrrp", which is 23 characters long.

    To see the mistake of repeating the wrong piece, run ("bu" + "r" + "p").repeat(3): it gives "burpburpburp", three whole burps instead of one longer one.

    Another way to write it is a loop that adds one "r" per turn, but repeat says the same thing in one expression.

    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free