Skip to content

Commit b25d953

Browse files
committed
file path sliced
1 parent 467717b commit b25d953

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

Sprint-1/1-key-exercises/3-paths.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
// (All spaces in the "" line should be ignored. They are purely for formatting.)
1111

1212
const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt";
13+
1314
const lastSlashIndex = filePath.lastIndexOf("/");
1415
const base = filePath.slice(lastSlashIndex + 1);
15-
console.log(`The base part of ${filePath} is ${base}`);
16-
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-
20-
const dir = ;
21-
const ext = ;
16+
// +1 to not include the slash in the base
17+
// lastSlashIndex finds position of the last / in the string
18+
// slice extracts part of the string from that position to the end
19+
const dir = filePath.slice(0, lastSlashIndex);
2220

23-
// https://www.google.com/search?q=slice+mdn
21+
const dotIndex = base.lastIndexOf(".");
22+
const ext = base.slice(dotIndex);
23+
// dotIndex finds position of the last . in the base string and slice extracts after this
24+
console.log(`The base part of the file path is ${base}, the dir part is ${dir}, and the ext part is ${ext}`);

0 commit comments

Comments
 (0)