-
Notifications
You must be signed in to change notification settings - Fork 1
/
joinPath.js
20 lines (17 loc) · 871 Bytes
/
joinPath.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Write a function that receives two portions of a path and joins them. The portions will be joined with the "/" separator.
// There could be only one separator and if it is not present it should be added.
// Examples:
// joinPath("portion1", "portion2") ➞ "portion1/portion2"
// joinPath("portion1/", "portion2") ➞ "portion1/portion2"
// joinPath("portion1", "/portion2") ➞ "portion1/portion2"
// joinPath("portion1/", "/portion2") ➞ "portion1/portion2"
// Notes:
// Try not to solve this challenge using only if-else conditions.
function joinPath(portion1, portion2) {
if (!portion1.includes("/") && !portion2.includes("/")) {
return portion1 + "/" + portion2;
} else if (portion1.includes("/") || portion2.includes("/")) {
return portion1.replace("/", "") + "/" + portion2.replace("/", "");
}
}
console.log(joinPath("portion1", "portion2"));