Problem overview
The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. The name has at least one dot and neither starts nor ends with one.
"photo.jpeg" gives "jpeg". "archive.tar.gz" has two dots, and only what follows the last one counts, so the answer is "gz". "README.MD" gives "MD": the letters keep their case.
Approach
The method split(".") cuts a string at every dot and returns an array of the pieces. "archive.tar.gz".split(".") is ["archive", "tar", "gz"]. The extension is always the last piece, however many dots the name has.
An array is numbered from 0 and has a length, just like a string, so its last item is at index parts.length - 1.
Step by step
- Split the name at the dots into an array called
parts. - Return the last item of
parts, at indexparts.length - 1.
What split returns
For "photo.jpeg", split(".") returns ["photo", "jpeg"], an array of length 2, so the last index is 1. For "archive.tar.gz" it returns ["archive", "tar", "gz"], an array of length 3, so the last index is 2. The formula parts.length - 1 adapts to the number of dots, which a fixed index can't do.
The solution in JavaScript
class Solution {
fileExtension(fileName) {
let parts = fileName.split(".");
return parts[parts.length - 1];
}
}
On the examples it returns:
"photo.jpeg" -> "jpeg"
"archive.tar.gz" -> "gz"
"README.MD" -> "MD"
The dot passed to split is a plain string, not a pattern, so it matches a literal dot. Splitting never changes the letters of the pieces.
Another way
You can also find the last dot directly. lastIndexOf(".") returns the index of the last dot, and slice with the index after it returns the rest: for "archive.tar.gz", lastIndexOf(".") is 11 and fileName.slice(11 + 1) is "gz". This avoids building an array. The lesson uses split, which also shows how arrays are indexed.
Thinking about the problem
The statement defines the extension as the part after the last dot. A name can have several dots, so the first dot is not the one that matters. Cutting at every dot turns the name into pieces, and the extension is the final piece. The name never starts or ends with a dot, so no piece is empty: "archive.tar.gz" never produces an empty item at either end. That is why the last piece is always a real extension.
Edge cases and common mistakes
- Taking
parts[1]. That works for"photo.jpeg"but returns"tar"for"archive.tar.gz". Use the last index. - Using
parts[parts.length]. That is one past the end and givesundefined. - A name without a dot.
"noext".split(".")is["noext"], so the code would return the whole name. The lesson guarantees at least one dot. - Changing the case. The extension is returned exactly as written.
Testing
"my-file_1.v2.txt"gives"txt": hyphens, underscores and digits stay inside their parts."a.b"gives"b": a one-letter extension is fine.
An array method such as at(-1) also reads the last item: "a.b".split(".").at(-1) is "b". The lesson uses the index parts.length - 1, which works the same way and is the form you will meet in older code.
