Start learning free

LESSON 74 OF 289 · STRINGS - CHALLENGES

Concat First and Last Char in TypeScript

Practice Read the full explanation

Given a string s, return a new string made of two characters: the first character of s, followed by its last character.

 

Example 1:

Input: s = "hello"
Output: "ho"
Explanation: The first character is "h" and the last one is "o".

Example 2:

Input: s = "Hello World!"
Output: "H!"
Explanation: Every character counts, so the last one is the "!", not the "d". The "H" stays upper case.

Example 3:

Input: s = "ab"
Output: "ab"
Explanation: With only two characters, the first is "a" and the last is "b".

 

Constraints:

  • s has at least 2 characters.
  • s contains English letters, digits, spaces and punctuation, and doesn't start or end with a space.
STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · STRINGS - CHALLENGES

    Concat First And Last Char in TypeScript, explained

    Problem overview

    The method concatFirstAndLastChar receives a string s of at least two characters and returns a new string with two characters: the first one of s, then the last one. For "hello" the result is "ho". For "Hello World!" it is "H!": every character counts, so the last one is the exclamation mark, and the H keeps its case. For "ab" the first and last characters are the whole string, so the result is "ab".

    Approach

    A string can be read by position with square brackets, and positions start at 0. The first character is at index 0. The last one is at index s.length - 1, because a string with 5 characters has the indexes 0 to 4. Reading a position gives a one-character string, and the + operator between two strings joins them into one.

    Step by step

    1. Read the first character with s[0].
    2. Read the last character with s[s.length - 1].
    3. Join them with + and return the result. Both parts are strings, so + concatenates them instead of adding numbers.

    The solution in TypeScript

    class Solution {
        concatFirstAndLastChar(s: string): string {
            return s[0] + s[s.length - 1];
        }
    }
    

    Two details are worth knowing:

    • s.length has no parentheses. In TypeScript length is a property of a string, not a method.
    • Square brackets give a string. TypeScript has no separate character type, so s[0] is a string of length 1. That is why + joins the two characters as text.

    Tracing the example

    For s = "hello" the length is 5, so the last index is 4. The expression s[0] reads "h" and s[s.length - 1] reads "o". Joined with +, they give "ho".

    TypeScript offers other ways to read the last character. s.at(-1) counts from the end, so it gives "o" without any arithmetic, and s.charAt(s.length - 1) gives the same result. A template string, `${s[0]}${s[s.length - 1]}`, also builds "ho". The lesson uses the bracket form because it shows how indexes and length relate, and that idea carries over to arrays.

    The type annotations s: string and : string are checked by the compiler and then erased: the JavaScript that runs has no types. They stop you from passing a number by mistake while you write the code, but they don't check anything while the program runs.

    Indexing a string

    A string is a sequence of characters, and each character has a position. For "hello" the positions are:

    • index 0 holds "h";
    • index 1 holds "e";
    • index 2 and index 3 both hold "l";
    • index 4 holds "o", and 4 is s.length - 1.

    Reading an index that doesn't exist doesn't stop the program. It gives undefined, and a later step may then fail or produce a surprising value such as NaN. That is why checking the index with the length, not guessing it, is the habit to build. Strings can't be changed through an index either: the compiler reports an error for an assignment such as s[0] = "j": an index of a string only permits reading.

    Edge cases and common mistakes

    • Using s[s.length] for the last character. That index is one past the end. For "ab" it gives undefined, while s[s.length - 1] gives "b".
    • Strings shorter than two characters. The lesson guarantees at least two characters. With a one-character string such as "x" the first and last character are the same one, and the result is "xx". With an empty string both reads give undefined and adding them gives NaN, which is why the guarantee matters.
    • Counting spaces and punctuation. They are characters like any other, so "Hello World!" ends with "!".

    Testing

    • "hello" gives "ho".
    • "Hello World!" gives "H!".
    • "ab" gives "ab".
    • "a1" gives "a1".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free