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 │
77// " / home/user/dir / file .txt "
88// └──────┴──────────────┴──────┴─────┘
99
10- // (All spaces in the "" line should be ignored. They are purely for formatting.)
11-
1210const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt" ;
11+
12+ // Find last slash to separate dir and base
1313const lastSlashIndex = filePath . lastIndexOf ( "/" ) ;
14+
15+ // base = name + ext (e.g. "file.txt")
1416const base = filePath . slice ( lastSlashIndex + 1 ) ;
1517console . 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
2020const 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
2428console . log ( `The dir part of ${ filePath } is ${ dir } ` ) ;
2529console . 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