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
- Read the first character with
s[0]. - Read the last character with
s[s.length - 1]. - 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.lengthhas no parentheses. In TypeScriptlengthis 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 iss.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 givesundefined, whiles[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 giveundefinedand adding them givesNaN, 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".
