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
- Start with the fixed beginning,
"bu". - Add
"r".repeat(n)for the variable middle. - 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
nr'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 thebuand theptoo. Only the"r"repeats.
Testing
n = 1gives"burp".n = 2gives"burrp".n = 20gives"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.
