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
- Read the first character with
s[0]. - Work out the last index as
s.length - 1and read that character. - 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.lengthas the index.s[s.length]is one past the end and givesundefined. The last valid index iss.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
toUpperCaseortoLowerCaseis 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.
