Repository navigation
London | 26-ITP-May | Vito Moratti | Sprint 2 | Coursework #1364
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 16 commits
d4df6a4
9256b7f
522cb56
5dcae6a
c68ec3e
bc5e89a
2114a9e
78041d6
6d8eefd
fbce304
b10547d
2dbe395
91bfc23
aa5ee68
4349ed1
aff50e0
9ca3f78
34f16f3
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,6 @@ | ||
| function contains() {} | ||
|
|
||
| module.exports = contains; | ||
| function contains(obj, prop) { | ||
| if (Array.isArray(obj)) { | ||
| return false; // returns false if an object contains an array | ||
| } | ||
| return obj.hasOwnProperty(prop); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,31 @@ as the object doesn't contains a key of 'c' | |
| // 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"); | ||
| 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 an object returns true, or false otherwise", () => { | ||
| expect(contains({a: 1, b: 2, c: 3}, "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 an object returns true, or false otherwise", () => { | ||
| expect(contains({a: 1, b: 2, c: 3}, "d")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains an object returns true, or false otherwise", () => { | ||
| expect(contains([1, 2, 3, 'a'], "0")).toBe(false); | ||
| }); | ||
|
Comment on lines
42
to
47
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. Invalid parameters also include values like
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. I have added extra validation to the contains() function now. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(entries) { | ||
| let obj = Object.fromEntries(entries); | ||
| return obj; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(arr) { | ||
| let countOfItemsObj = {}; | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("Invalid input"); | ||
| } else if (arr.length < 1) { | ||
| return countOfItemsObj; | ||
| } else { | ||
| for (let index = 0; index < arr.length; index++) { | ||
| const exist = Object.hasOwn(countOfItemsObj, arr [index]); | ||
| if (!exist) { | ||
| countOfItemsObj[arr[index]] = 1; | ||
| } else { | ||
| countOfItemsObj[arr[index]] = countOfItemsObj[arr[index]] + 1; | ||
| } | ||
| } | ||
| } | ||
| return countOfItemsObj; | ||
| } | ||
| module.exports = tally; |
Uh oh!
There was an error while loading. Please reload this page.