Skip to content

Commit 25632b9

Browse files
committed
3-paths.js
1 parent 5357eab commit 25632b9

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Sprint-1/1-key-exercises/2-initials.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,16 @@ let initials = [firstName, middleName, lastName].map(name => name.charAt(0)).joi
2020

2121
/*
2222
There are more efficient ways of implementing the solution, however these concepts haven't been covered yet
23-
In these scenarios the slice and charAt have been implemented through a map
23+
In these scenarios the slice and charAt have been implemented through an array and map
2424
2525
let initials = [firstName, middleName, lastName].map(name => name.slice(0,1)).join('')
2626
let initials = [firstName, middleName, lastName].map(name => name.charAt(0)).join('')
2727
28+
29+
Direct use of `charAt`, `slice`, or indexing is simple and readable for a fixed number of names but becomes repetitive and less scalable.
30+
Using an array with `map` is concise, scalable, and handles missing names well, making it best for variable-length inputs.
31+
Template literals or concatenation with `||` are concise and handle missing names, but are still repetitive for many name parts.
32+
For flexibility, the array + map approach is generally preferred.
2833
*/
2934

3035
// https://www.google.com/search?q=get+first+character+of+string+mdn

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ const base = filePath.slice(lastSlashIndex + 1);
1515
console.log(`The base part of ${filePath} is ${base}`);
1616

1717
// Create a variable to store the dir part of the filePath variable
18+
const dir = filePath.slice(0, lastSlashIndex); // Extract the directory part (everything before the last slash)
19+
20+
21+
22+
1823
// Create a variable to store the ext part of the variable
24+
const lastDotIndex = base.lastIndexOf("."); // Find the index of the last dot in the base to get the extension
25+
const ext = lastDotIndex !== -1 ? base.slice(lastDotIndex) : ""; // Extract the extension (including the dot), or empty string if none
26+
27+
28+
29+
1930

20-
const dir = ;
21-
const ext = ;
31+
console.log(`The dir part of ${filePath} is ${dir}`);
32+
console.log(`The ext part of ${filePath} is ${ext}`);
2233

2334
// https://www.google.com/search?q=slice+mdn

0 commit comments

Comments
 (0)