Problem overview
The method repeatLastChar receives a string s and a number n, and returns the last character of s written n times in a row.
For "banana" and 3 the last character is "a", so the answer is "aaa". For "Wow!" and 2 it is "!!", because any character can be the last one. For "z" and 1 it is "z".
Approach
There are two parts: find the last character, then repeat it. The last character is at index s.length - 1, since indexes start at 0. Reading it with square brackets gives a one-character string.
Strings have a repeat(count) method that returns the string written count times in a row: "ab".repeat(3) is "ababab". No loop is needed.
Step by step
- Get the last character with
s[s.length - 1]. - Call
repeat(n)on it and return the result.
How repeat works
repeat takes one argument, the count, and returns a new string. "b".repeat(3) is "bbb" and "ab".repeat(2) is "abab". A count of 0 returns an empty string. A negative count throws a RangeError with the message Invalid count value: -1, but the lesson only uses counts from 1 to 10.
The solution in JavaScript
class Solution {
repeatLastChar(s, n) {
return s[s.length - 1].repeat(n);
}
}
On the examples it returns:
"banana", 3 -> "aaa"
"Wow!", 2 -> "!!"
"z", 1 -> "z"
Method calls chain left to right: s[s.length - 1] is evaluated first, and .repeat(n) is called on the string it produces. With n equal to 0, repeat returns an empty string, but the lesson keeps n between 1 and 10.
Other ways to get the last character
The method at counts from the end with a negative number: s.at(-1) is the last character, so s.at(-1).repeat(n) works too, and "hello".at(-1) is "o". The lesson uses s[s.length - 1], the form that works in every language.
A fractional count is cut down to a whole number, so "ab".repeat(2.9) is "abab". The lesson's n is always a whole number.
Thinking about the problem
The problem splits into a choice, which character, and a count, how many times. The choice depends only on s and the count only on n, so the two don't interfere. First reduce s to its last character, which is a one-character string. Then the question becomes "write this string n times", which is exactly what repeat answers. The string may contain spaces, digits and punctuation, but its last character is still just a character, and repeating it works the same way.
Print s[s.length - 1] on its own before calling repeat. If it shows the right character, the only part left to check is the count, and any mistake must be there.
The same two-step shape, pick a piece and then transform it, appears in many string exercises.
Edge cases and common mistakes
- Reading
s[s.length]. That is one past the end and givesundefined, and callingrepeatonundefinedthrows aTypeError. - Repeating the whole string.
s.repeat(n)repeats all ofs. Only the last character should be repeated. - A one-character string. Its first and last characters are the same, and the formula still works.
Testing
"hello"with 5 gives"ooooo"."a1"with 10 gives"1111111111": digits are characters like any other."a b"with 2 gives"bb".
