Skip to content

Commit 7dd5700

Browse files
committed
Finish implement exercises
1 parent 4c6a9e4 commit 7dd5700

5 files changed

Lines changed: 52 additions & 44 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
function contains(obj, property) {
2-
if (
3-
obj === null ||
4-
typeof obj !== "object" ||
5-
Array.isArray(obj)
6-
) {
2+
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
73
return false;
4+
}
5+
return property in obj;
86
}
9-
}
10-
11-
return property in obj;
127

138
module.exports = contains;

‎Sprint-2/implement/querystring.js‎

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

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
8+
const pairs = queryString.split("&");
9+
10+
for (const pair of pairs) {
11+
if (pair === "") {
12+
continue;
13+
}
14+
15+
const equalIndex = pair.indexOf("=");
16+
17+
let key;
18+
let value;
19+
20+
if (equalIndex === -1) {
21+
key = pair;
22+
value = "";
23+
} else {
24+
key = pair.slice(0, equalIndex);
25+
value = pair.slice(equalIndex + 1);
26+
}
27+
28+
key = key.replace(/\+/g, " ");
29+
value = value.replace(/\+/g, " ");
30+
31+
key = decodeURIComponent(key);
32+
value = decodeURIComponent(value);
33+
34+
if (queryParams[key] === undefined) {
35+
queryParams[key] = value;
36+
} else if (Array.isArray(queryParams[key])) {
37+
queryParams[key].push(value);
38+
} else {
39+
queryParams[key] = [queryParams[key], value];
40+
}
1141
}
1242

1343
return queryParams;

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => {
3636
"full name": "John Doe",
3737
});
3838
});
39-
40-
// Stretch exercise: Handling query strings that contain identical keys
41-
42-
// Delete this test if you are not working on this optional case
43-
test("should store values of a key in an array when the key has 2 or more values", () => {
44-
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
45-
key: ["value1", "value2", "value3"],
46-
foo: "bar",
47-
});
48-
});

‎Sprint-2/implement/tally.js‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
function tally() {}
1+
function tally() {
2+
const counts = {};
3+
4+
for (const item of items) {
5+
if (counts[item] === undefined) {
6+
counts[item] = 1;
7+
} else {
8+
counts[item]++;
9+
}
10+
}
11+
12+
return counts;
13+
}
214

315
module.exports = tally;

‎Sprint-2/interpret/invert.js‎

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,4 @@
1-
// Let's define how invert should work
2-
3-
// Given an object
4-
// When invert is passed this object
5-
// Then it should swap the keys and values in the object
6-
7-
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8-
9-
function invert(obj) {
10-
const invertedObj = {};
11-
12-
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
14-
}
15-
16-
return invertedObj;
17-
}
18-
19-
// a) What is the current return value when invert is called with { a : 1 }
20-
1+
//
212
// b) What is the current return value when invert is called with { a: 1, b: 2 }
223

234
// c) What is the target return value when invert is called with {a : 1, b: 2}

0 commit comments

Comments
 (0)