Repository navigation
London | 26-ITP-Sep | Chandramani Gaire | Sprint 2 | Module-Data-Group #1515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
816c4d1
21f4801
d598feb
b28c7c1
6f70a1a
6980b95
4da133e
4703c3f
e56354c
9c0640c
afd1d85
87890a4
1ad7ce2
92a706f
d607dae
3fa1c43
77121c2
104e2ff
6be96ff
3f7ac41
b6c0846
f04ed1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,8 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (object === null || typeof object !== "object" || Array.isArray(object)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
| return Object.hasOwn(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| function createLookup() { | ||
| function createLookup(pairs) { | ||
| // implementation here | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of pairs) { | ||
| const countryCode = pair[0]; | ||
| const currencyCode = pair[1]; | ||
|
|
||
| lookup[countryCode] = currencyCode; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| const key = String(item); | ||
|
|
||
| if (counts.hasOwnProperty(key)) { | ||
| counts[key] += 1; | ||
| } else { | ||
| counts[key] = 1; | ||
| } | ||
| } | ||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,21 +14,80 @@ const tally = require("./tally.js"); | |
| * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Given an array of items | ||
| // When passed to tally | ||
| // Then it should return an object containing the count for each unique item | ||
| test("tally on a single item counts it once", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| }); | ||
|
|
||
| test("tally on repeated items counts each one", () => { | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| }); | ||
|
|
||
| test("tally on a mix of items counts each unique item", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
| }); | ||
|
|
||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
| test("tally on an empty array returns an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| test("tally counts duplicates that are not next to each other", () => { | ||
| expect(tally(["a", "b", "a", "c", "b", "a"])).toEqual({ a: 3, b: 2, c: 1 }); | ||
| }); | ||
|
|
||
| // Given an invalid input like a string, a number, or no argument at all | ||
| // When passed to tally | ||
| // Then it should throw Error("tally requires an array") | ||
| test("tally throws on a string", () => { | ||
| expect(() => tally("abc")).toThrow(new Error("tally requires an array")); | ||
| }); | ||
|
|
||
| test("tally throws on a number", () => { | ||
| expect(() => tally(42)).toThrow(new Error("tally requires an array")); | ||
| }); | ||
|
|
||
| test("tally throws when called with no argument", () => { | ||
| expect(() => tally()).toThrow(new Error("tally requires an array")); | ||
| }); | ||
|
|
||
| test("tally throws on null, undefined and plain objects", () => { | ||
| expect(() => tally(null)).toThrow(new Error("tally requires an array")); | ||
| expect(() => tally(undefined)).toThrow(new Error("tally requires an array")); | ||
| expect(() => tally({ a: 1 })).toThrow(new Error("tally requires an array")); | ||
| }); | ||
|
|
||
| // Extra edge cases | ||
| test("tally handles item names that match built-in object properties", () => { | ||
| const result = tally(["constructor", "toString", "constructor"]); | ||
| expect(result.constructor).toBe(2); | ||
| expect(result.toString).toBe(1); | ||
| }); | ||
|
|
||
| test("tally does not allow prototype pollution via __proto__", () => { | ||
| const result = tally(["__proto__", "__proto__"]); | ||
| expect({}.polluted).toBeUndefined(); | ||
| expect(Object.getPrototypeOf(result)).toBe(Object.prototype); | ||
| expect(Object.keys(result)).toEqual([]); | ||
| }); | ||
|
|
||
| test("tally counts numbers and booleans by their string form", () => { | ||
| expect(tally([1, 1, 2, true, true])).toEqual({ 1: 2, 2: 1, true: 2 }); | ||
| }); | ||
|
|
||
| test("tally treats different cases as different items", () => { | ||
| expect(tally(["a", "A", "a"])).toEqual({ a: 2, A: 1 }); | ||
| }); | ||
|
|
||
| test("tally does not change the array it is given", () => { | ||
| const input = ["a", "b", "a"]; | ||
| tally(input); | ||
| expect(input).toEqual(["a", "b", "a"]); | ||
| }); | ||
|
Comment on lines
+81
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Very good coverage for having these 3 test cases.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("swaps keys and values for the example in the exercise", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| test("works with a single key", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("works with two keys", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); | ||
| }); | ||
|
|
||
| test("returns an empty object for an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("string values become keys", () => { | ||
| expect(invert({ first: "one", second: "two" })).toEqual({ | ||
| one: "first", | ||
| two: "second", | ||
| }); | ||
| }); | ||
|
|
||
| test("original numeric keys come back as strings", () => { | ||
| expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" }); | ||
| }); | ||
|
|
||
| test("when two keys share a value, the last key wins", () => { | ||
| expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); | ||
| }); | ||
|
|
||
| test("does not change the original object", () => { | ||
| const original = { a: 1, b: 2 }; | ||
| invert(original); | ||
| expect(original).toEqual({ a: 1, b: 2 }); | ||
| }); | ||
|
|
||
| test("inverting twice gives back the original when values are unique strings", () => { | ||
| const original = { a: "x", b: "y" }; | ||
| expect(invert(invert(original))).toEqual(original); | ||
| }); | ||
|
Comment on lines
+40
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting test case, very good!
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thanks you |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that this case has an implementation test, what will Jest still report from the earlier
test.todo? The same leftover TODO is present inlookup.test.js.To "like" or "dislike" this comment, please follow this link
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i remove the TODO from lookup.test.js
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please also remove the TODO from this file as well. Otherwise, Jest library would still not showing a pass. Thank you.