You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: L-B/0026 FileExtension/README.md
+10-5Lines changed: 10 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
## Problem
4
4
5
-
Write a function that takes a string and returns the file extension, which is the string after the last dot (.) in the string. If there is no dot in the string, return undefined.
5
+
Write a function that takes a string and returns the file extension, which is the string after the last dot (.) in the string. If there is no dot in the string, return empty string.
6
6
7
7
## Solution
8
8
@@ -11,11 +11,16 @@ function extractFileExtension(fileName) {
11
11
// Split the file name into an array of strings at the dot character.
12
12
constsplitFileName=fileName.split(".");
13
13
14
-
// Get the last element in the array, which is the file extension.
15
-
constfileExtension=splitFileName.pop();
14
+
// Check if the split file name array has more than one element.
15
+
if (splitFileName.length>1) {
16
+
// Get the last element in the array, which is the file extension.
17
+
constfileExtension=splitFileName.pop();
16
18
17
-
// Return the file extension.
18
-
return fileExtension;
19
+
// Return the file extension.
20
+
return fileExtension;
21
+
}
22
+
// The file name does not have an extension, so return an empty string.
0 commit comments