Skip to content

Commit f110f2c

Browse files
committed
Excercises Sprint 2- querry String / Tally
1 parent d7ce32b commit f110f2c

File tree

5 files changed

+134
-10
lines changed

5 files changed

+134
-10
lines changed

Sprint-2/implement/querystring.js

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1+
// function parseQueryString(queryString) {
2+
// const queryParams = {};
3+
// if (queryString.length === 0) {
4+
// return queryParams;
5+
// }
6+
// const keyValuePairs = queryString.split("&");
7+
8+
// for (const pair of keyValuePairs) {
9+
// const [key, value] = pair.split("=");
10+
// queryParams[key] = value;
11+
// }
12+
13+
// return queryParams;
14+
// }
15+
16+
// module.exports = parseQueryString;
17+
118
function parseQueryString(queryString) {
219
const queryParams = {};
3-
if (queryString.length === 0) {
20+
21+
if (!queryString) {
422
return queryParams;
523
}
24+
625
const keyValuePairs = queryString.split("&");
726

827
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
28+
const index = pair.indexOf("=");
29+
30+
if (index === -1) {
31+
// key with no value
32+
queryParams[pair] = "";
33+
} else {
34+
const key = pair.slice(0, index);
35+
const value = pair.slice(index + 1);
36+
queryParams[key] = value;
37+
}
1138
}
1239

1340
return queryParams;

Sprint-2/implement/querystring.test.js

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,39 @@
33
// Below is one test case for an edge case the implementation doesn't handle well.
44
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

8-
test("parses querystring values containing =", () => {
9-
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
11-
});
8+
// Empty string
9+
test("empty string", () => {
10+
expect(parseQueryString("")).toEqual({});
1211
});
12+
// Single value
13+
test("single key-value", () => {
14+
expect(parseQueryString("name=alice")).toEqual({ name: "alice" });
15+
});
16+
17+
// Multiple pairs
18+
test("multiple pairs", () => {
19+
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
20+
});
21+
22+
// Key with no value
23+
test("key with no value", () => {
24+
expect(parseQueryString("flag")).toEqual({ flag: "" });
25+
});
26+
27+
// Empty value
28+
test("empty value", () => {
29+
expect(parseQueryString("name=")).toEqual({ name: "" });
30+
});
31+
32+
// Multiple '=' in value
33+
test("values with multiple =", () => {
34+
expect(parseQueryString("equation=x=y+1")).toEqual({ equation: "x=y+1" });
35+
});
36+
37+
// Special characters
38+
test("special characters", () => {
39+
expect(parseQueryString("msg=hello%20world")).toEqual({ msg: "hello%20world" });
40+
});
41+

Sprint-2/implement/tally.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1-
function tally() {}
1+
function tally(arr) {
2+
// Handle invalid input
3+
if (!Array.isArray(arr)) {
4+
throw new Error("Input must be an array");
5+
}
6+
7+
// Handle empty array
8+
if (arr.length === 0) {
9+
return {};
10+
}
11+
12+
// Count frequencies
13+
const result = {};
14+
15+
for (const item of arr) {
16+
if (result[item]) {
17+
result[item]++;
18+
} else {
19+
result[item] = 1;
20+
}
21+
}
22+
23+
return result;
24+
}
225

326
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
const tally = require("./tally.js");
22

3+
// Test 1: Empty array
4+
test("tally on an empty array returns an empty object", () => {
5+
expect(tally([])).toEqual({});
6+
});
7+
8+
// Test 2: Single item
9+
test("tally on single item returns count of 1", () => {
10+
expect(tally(["a"])).toEqual({ a: 1 });
11+
});
12+
13+
// Test 3: Duplicate items
14+
test("tally counts duplicate items", () => {
15+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
16+
});
17+
18+
// Test 4: Mixed items
19+
test("tally counts multiple unique items", () => {
20+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
21+
});
22+
23+
// Test 5: Invalid input throws error
24+
test("tally throws error for non-array input", () => {
25+
expect(() => tally("hello")).toThrow();
26+
});
27+
328
/**
429
* tally array
530
*
@@ -23,7 +48,7 @@ const tally = require("./tally.js");
2348
// Given an empty array
2449
// When passed to tally
2550
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
51+
2752

2853
// Given an array with duplicate items
2954
// When passed to tally

package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "module-data-groups",
3+
"version": "1.0.0",
4+
"description": "Like learning a musical instrument, programming requires daily practice.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Della-Bella/Module-Data-Groups-Old.git"
12+
},
13+
"keywords": [],
14+
"author": "",
15+
"license": "ISC",
16+
"bugs": {
17+
"url": "https://github.com/Della-Bella/Module-Data-Groups-Old/issues"
18+
},
19+
"homepage": "https://github.com/Della-Bella/Module-Data-Groups-Old#readme"
20+
}

0 commit comments

Comments
 (0)