-
-
Notifications
You must be signed in to change notification settings - Fork 284
London | 26-ITP-May | Alex Jamshidi | Sprint 2 | Exercises #1226
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
bd846e8
dc61f91
5761063
3ff26b1
59b7763
40a2e86
5567875
f928482
3b91514
352aef9
66a4554
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,8 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (typeof object !== 'object' || Array.isArray(object)) {return false;} | ||
| if (typeof property !== 'string') {return false;} | ||
|
Comment on lines
+2
to
+3
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. Line 2 cannot yet reject |
||
|
|
||
| return Object.hasOwn(object, property) | ||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,24 +12,44 @@ 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 | ||
|
|
||
| describe("contains", () => { | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| it("contains on empty object returns false", () => { | ||
| const object = {}; | ||
| const property = 'a' | ||
| expect(contains(object, property)).toEqual(false) | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| it("object contains property returns true", () => { | ||
| const object = {a: 1, b: 2}; | ||
| const property = 'a' | ||
| expect(contains(object, property)).toEqual(true) | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| it("object does not contain property returns false", () => { | ||
| const object = {a: 1, b: 2}; | ||
| const property = 'c' | ||
| expect(contains(object, property)).toEqual(false) | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| it("given invalid parameter (an array) returns false or throws an error", () => { | ||
| const object = []; | ||
| const property = []; | ||
| expect(contains(object, property)).toEqual(false) | ||
|
Comment on lines
+50
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. Your function can correctly return However, we write tests not only to verify our current implementation, but also to ensure that future changes do not alter the function's expected behavior. The current test cannot yet confirm the function can correctly return Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid key to ensure the function returns |
||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arrayOfArrays) { | ||
| const lookup = {} | ||
| for (array of arrayOfArrays) { | ||
|
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.
|
||
| lookup[array[0]] = array[1]; | ||
| } | ||
| return lookup | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,40 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Adds percentage-encoded characters | ||
| queryString = changePEC(queryString) | ||
|
|
||
| // Replaces + with space | ||
| queryString = queryString.replaceAll("+", " ") | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (pair !== "") { | ||
| let index = pair.indexOf("=") | ||
| if (!pair.includes("=")) {index = pair.length} | ||
| const [key, value] = [pair.slice(0, index), pair.slice(index + 1)] | ||
| queryParams[key] = value; | ||
| } | ||
| else {}; | ||
|
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. Why leave a do-nothing else block in the function? |
||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| // this function below can have the object PEC expanded to include all percentage-encoded characters | ||
| // currently without adding this database, an unknown PEC could cause the code to get stuck in an infinite loop | ||
|
|
||
| function changePEC(string) { | ||
| const PEC = {"%24": "$", "%2F": "/"} | ||
|
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. There are more than two percentage encoded characters. Consider using the built-in function to decode PEC's in the string instead. |
||
| while (string.includes("%")) { | ||
| const index = string.indexOf("%"); | ||
| const code = string.slice(index, index + 3); | ||
| if (PEC[code]) {string = string.replace(code, PEC[code]);} | ||
| else {break;} | ||
| } | ||
| return string | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) {throw new Error("Invalid array");} | ||
| tally = {} | ||
| array.forEach((item) =>{ | ||
| if (item in tally) {tally[item] += 1} | ||
| else tally[item] = 1; | ||
| }) | ||
| return tally | ||
|
Comment on lines
+3
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. Does the following function call returns the value you expect? Suggestion:
|
||
| } | ||
|
|
||
| module.exports = tally; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| describe("invert", () => { | ||
| it("object returns inverted object", () => { | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| let object = { a : 1 }; | ||
| expect(invert(object)).toEqual({1: "a"}) | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| object = { a: 1, b: 2 }; | ||
| expect(invert(object)).toEqual({1: "a", 2: "b"}) | ||
| }); | ||
| }); |
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.
Your code outputs every parts of an address except the "house number".