Skip to content

Commit 7aeec86

Browse files
authored
Merge pull request #8 from jonathanv3232/master
adds solution to 1.3
2 parents ddc403f + c08dd8c commit 7aeec86

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//my code was not originally this clean, but same concept as solution
2+
function URLify(arr, len) {
3+
let spaces = 0;
4+
for(let i = 0; i < len; i++) {
5+
if(arr[i] === ' ') spaces++;
6+
}
7+
//last index
8+
let index = len + spaces * 2 - 1;
9+
10+
for(let i = len - 1; i >= 0; i--) {
11+
if(arr[i] === ' ') {
12+
arr[index] = '0';
13+
arr[index - 1] = '2';
14+
arr[index - 2] = '%';
15+
index -= 3
16+
} else {
17+
arr[index] = arr[i];
18+
index--;
19+
}
20+
}
21+
return arr;
22+
}
23+
24+
//testing
25+
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];
26+
//before
27+
console.log(arr);
28+
let ans = URLify(arr, 13);
29+
//after
30+
console.log(ans);

0 commit comments

Comments
 (0)