-
-
Notifications
You must be signed in to change notification settings - Fork 337
Expand file tree
/
Copy path3-paths.js
More file actions
28 lines (20 loc) · 1.18 KB
/
3-paths.js
File metadata and controls
28 lines (20 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// The diagram below shows the different names for parts of a file path on a Unix operating system
// ┌─────────────────────┬────────────┐
// │ dir │ base │
// ├──────┬ ├──────┬─────┤
// │ root │ │ name │ ext │
// " / home/user/dir / file .txt "
// └──────┴──────────────┴──────┴─────┘
// (All spaces in the "" line should be ignored. They are purely for formatting.)
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
const lastSlashIndex = filePath.lastIndexOf("/");
const base = filePath.slice(lastSlashIndex + 1);
console.log(`The base part of ${filePath} is ${base}`);
// Create a variable to store the dir part of the filePath variable
// Create a variable to store the ext part of the variable
const lastDotIndex = filePath.lastIndexOf(".");
const dir = filePath.slice(0, lastSlashIndex);
const ext = filePath.slice(lastDotIndex + 1);
console.log(dir);
console.log(ext);
// https://www.google.com/search?q=slice+mdn