Skip to content

Commit 9251977

Browse files
For the final section for PR submission
1 parent 4d7b72c commit 9251977

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

Sprint-2/implement/querystring.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
4+
if (!queryString || queryString.length === 0) {
45
return queryParams;
56
}
6-
const keyValuePairs = queryString.split("&");
77

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
8+
const pairs = queryString.split("&");
9+
10+
for (const pair of pairs) {
11+
if (!pair) continue;
12+
13+
const [key, ...rest] = pair.split("=");
14+
const value = rest.join("=");
15+
16+
if (!key) continue;
17+
1018
queryParams[key] = value;
1119
}
1220

Sprint-2/interpret/invert.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,33 @@ function invert(obj) {
1010
const invertedObj = {};
1111

1212
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
13+
invertedObj[value] = key;
1414
}
1515

1616
return invertedObj;
1717
}
18+
console.log(invert({ a: 1 }));
19+
console.log(invert({ a: 1, b: 2 }));
20+
console.log(invert({ a: 1, b: 2 }));
1821

1922
// a) What is the current return value when invert is called with { a : 1 }
23+
// the current output value of this invert is {key:1}.
2024

2125
// b) What is the current return value when invert is called with { a: 1, b: 2 }
26+
/* the current output value of this invert is {key:2} because
27+
instead of accessing the [key] variable inside the condition parameter for store key for each loop, the code in line 13 instead adding .key behind and the system
28+
interpret as string literal so each loop is overwrite the previous key value.
29+
.*/
2230

2331
// c) What is the target return value when invert is called with {a : 1, b: 2}
32+
// is also the same with { a: 1, b: 2 } because is overwrite the previous key each loop.
2433

2534
// c) What does Object.entries return? Why is it needed in this program?
35+
/* Object.entries return deconstruct object key and value pair into individual item in an array.
36+
This is very important because since object is are not iterable, this help by deconstructing object into array when we try to loop each key and value pair*/
2637

2738
// d) Explain why the current return value is different from the target output
28-
39+
/*
40+
Because inside the for of loop, the invertedObj output assign they keys first instead of value for each loop.
41+
*/
2942
// e) Fix the implementation of invert (and write tests to prove it's fixed!)

0 commit comments

Comments
 (0)