@@ -24,14 +24,32 @@ console.log(`The dir part of ${filePath} is ${dir}`);
2424console.log(`The ext part of ${filePath} is ${ext}`);
2525*/
2626
27- function pathFinder ( path ) {
28- const lastSlash = path . lastIndexOf ( "/" ) ;
29- const dir = path . slice ( 0 , lastSlash ) ;
30- const base = path . slice ( lastSlash + 1 ) ;
31- const dot = base . lastIndexOf ( "." ) ;
32- const ext = dot !== - 1 ? base . slice ( dot ) : "" ;
33-
34- return { dir, base, ext } ;
27+ function pathFinder ( userInput , filePath ) {
28+ // 1. Check if the user input exists inside the full path
29+ if ( ! fullPath . includes ( userInput ) ) {
30+ console . log ( "Input not found in path" ) ;
31+ return ;
32+ }
33+
34+ // 2. Split the path into parts
35+ const parts = filePath . split ( "/" ) ;
36+
37+ // 3. Base = last element
38+ const base = parts [ parts . length - 1 ] ;
39+
40+ // 4. Dir = everything except the last element
41+ const dir = parts . slice ( 0 , parts . length - 1 ) . join ( "/" ) ;
42+
43+ // 5. Extension logic
44+ const dotIndex = base . lastIndexOf ( "." ) ;
45+ const ext = dotIndex === - 1 ? "" : base . slice ( dotIndex ) ;
46+
47+ // 6. Print everything
48+ console . log ( `Full path: ${ filePath } ` ) ;
49+ console . log ( `Directory: ${ dir } ` ) ;
50+ console . log ( `Base: ${ base } ` ) ;
51+ console . log ( `Extension: ${ ext } ` ) ;
52+
3553}
3654
3755console . log ( pathFinder ( filePath ) ) ;
0 commit comments