Problem overview
The method repeatLastChar receives a string s and a number n from 1 to 10. It returns the last character of s written n times in a row. For "banana" and 3 the answer is "aaa". The last character can be any character: for "Wow!" and 2 it is "!!". A one-character string such as "z" with n = 1 gives "z".
Approach
First find the last character, then repeat it. Indexes start at 0, so the last character of a string is at s.length - 1. For the repetition, a string has the repeat method: text.repeat(n) returns text written n times in a row. For example, "ab".repeat(3) is "ababab".
Step by step
- Read the last character with
s[s.length - 1]. This is a string of length 1. - Call
repeat(n)on it to write itntimes. - Return the result.
The solution in TypeScript
class Solution {
repeatLastChar(s: string, n: number): string {
return s[s.length - 1].repeat(n);
}
}
The call chains two operations: the square brackets produce a string, and .repeat(n) is called on that result. Since TypeScript has no separate character type, a single character is just a string, so it has the same methods as any other string.
Tracing the example
For s = "banana" and n = 3, the length is 6, so the last index is 5 and s[5] is "a". Then "a".repeat(3) is "aaa".
Note the difference from calling repeat on the whole string: "banana".repeat(2) is "bananabanana". The method repeats whatever string it is called on, so the choice of that string is the whole exercise.
Other ways to read the last character
s.at(-1) is another way to read the last character: a negative index counts from the end. Square brackets don't accept negative indexes this way, and s[-1] is undefined. The lesson uses s[s.length - 1], which works the same way for arrays, where it is common.
How repeat works
The repeat method takes one argument, a count, and returns a new string made of the original written that many times. Some values of the count:
"ab".repeat(3)is"ababab";"z".repeat(1)is"z", the same text once;"z".repeat(0)is"", an empty string.
The original string isn't changed; repeat, like the other string methods, returns a new string. Its argument must be a whole, non-negative number and not infinite, so the lesson's range from 1 to 10 is always safe.
No loop is needed for this task. Without repeat, you would build the result by adding the character to a string n times in a loop. The method does that work for you and states the idea directly: write this character n times.
Edge cases and common mistakes
- Using
s[s.length]. That index is past the end and givesundefined. Calling.repeaton it would stop the program with aTypeError. - Repeating the whole string.
s.repeat(n)repeats all ofs. Only the last character should be repeated. - The value of
n. The lesson keepsnbetween 1 and 10.repeat(0)returns an empty string, and a negative count throwsRangeError: Invalid count value: -1. - Trailing punctuation and spaces. They count as the last character like any other: for
"Wow!"the repeated character is"!", not"w".
Testing
"banana", 3 gives"aaa"."Wow!", 2 gives"!!"."z", 1 gives"z".
