Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 16e30ac

Browse files
FileExtension readme update
1 parent 9362b78 commit 16e30ac

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

‎L-B/0026 FileExtension/README.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## Problem
44

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.
66

77
## Solution
88

@@ -11,11 +11,16 @@ function extractFileExtension(fileName) {
1111
// Split the file name into an array of strings at the dot character.
1212
const splitFileName = fileName.split(".");
1313

14-
// Get the last element in the array, which is the file extension.
15-
const fileExtension = 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+
const fileExtension = splitFileName.pop();
1618

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.
23+
return "";
1924
}
2025
```
2126

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /