Repository navigation
London | 26-ITP-Sept | Fung Nin Lee | Sprint 2 | Data Group #1526
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
46f5612
323e17f
851a28d
ed6358c
5bd55e4
1f1e59f
78485fb
6f72c2a
221cf1a
6a23595
9e536ce
c1c0f60
8d4e97a
5a524db
3eae916
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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. While your fix produces a correct result, you have also changed the program logic completely. The original algorithm is using a for-of loop and your version is using built-in function. Can you think of a better fix which at least keeping the for loop structure? |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| // Predict and explain first... | ||
| // Ingredients will return the whole recipe before fix. | ||
|
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. What does JavaScript render for To "like" or "dislike" this comment, please follow this link
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. SyntaxError: missing ) after argument list |
||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
|
|
@@ -12,4 +13,4 @@ const recipe = { | |
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ${recipe.ingredients.join("\n")}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function contains() {} | ||
| function contains(testObject, keyword) { | ||
| if ( | ||
| typeof testObject === "object" && | ||
| testObject !== null && | ||
| Array.isArray(testObject) === false | ||
| ) { | ||
| return Object.hasOwn(testObject, keyword); | ||
| } | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,21 +20,55 @@ as an array isn't an object | |
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| describe("contains", () => { | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
|
|
||
| test("return for {}", () => { | ||
| expect(contains({})).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("return for {a: 1, b: 2}, 'a'", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("return for {a: 1, b: 2}, 'c'", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
|
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. All 4 test cases below are correct. How about the test case for "no argument"? Please add it as well. Thank you. |
||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| test("return for ['a', 'b', 'c']", () => { | ||
| expect(() => contains(["a", "b", "c"])).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); | ||
|
|
||
| test("return for null", () => { | ||
| expect(() => contains(null)).toThrow("contains requires an object"); | ||
| }); | ||
|
|
||
| test("return for 'It is a string.'", () => { | ||
| expect(() => contains("It is a string.")).toThrow( | ||
| "contains requires an object" | ||
| ); | ||
| }); | ||
|
|
||
| test("return for 10", () => { | ||
| expect(() => contains(10)).toThrow("contains requires an object"); | ||
| }); | ||
| }); | ||
|
|
||
| test("return for no argument", () => { | ||
| expect(() => contains()).toThrow("contains requires an object"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| function createLookup(lookUpPairs) { | ||
| // implementation here | ||
| return Object.fromEntries(lookUpPairs); | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,17 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| const counter = {}; | ||
| for (const item of items) { | ||
| if (Object.hasOwn(counter, item) === false) { | ||
| counter[item] = 1; | ||
| } else { | ||
| counter[item]++; | ||
| } | ||
| } | ||
|
|
||
| return counter; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,28 @@ const tally = require("./tally.js"); | |
| // 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("return empty object for []", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| test("return a list of items and count the frequency of each item", () => { | ||
| expect(tally(["a", "a", "a", "b", "b"])).toEqual({ a: 3, b: 2 }); | ||
| }); | ||
|
|
||
| // 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("throw error for 'It is a string'", () => { | ||
| expect(() => tally('It is a string')).toThrow("tally requires an array"); | ||
| }); | ||
|
Comment on lines
+40
to
+42
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. Hmm... please check this test case carefully. If you need a hint, please feel free to let me know. Thank you. |
||
|
|
||
| test("throw error for 10", () => { | ||
| expect(() => tally(10)).toThrow("tally requires an array"); | ||
| }); | ||
|
|
||
| test("throw error for ''", () => { | ||
| expect(() => tally()).toThrow("tally requires an array"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| describe("invert", () => { | ||
| test("inverting key and value of object", () => { | ||
| expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" }); | ||
| }); | ||
|
|
||
| test("inverting empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("inverting object with either key or value is empty", () => { | ||
| expect(invert({ "": 1, b: "" })).toEqual({ 1: "", "": "b" }); | ||
| }); | ||
|
Comment on lines
+12
to
+14
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! |
||
|
|
||
| test("inverting object with duplicate value", () => { | ||
| expect(() => invert({ a: 1, b: 1 })).toThrow( | ||
| 'Duplicate key found after inversion: "1"' | ||
| ); | ||
| }); | ||
| }); | ||
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.
When the original
address[0]expression runs, does it throw an error or produce a value?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 don't understand what you need. object elements supposed to be accessed by key instead of index.