-
-
Notifications
You must be signed in to change notification settings - Fork 327
Cape Town | 25-ITP-MAY | Asanda Dunn | Sprint 2 | Data Groups #1447
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
59f99a8
886e0b0
48f6834
fd9d025
045e61c
9e4acfd
113302d
994ac21
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 |
|---|---|---|
|
|
@@ -12,4 +12,6 @@ const address = { | |
| postcode: "XYZ 123", | ||
| }; | ||
|
|
||
| // console.log(`${address[2]}`) | ||
| console.log(`My house number is ${address[0]}`); | ||
|
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. This tells me the house number is undefined? Is that right? |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Object.hasOwn(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; | ||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,15 +21,26 @@ as the object doesn't contains a key of 'c' | |
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
|
|
||
| test("contains on empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| test("contains returns true when property exists", () => { | ||
| 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("contains returns false when property does not exist", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains returns false when passed an array", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
| }); | ||
|
Comment on lines
+44
to
+46
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. There should be more test cases within this test suite. Can you think of them and add more? Thank you. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| function createLookup() { | ||
| function createLookup(countryCurrencyPairs) { | ||
| // implementation here | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of countryCurrencyPairs) { | ||
| const [countryCode, currencyCode] = pair; | ||
| lookup[countryCode] = currencyCode; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
|
|
||
| module.exports = createLookup; |
|
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. There should be more test cases such as empty array or array with one pair of country-currency code etc. Please add them to increase the testing coverage. thank you. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,50 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| // If the query string is empty, return an empty object | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Split the query string into separate key-value pairs | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| // Ignore empty pairs | ||
| if (pair === "") { | ||
| continue; | ||
|
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. please use |
||
| } | ||
|
|
||
| // Find the first "=" | ||
| const equalsPosition = pair.indexOf("="); | ||
|
|
||
| let key; | ||
| let value; | ||
|
|
||
| // If there is no "=" | ||
| if (equalsPosition === -1) { | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| // Everything before "=" is the key | ||
| key = pair.substring(0, equalsPosition); | ||
|
|
||
| // Everything after the first "=" is the value | ||
| value = pair.substring(equalsPosition + 1); | ||
| } | ||
|
|
||
| // Replace "+" with spaces and decode special characters | ||
| key = decodeURIComponent(key.replace(/\+/g, " ")); | ||
| value = decodeURIComponent(value.replace(/\+/g, " ")); | ||
|
|
||
| // If the key already exists, store multiple values in an array | ||
| if (queryParams[key] === undefined) { | ||
| queryParams[key] = value; | ||
| } else if (Array.isArray(queryParams[key])) { | ||
| queryParams[key].push(value); | ||
| } else { | ||
| queryParams[key] = [queryParams[key], value]; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| // Check that the input is an array | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| // Go through each item in the array | ||
| for (const item of items) { | ||
| if (counts[item] === undefined) { | ||
| counts[item] = 1; | ||
| } else { | ||
| counts[item] = counts[item] + 1; | ||
| } | ||
| } | ||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,11 +24,23 @@ const tally = require("./tally.js"); | |
| // 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 duplicate items", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ | ||
| a: 2, | ||
| b: 1, | ||
| c: 1, | ||
| }); | ||
| }); | ||
|
|
||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| test("tally throws an error when given a string", () => { | ||
| expect(() => tally("hello")).toThrow(); | ||
| }); | ||
|
Comment on lines
41
to
+46
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. Besides string, there are other invalid inputs which would be good to add to the test suite as well. Thank you. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("inverts an object with one property", () => { | ||
| expect(invert({ a: 1 })).toEqual({ | ||
| 1: "a", | ||
| }); | ||
| }); | ||
|
|
||
| test("inverts an object with multiple properties", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ | ||
| 1: "a", | ||
| 2: "b", | ||
| }); | ||
| }); | ||
|
|
||
| test("inverts the example object", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ | ||
| 10: "x", | ||
| 20: "y", | ||
| }); | ||
| }); |
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.
address is an object but not array. so it cannot use indexing to get an element. please try again. thank you.