-
-
Notifications
You must be signed in to change notification settings - Fork 280
Sheffield | 26-Jan-ITP | Martha Ogunbiyi| Sprint 2| Coursework/sprint 2 #1133
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
36332cf
a706aa1
57572df
c84f128
7beca08
52babe2
96d9df2
3a2ddf7
3a6e6f9
42a2aae
fb46c74
f0e1410
08d64f9
4a6e24e
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,13 @@ | ||
| function contains() {} | ||
| function contains(obj, targetKey) { | ||
| if ( | ||
| obj === null || | ||
| typeof obj !== "object" || | ||
| Array.isArray(obj) || | ||
| Object.getPrototypeOf(obj) !== Object.prototype | ||
| ) { | ||
| throw new Error("Invalid Parameter"); | ||
| } | ||
| return Object.hasOwn(obj, targetKey); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,43 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () => { | ||
| const input = {}; | ||
| const target = 'a' | ||
| const result = contains(input, target); | ||
| expect(result).toBe(false); | ||
|
|
||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("Given an object with properties,it should return true", () => { | ||
| const input = { a: 1, b: 2 }; | ||
| const target = 'a' | ||
| const result = contains(input, target) | ||
| expect(result).toBe(true); | ||
| }); | ||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("given an object with a non-existent property name, it should return false", () => { | ||
| const input = { a: 1, b: 2 }; | ||
| const target = 'c' | ||
| const result = contains(input, target); | ||
| expect(result).toBe(false); | ||
| }); | ||
|
|
||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("given an invalid parameters, it should return false or throw an error", () => { | ||
| const input = ['a', 'b', 'c']; | ||
| const target = 0; | ||
| expect(() => contains(input, target)).toThrow(); | ||
|
|
||
| }); | ||
|
Comment on lines
53
to
+61
Contributor
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 test does not yet confirm that the function correctly returns false when the first argument is an array. Arrays are objects, with their indices acting as keys. A proper test should use an array along with a valid key to ensure the function returns
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 for the feedback. Test case now updated to correctly fail, given an invalid parameter |
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(codeCurrencyArrays) { | ||
| const result = {}; | ||
|
|
||
| for (const [code, currency] of codeCurrencyArrays) { | ||
| result[code] = currency; | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,20 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (pair === "") continue; | ||
| const index = pair.indexOf("="); | ||
|
|
||
| if (index === -1) { | ||
| queryParams[pair] = ""; | ||
| continue; | ||
| } | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
| queryParams[key] = value; | ||
| } | ||
|
Comment on lines
8
to
19
Contributor
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. For the following function call, does your function return the value you expect?
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 for the feedback, function updated to skip empty entries |
||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| function tally() {} | ||
| function tally(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("Invalid Parameter"); | ||
| } | ||
|
|
||
| const uniqueItems = Object.create(null); | ||
|
|
||
| for (const item of list) { | ||
| uniqueItems[item] = (uniqueItems[item] || 0) + 1; | ||
| } | ||
|
|
||
| return uniqueItems; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| //Given a function invert() | ||
| //When passed an object | ||
| //Then it should return a swapped key and value in the object | ||
| // example: Given => {a: 1, b: 2, c: 3}, should return {1: a, 2: b, 3: c} | ||
|
|
||
| test("Given an object with at least one key value pair, it should return an inverted key value", () => { | ||
| const input = { a: 1, b: 2, c: 3 }; | ||
| const output = invert(input); | ||
| const target = { "1": "a", "2": "b", "3": "c" }; | ||
| expect(output).toEqual(target); | ||
| }) |
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.
Does this code output the property values such as "Zadie", "Smith", ...?
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.
Thanks for the feedback, i have update to output the values rather the keys