Start learning free

LESSON 72 OF 285 · STRINGS - CHALLENGES

Concat First and Last Char in JavaScript

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

    JAVASCRIPT · STRINGS - CHALLENGES

    Concat First And Last Char in JavaScript, explained

    Problem overview

    The method concatFirstAndLastChar receives a string s with at least two characters. It returns a new two-character string: the first character of s followed by its last character.

    For "hello" the answer is "ho". For "Hello World!" it is "H!": every character counts, so the last one is the exclamation mark, and the capital H stays a capital. For "ab" the first and last characters are the whole string, so the answer is "ab".

    Approach

    A string in JavaScript is a sequence of characters numbered from 0. Writing s[0] gives the first one. The last character sits at index s.length - 1, because a string of length 5 has indexes 0 to 4. Reading a character with square brackets gives a one-character string, and + on two strings joins them as text.

    Step by step

    1. Read the first character with s[0].
    2. Work out the last index as s.length - 1 and read that character.
    3. Join the two with + and return the result.

    Tracing an example

    Take s = "hello". Its length is 5, so the indexes run from 0 to 4. The first character, s[0], is "h". The last index is s.length - 1, which is 4, and s[4] is "o". Joined with +, they make "ho". Reading index 5, one past the end, gives undefined instead of an error, which is why the last index must be computed from the length and not guessed.

    The solution in JavaScript

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

    Running it on the three examples gives these results:

    "hello"        -> "ho"
    "Hello World!" -> "H!"
    "ab"           -> "ab"
    

    Both operands of + are strings here, so the result is a string. If you read from an empty string, s[0] is undefined and undefined + undefined is NaN. The lesson guarantees at least two characters, so that case doesn't come up.

    Other ways to write it

    Two other forms give the same result. s.at(-1) reads the last character by counting from the end, so "hello".at(-1) is "o" and no length is needed. A template literal, `${s[0]}${s[s.length - 1]}`, joins the two characters into one string too. The lesson uses indexes and +, which are the basics to know first.

    The method charAt reads a character as well, but it differs from square brackets for a missing index: "hello".charAt(9) is an empty string, while "hello"[9] is undefined.

    Why no loop is needed

    The result always has two characters, whatever the length of s. Only two positions matter, the first and the last, so the code reads exactly those two. Looping over the string would visit characters that the answer never uses. Knowing a string's length and its first index is enough to reach both ends in constant time.

    Edge cases and common mistakes

    • Using s.length as the index. s[s.length] is one past the end and gives undefined. The last valid index is s.length - 1.
    • Looking for a letter. The last character can be a digit, a space-free punctuation mark or anything else. In "Hello World!" it is "!".
    • Changing the case. The characters are copied as they are, so no toUpperCase or toLowerCase is needed.

    Testing

    A few more inputs worth trying, with the results the solution gives:

    • "a1" gives "a1": the characters can be digits.
    • "x?" gives "x?": punctuation counts as a character.
    • "a b" gives "ab": the space in the middle is skipped because it is neither first nor last.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free