Skip to content

Commit b793f4b

Browse files
committed
the implement part is done
1 parent 9feaf69 commit b793f4b

File tree

8 files changed

+116
-6
lines changed

8 files changed

+116
-6
lines changed

Sprint-2/implement/contains.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
function contains() {}
1+
function contains() {function contains(obj, key) {
2+
// First, check if obj is a real object
3+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
4+
return false;
5+
}
6+
7+
// Use hasOwnProperty to check if the key exists
8+
return obj.hasOwnProperty(key);
9+
}
10+
11+
module.exports = contains;
12+
}
213

314
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,33 @@ as the object doesn't contains a key of 'c'
2020
// Given an empty object
2121
// When passed to contains
2222
// Then it should return false
23-
test.todo("contains on empty object returns false");
2423

24+
test("contains on empty object returns false", () => {
25+
expect(contains({}, "a")).toBe(false);
26+
});
2527
// Given an object with properties
2628
// When passed to contains with an existing property name
2729
// Then it should return true
30+
test("contains returns true for existing property", () => {
31+
const obj = { a: 1, b: 2 };
32+
expect(contains(obj, "a")).toBe(true);
33+
});
34+
2835

2936
// Given an object with properties
3037
// When passed to contains with a non-existent property name
3138
// Then it should return false
39+
test("contains returns false for non-existent property", () => {
40+
const obj = { a: 1, b: 2 };
41+
expect(contains(obj, "c")).toBe(false);
42+
});
43+
3244

3345
// Given invalid parameters like an array
3446
// When passed to contains
3547
// Then it should return false or throw an error
48+
test("contains returns false for invalid input", () => {
49+
expect(contains([], "a")).toBe(false);
50+
expect(contains(null, "a")).toBe(false);
51+
expect(contains("string", "a")).toBe(false);
52+
});

Sprint-2/implement/lookup.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
function createLookup() {
22
// implementation here
3+
function createLookup(pairs) {
4+
const lookup = {};
5+
6+
for (let i = 0; i < pairs.length; i++) {
7+
const pair = pairs[i];
8+
const country = pair[0];
9+
const currency = pair[1];
10+
lookup[country] = currency;
11+
}
12+
13+
return lookup;
14+
}
15+
16+
module.exports = createLookup;
17+
318
}
419

520
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ It should return:
3333
'CA': 'CAD'
3434
}
3535
*/
36+
37+
const createLookup = require("./lookup.js");
38+
39+
test("creates a country currency code lookup for multiple codes", () => {
40+
const countryCurrencyPairs = [['US', 'USD'], ['CA', 'CAD'], ['GB', 'GBP']];
41+
const result = createLookup(countryCurrencyPairs);
42+
43+
expect(result).toEqual({
44+
'US': 'USD',
45+
'CA': 'CAD',
46+
'GB': 'GBP'
47+
});
48+
});

Sprint-2/implement/querystring.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
if (!queryString) {
44
return queryParams;
55
}
6+
67
const keyValuePairs = queryString.split("&");
78

89
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10+
const index = pair.indexOf("="); // find the first =
11+
if (index === -1) continue; // skip if no =
12+
13+
const key = pair.substring(0, index); // before the =
14+
const value = pair.substring(index + 1); // after the =
1015
queryParams[key] = value;
1116
}
1217

Sprint-2/implement/querystring.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,24 @@ test("parses querystring values containing =", () => {
1010
"equation": "x=y+1",
1111
});
1212
});
13+
test("parses querystring values containing =", () => {
14+
expect(parseQueryString("equation=x=y+1")).toEqual({
15+
"equation": "x=y+1",
16+
});
17+
});
18+
19+
test("parses empty query string", () => {
20+
expect(parseQueryString("")).toEqual({});
21+
});
22+
23+
test("parses multiple key-value pairs", () => {
24+
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
25+
});
26+
27+
test("parses key with empty value", () => {
28+
expect(parseQueryString("foo=")).toEqual({ foo: "" });
29+
});
30+
31+
test("parses empty key with value", () => {
32+
expect(parseQueryString("=bar")).toEqual({ "": "bar" });
33+
});

Sprint-2/implement/tally.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1-
function tally() {}
1+
function tally(arr) {
2+
if (!Array.isArray(arr)) {
3+
throw new Error("Input must be an array");
4+
}
5+
6+
const counts = {};
7+
8+
for (let i = 0; i < arr.length; i++) {
9+
const item = arr[i];
10+
11+
12+
if (counts[item]) {
13+
counts[item] += 1;
14+
} else {
15+
counts[item] = 1;
16+
}
17+
}
18+
19+
return counts;
20+
}
221

322
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,21 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test("tally on an empty array returns an empty object", () => {
27+
expect(tally([])).toEqual({});
28+
});
2729

2830
// Given an array with duplicate items
2931
// When passed to tally
3032
// Then it should return counts for each unique item
33+
test("tally on an empty array returns an empty object", () => {
34+
expect(tally([])).toEqual({});
35+
});
36+
3137

3238
// Given an invalid input like a string
3339
// When passed to tally
3440
// Then it should throw an error
41+
test("tally throws error for invalid input", () => {
42+
expect(() => tally("not an array")).toThrow("Input must be an array");
43+
});

0 commit comments

Comments
 (0)