Start learning free

LESSON 78 OF 289 · STRINGS - CHALLENGES

Repeat Last Char in TypeScript with repeat()

Practice Read the full explanation

Given a string s and a number n, return a new string made of the last character of s, written n times in a row.

 

Example 1:

Input: s = "banana", n = 3
Output: "aaa"
Explanation: The last character of "banana" is "a". Written 3 times, it gives "aaa".

Example 2:

Input: s = "Wow!", n = 2
Output: "!!"
Explanation: The last character can be any character: here it is the exclamation mark.

Example 3:

Input: s = "z", n = 1
Output: "z"
Explanation: A string of one character: its last character is "z", written once.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
  • n is a whole number from 1 to 10.
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Repeat Last Char in TypeScript, explained

    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

    1. Read the last character with s[s.length - 1]. This is a string of length 1.
    2. Call repeat(n) on it to write it n times.
    3. 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 gives undefined. Calling .repeat on it would stop the program with a TypeError.
    • Repeating the whole string. s.repeat(n) repeats all of s. Only the last character should be repeated.
    • The value of n. The lesson keeps n between 1 and 10. repeat(0) returns an empty string, and a negative count throws RangeError: 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".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free