File tree Expand file tree Collapse file tree 1 file changed +20
-2
lines changed
Expand file tree Collapse file tree 1 file changed +20
-2
lines changed Original file line number Diff line number Diff line change 99
1010// (All spaces in the "" line should be ignored. They are purely for formatting.)
1111
12+ /* ================ lastIndexOf() ===================
13+ -we use this to obtain the last position of / where is located the File.txt
14+ -const base = filePath.slice(lastSlashIndex + 1); with + 1 we start to subtract after /
15+ Index: 0 1 2 ... 37 38 39 40 41 42
16+ filePath: / U s ... / f i l e .txt
17+ -
18+ */
19+
1220const filePath = "/Users/mitch/cyf/Module-JS1/week-1/interpret/file.txt" ;
1321const lastSlashIndex = filePath . lastIndexOf ( "/" ) ;
22+ console . log ( lastSlashIndex ) ; // 44
1423const base = filePath . slice ( lastSlashIndex + 1 ) ;
15- console . log ( `The base part of ${ filePath } is ${ base } ` ) ;
24+ console . log ( base ) ; // file.txt
25+ // console.log(`The base part of ${filePath} is ${base}`);
1626
1727// Create a variable to store the dir part of the filePath variable
18- // Create a variable to store the ext part of the variable
28+ const lastDotIndex = filePath . lastIndexOf ( "." ) ;
29+ console . log ( lastDotIndex ) ; // index position 49
30+ const extractDir = filePath . slice ( 0 , lastDotIndex ) ;
31+ console . log ( `Directory and filename (without extension) ${ extractDir } ` ) ;
32+
33+
34+ // Create a variable to store the ext part of the variable"
35+ const extensionName = filePath . slice ( lastDotIndex + 1 ) ;
36+ console . log ( `extension: ${ extensionName } ` ) ; // txt
You can’t perform that action at this time.
0 commit comments