Skip to content

Commit e2f242e

Browse files
committed
Enhance comments for clarity and remove unnecessary lines in 3-paths.js
1 parent 6106da7 commit e2f242e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed
Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// The diagram below shows the different names for parts of a file path on a Unix operating system
1+
/// The diagram below shows the different names for parts of a file path on a Unix operating system
22

33
// ┌─────────────────────┬────────────┐
44
// │ dir │ base │
@@ -7,20 +7,27 @@
77
// " / home/user/dir / file .txt "
88
// └──────┴──────────────┴──────┴─────┘
99

10-
// (All spaces in the "" line should be ignored. They are purely for formatting.)
11-
1210
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
11+
12+
// Find last slash to separate dir and base
1313
const lastSlashIndex = filePath.lastIndexOf("/");
14+
15+
// base = name + ext (e.g. "file.txt")
1416
const base = filePath.slice(lastSlashIndex + 1);
1517
console.log(`The base part of ${filePath} is ${base}`);
1618

17-
// Create a variable to store the dir part of the filePath variable
18-
// Create a variable to store the ext part of the variable
19-
19+
// dir = everything before the last slash
2020
const dir = filePath.slice(0, lastSlashIndex);
21-
const ext = filePath.slice(lastSlashIndex);
2221

22+
// Find last dot to extract only extension
23+
const lastDotIndex = filePath.lastIndexOf(".");
24+
25+
// ext = everything from the last dot onward
26+
const ext = filePath.slice(lastDotIndex);
2327

2428
console.log(`The dir part of ${filePath} is ${dir}`);
2529
console.log(`The ext part of ${filePath} is ${ext}`);
26-
// https://www.google.com/search?q=slice+mdn
30+
31+
// https://www.google.com/search?q=slice+mdn
32+
33+

0 commit comments

Comments
 (0)