-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Russom Gebremeskel | Sprint 2 | Coursework #1351
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
c9145df
1cf01db
256ef50
84b5c1e
51f62a2
ae0b900
702b5ba
583bb2c
c0f9787
552c68c
9058a37
ce06e18
e6267ac
14c848c
c66861d
c4dec19
d5c28aa
b10058d
7fc4886
946461b
37e70cb
1b4063a
af59161
0877d2d
4c0bba5
5518e5e
ed6927a
d438535
3163a9c
0be80fe
473160f
ed53e3c
e711ff1
bc5da26
88c48a1
94caca5
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,10 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| for (const key in object) { | ||
| if (object[key] === property) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
Comment on lines
+1
to
+8
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. Could this function pass all the tests in |
||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,44 @@ as the object doesn't contains a key of 'c' | |
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
| test("contains on an object with property returns true", function () { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
| test("contains on an object without property returns false", function () { | ||
| expect(contains({ a: 1, b: 2 }, "d")).toBe(false); | ||
| }); | ||
|
|
||
| // 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", function () { | ||
| 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("contain on object with properties", function () { | ||
| expect(contains({ c: 4, d: 5 }, "c")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("contains on object with non-existent property name returns false", function () { | ||
| expect(contains({ c: 4, d: 5 }, "x")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contain on invalid parameters returns false", function () { | ||
| expect(contains([], "0")).toBe(false); | ||
| }); | ||
|
Comment on lines
47
to
+52
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. Could you check if |
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name "0" | ||
| // Then it should return false | ||
| test("contain on with non-existent property returns false", function () { | ||
| expect(contains(["a", "b", "c"], "0")).toBe(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| let obj = {}; | ||
| for (let pair of pairs) { | ||
| obj[pair[0]] = pair[1] | ||
| } | ||
| return obj; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("Invalid input"); | ||
| } | ||
| const count = {}; | ||
|
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. Does the following function call returns the value you expect? Suggestion:
|
||
| for (const item of arr) { | ||
| if (Object.hasOwn(count, item)) { | ||
| count[item] = count[item] + 1; | ||
| } else { | ||
| count[item] = 1; | ||
| } | ||
| } | ||
| return count; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("invert on an object with key-value returns key-value swapped", function () { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); | ||
| expect(invert({ distance1: 105, distance2: 201 })) | ||
| .toEqual({ 105: "distance1", 201: "distance2" }); | ||
| expect(invert({ fruit: "apple", veg: "carrot" })) | ||
| .toEqual({ "apple": "fruit", "carrot": "veg" }); | ||
| }); |
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.
The loop variable
valuetakes on the key (or property) of the object. Naming it 'value' could confuse people.