-
-
Notifications
You must be signed in to change notification settings - Fork 270
Sheffield | 26-Jan-ITP | Daniel Aderibigbe | Sprint 2 | Coursework/sprint 2 #1053
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
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,5 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| return Object.prototype.hasOwnProperty.call(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,53 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // 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 checks if an object contains a particular property", () => { | ||
| const obj = { a: 1, b: 2 }; | ||
| expect(contains(obj, "a")).toBe(true); | ||
| expect(contains(obj, "c")).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", () => { | ||
| 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 with existing property returns true", () => { | ||
| const obj = { a: 1, b: 2 }; | ||
| expect(contains(obj, "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 with non-existent property returns false", () => { | ||
| const obj = { a: 1, b: 2 }; | ||
| expect(contains(obj, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains with invalid parameters returns false", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
| }); | ||
|
Comment on lines
48
to
+53
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.
You could also add a few more samples to ensure the function can properly handle arguments of different types. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrency) { | ||
| const lookup = {}; | ||
| for (const [country, currency] of countryCurrency) { | ||
| lookup[country] = currency; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,22 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const indexOfEquals = pair.indexOf("="); | ||
|
|
||
| if (indexOfEquals === -1) { | ||
| queryParams[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, indexOfEquals); | ||
| const value = pair.slice(indexOfEquals + 1); | ||
| queryParams[key] = value; | ||
| } | ||
|
Comment on lines
+11
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 each of the following function calls, does your function return the value you expect? Note: (You don't have to implement this)
|
||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Invalid input"); | ||
| } | ||
|
|
||
| const result = {}; | ||
|
|
||
| for (const item of items) { | ||
| if (result[item]) { | ||
| result[item]++; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| } | ||
|
Comment on lines
+6
to
+14
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: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("inverts a single key-value pair", () => { | ||
| expect(invert({ a: 1 })).toEqual({ | ||
| 1: "a", | ||
| }); | ||
| }); | ||
|
|
||
| test("inverts multiple key-value pairs", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ | ||
| 1: "a", | ||
| 2: "b", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns empty object when given empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,24 @@ | |
|
|
||
| 3. Order the results to find out which word is the most common in the input | ||
| */ | ||
| function countWords(str) { | ||
| const result = {}; | ||
|
|
||
| if (str.length === 0) { | ||
| return result; | ||
| } | ||
|
|
||
| const words = str.split(" "); | ||
|
|
||
| for (const word of words) { | ||
| if (result[word]) { | ||
| result[word]++; | ||
| } else { | ||
| result[word] = 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
Comment on lines
+29
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. Can you check if your function returns what you expect in the following function calls? Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check. |
||
|
|
||
| module.exports = countWords; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| const countWords = require("./count-words.js"); | ||
|
|
||
| test("counts words correctly", () => { | ||
| expect(countWords("you and me and you")).toEqual({ | ||
| you: 2, | ||
| and: 2, | ||
| me: 1, | ||
| }); | ||
| }); | ||
|
|
||
| test("returns empty object for empty string", () => { | ||
| expect(countWords("")).toEqual({}); | ||
| }); |
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 function is expected to throw an error or return
falsewhen object is not an object or is an array.Could also explore
Object.hasOwn().