Problem overview
The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. For "photo.jpeg" that is "jpeg". A name can contain several dots, and only the last one matters: "archive.tar.gz" gives "gz". The extension keeps its letters exactly as written, so "README.MD" gives "MD". The lesson guarantees that the name has at least one dot and doesn't start or end with one.
Approach
The idea is to cut the name at every dot and keep the last piece. The split method of a string does the cutting: fileName.split(".") returns an array of strings, the parts between the separators, and the separator itself is not included. For "archive.tar.gz" the result is ["archive", "tar", "gz"]. For "photo.jpeg" it is ["photo", "jpeg"].
Whatever the number of dots, the extension is always the last item of that array.
Step by step
- Split the name at the dots. Store the array in
parts, declared withletand the typestring[], an array of strings. - Read the last item. An array is numbered from 0, like a string, and has a
length. Its last item is at indexparts.length - 1. Return it.
The solution in TypeScript
class Solution {
fileExtension(fileName: string): string {
let parts: string[] = fileName.split(".");
return parts[parts.length - 1];
}
}
A few details about the code:
- The separator is a string, not a pattern.
split(".")cuts at the dot character itself. The dot has no special meaning here, because the argument is a plain string and not a regular expression. string[]is an array type. It says that every item ofpartsis a string. The annotation is checked by the compiler and erased from the JavaScript that runs.- The array's length is one more than the number of dots.
"archive.tar.gz"has two dots and gives 3 parts. The last index is thereforeparts.length - 1, which is 2 here.
The string[] that split returns is a new array. The original fileName is not changed, because strings can't be modified in place.
Tracing the example
For "archive.tar.gz", parts is ["archive", "tar", "gz"] and parts.length is 3. The index 3 - 1 is 2, and parts[2] is "gz". For "my-file_v2.final.txt" the same code returns "txt": the hyphen and the underscore are ordinary characters and don't affect the cut.
Other ways to write it
The method at reads from the end, so fileName.split(".").at(-1) also gives "gz". Another approach finds the last dot with lastIndexOf("."), which is 11 in "archive.tar.gz", and slices from the next position: fileName.slice(fileName.lastIndexOf(".") + 1). Both return "gz". The lesson's version teaches arrays and indexing, and it needs no arithmetic with positions in the string.
Edge cases and common mistakes
- Taking
parts[1]. That works for"photo.jpeg"but returns"tar"for"archive.tar.gz". The extension is the last part, not the second. - Using
parts[parts.length]. That index is one past the end and givesundefined. - Changing the case. The extension is returned as written, so
"MD"stays"MD". - A name without a dot. The lesson rules it out. For
"README",split(".")gives["README"], so the code would return the whole name,"README".
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD"."my-file_v2.final.txt"gives"txt".
