Start learning free

LESSON 76 OF 671 · STRINGS - CHALLENGES

Repeat Last Char in JavaScript: String 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

    JAVASCRIPT · STRINGS - CHALLENGES

    Repeat Last Char in JavaScript, explained

    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

    1. Get the last character with s[s.length - 1].
    2. 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 gives undefined, and calling repeat on undefined throws a TypeError.
    • Repeating the whole string. s.repeat(n) repeats all of s. 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".
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free