Skip to content

Commit 8f4d096

Browse files
committed
updated function implementations.
1 parent 79fe12d commit 8f4d096

File tree

3 files changed

+45
-59
lines changed

3 files changed

+45
-59
lines changed

Sprint-2/implement/querystring.test.js

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,26 @@
77

88
const parseQueryString = require("./querystring.js")
99

10-
test("parses querystring with single value", () => {
11-
expect(parseQueryString("a=1")).toEqual({"a": "1",
10+
test("parses querystring values containing =", () => {
11+
expect(parseQueryString("equation=x=y+1")).toEqual({
12+
"equation": "x=y+1",
1213
});
1314
});
14-
test("parses querystring with multiple values", () => {
15-
expect(parseQueryString("a=1&b=2")).toEqual({
16-
"a": "1",
17-
"b": "2",
18-
});
15+
test("parses multiple key-value pairs", () => {
16+
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
1917
});
20-
21-
//other edge cases
22-
test("handles empty query string", () => {
18+
test("parses empty string as empty object", () => {
2319
expect(parseQueryString("")).toEqual({});
2420
});
25-
test("handles query string with no key-value pairs", () => {
26-
expect(parseQueryString("&&")).toEqual({});
27-
});
28-
test("handles query string with only keys", () => {
29-
expect(parseQueryString("key1&key2")).toEqual({
30-
key1: undefined,
31-
key2: undefined
32-
});
21+
test("parses values with spaces encoded as +", () => {
22+
expect(parseQueryString("name=John+Doe")).toEqual({ name: "John Doe" });
3323
});
3424

35-
test("handles missing value", () => {
36-
expect(parseQueryString("foo=")).toEqual({ foo: "" });
25+
test("parses URL encoded special characters", () => {
26+
expect(parseQueryString("text=Hello%2C%20World%21")).toEqual({ text: "Hello, World!" });
3727
});
3828

39-
test("handles missing key", () => {
40-
expect(parseQueryString("=bar")).toEqual({ "": "bar" });
41-
});
42-
test("handles multiple key-value pairs", () => {
43-
expect(parseQueryString("key1=value1&key2=value2")).toEqual({
44-
key1: "value1",
45-
key2: "value2"
46-
});
47-
});
48-
test("handles multiple key-value pairs with same key", () => {
49-
expect(parseQueryString("key=value1&key=value2")).toEqual({
50-
key: "value2" // last value should overwrite previous
51-
});
52-
});
29+
test("parses values containing equals sign for any key", () => {
30+
expect(parseQueryString("formula=a=b+c")).toEqual({ formula: "a=b+c" });
31+
expect(parseQueryString("expression=x=5&result=y=10")).toEqual({ expression: "x=5", result: "y=10" });
32+
});

Sprint-2/implement/tally.js

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
function tally(arr) {
2-
if (!Array.isArray(arr)) {
1+
function tally(countFrequency) {
2+
if (!Array.isArray(countFrequency)) {
33
throw new Error("Input must be an array");
44
}
5-
return arr.reduce((acc, item) => {
6-
acc[item] = (acc[item] || 0) + 1;
7-
return acc;
8-
}, Object.create(null));
95

6+
if (countFrequency.length === 0) {
7+
return {};
8+
}
9+
10+
const result = {};
11+
for (item of countFrequency) {
12+
if (result[item]) {
13+
result[item]++;
14+
} else {
15+
result[item] = 1;
16+
}
17+
}
18+
return result;
1019
}
20+
console.log(tally(["a", "a", "a"]));
1121

12-
module.exports = tally;
22+
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,32 @@ const tally = require("./tally.js");
1919
// Given a function called tally
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
22-
test("tally on an array of items returns an object with counts", () => {
23-
const input = ['a', 'a', 'b', 'c'];
24-
const expectedOutput = { a: 2, b: 1, c: 1 };
25-
26-
expect(tally(input)).toEqual(expectedOutput);
22+
test("counts item correctly", () => {
23+
const input = ["a", "b", "c"];
24+
const output = { a: 1, b: 1, c: 1 };
25+
expect(tally(input)).toEqual(output);
2726
});
28-
2927
// Given an empty array
3028
// When passed to tally
3129
// Then it should return an empty object
3230
test("tally on an empty array returns an empty object", () => {
33-
const input = [];
34-
const expectedOutput = {};
35-
36-
expect(tally(input)).toEqual(expectedOutput);
31+
expect(tally([])).toEqual({});
3732
});
3833

3934
// Given an array with duplicate items
4035
// When passed to tally
4136
// Then it should return counts for each unique item
42-
test("tally on an array with duplicates returns correct counts", () => {
43-
const input = ['a', 'b', 'a', 'c', 'b', 'b'];
44-
const expectedOutput = { a: 2, b: 3, c: 1 };
45-
46-
expect(tally(input)).toEqual(expectedOutput);
37+
test("Given an array with duplicate items", () => {
38+
const input = ["a", "a", "b", "c"];
39+
const output = { a: 2, b: 1, c: 1 };
40+
expect(tally(input)).toEqual(output);
4741
});
4842

4943
// Given an invalid input like a string
5044
// When passed to tally
5145
// Then it should throw an error
52-
test("tally throws an error for non-array input", () => {
53-
expect(() => tally("not an array")).toThrow("Input must be an array");
54-
});
46+
test("Given an invalid input like a string, when passed to tally, then it should throw an error", () => {
47+
expect(() => {
48+
tally("invalid string input");
49+
}).toThrow("Input must be an array");
50+
});

0 commit comments

Comments
 (0)