-
-
Notifications
You must be signed in to change notification settings - Fork 323
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
edbc812
497a1fa
a56b817
bd5b52e
e544c0f
b75d15b
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,7 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, target) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| return false; | ||
| } | ||
| return Object.hasOwn(object, target); | ||
| } | ||
| module.exports = contains; |
| 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("an empty object should return false when passed to contain", () => { | ||
| 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", () => { | ||
| 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 false as the object doesn't contains the property", () => { | ||
| 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("invalid parameters like an array, when passed to contains should return false", () => { | ||
| const currentOutput = contains(["a", "b", "c", "e"], "1"); | ||
| 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,15 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(array) { | ||
| const tallyObject = Object.create(null); | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Invalid input"); | ||
| } | ||
| for (const item of array) { | ||
| if (tallyObject[item] === undefined) { | ||
| tallyObject[item] = 1; | ||
| } else { | ||
| tallyObject[item]++; | ||
| } | ||
| } | ||
| return tallyObject; | ||
| } | ||
| 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); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.