-
-
Notifications
You must be signed in to change notification settings - Fork 322
London | 26-ITP-May | Rizqah Popoola | Sprint 2 | Data groups #1323
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,10 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, target) { | ||
| for (let index = 0; index < Object.keys(object).length; index++) { | ||
| const element = Object.keys(object)[index]; | ||
| if (element === target) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| module.exports = contains; | ||
|
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. All the tests in this file have the same description. If they belong to the same category, you could group the tests into one cateogry. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,19 +17,44 @@ 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 | ||
|
|
||
| test("returns true if the object contains the property, false otherwise", () => { | ||
| const currentOutput = contains({ a: 1, b: 2, c: 2 }, "c"); | ||
| const targetOutput = true; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("returns true if the object contains the property, false otherwise", () => { | ||
| const currentOutput = contains({}, "d"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| test("returns true if the object contains the property, false otherwise", () => { | ||
| const currentOutput = contains({ a: 1, b: 2, c: 2 }, "b"); | ||
| const targetOutput = true; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("returns true if the object contains the property, false otherwise", () => { | ||
| const currentOutput = contains({ a: 1, b: 2, c: 2 }, "e"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("returns true if the object contains the property, false otherwise", () => { | ||
| const currentOutput = contains(["a", "b", "c", "e"], "e"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
Comment on lines
52
to
+60
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 quite match the spec given on lines 52-54. Please note that in JS, an array is a kind of object with its indices serve as its keys. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(nestedArray) { | ||
| let lookup = {}; | ||
| for (let i = 0; i < nestedArray.length; i++) { | ||
| let key = nestedArray[i][0]; | ||
| let value = nestedArray[i][1]; | ||
| lookup[key] = value; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,12 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| const keyValuePairs = queryString.replaceAll("+", " ").split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (pair === "") continue; | ||
| const [key = "", value = ""] = pair.split(/=(.*)/); | ||
|
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. Interesting approach! |
||
| queryParams[decodeURIComponent(key)] = decodeURIComponent(value); | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(array) { | ||
| let tallyObject = {}; | ||
|
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:
|
||
| if (Array.isArray(array)) { | ||
| for (const item of array) { | ||
| if (tallyObject[item] === undefined) { | ||
| tallyObject[item] = 1; | ||
| } else { | ||
| tallyObject[item]++; | ||
| } | ||
| } | ||
| return tallyObject; | ||
| } else { | ||
| throw new Error("Invalid input"); | ||
| } | ||
|
Comment on lines
+3
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. Could consider structuring the code this way: |
||
| } | ||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("when invert is passed to an object, it should swap the keys and values in the object", () => { | ||
| const currentOutput = invert({ x: 10, y: 20 }); | ||
| const targetOutput = { 10: "x", 20: "y" }; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| test("when invert is passed to an object, it should swap the keys and values in the object", () => { | ||
| const currentOutput = invert({ a: 1, b: 2 }); | ||
| const targetOutput = { 1: "a", 2: "b" }; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); |
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.
Code is correct.
Could also consider taking advantage of built-in Array or Object methods to achieve the same behavior with less code.