-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt1.txt
58 lines (42 loc) · 1.39 KB
/
prompt1.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
URLs cannot have spaces. Instead, all spaces in a string are replaced with %20. Write an algorithm that replaces all spaces in a string with %20.
You may not use the replace() method or regular expressions to solve this problem. Solve the problem with and without recursion.
Example
Input: "Jasmine Ann Jones"
Output: "Jasmine%20Ann%20Jones"
__________________________________________________________________
input - string
output - string
all spaces replaced with %20
! no regex
! no .replace
solve w recursion & w.o recursion
- w.o recursion initial thought: for loop
.split
turn into an array
array > string .join
WITHOUT RECURSION:
const stringToUrl = (input) => {
if (typeof(input) === 'string') {
const splitString = input.split(" ");
const stringArray = Array.from(splitString);
const result = stringArray.join("%20");
return result;
} else {
return "Please enter a string!";
};
};
string = "Jasmine Ann Jones";
splitString = "Jasmine", "Ann", "Jones";
stringArray = ["Jasmine", "Ann", "Jones"];
result: "Jasmine%20Ann%20Jones";
WITH RECURSION:
const stringToUrl = (input) => {
let firstSpace = input.indexOf(" ");
if (firstSpace === -1) {
return input;
}
let firstWord = input.substring(0, firstSpace);
let updatedFirstWord = firstWord + "%20";
let restOfInput = input.substring(firstSpace + 1);
return stringToUrl(updatedFirstWord + restOfInput);
};