-
-
Notifications
You must be signed in to change notification settings - Fork 289
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 15 commits
bd846e8
dc61f91
5761063
3ff26b1
59b7763
40a2e86
5567875
f928482
3b91514
352aef9
66a4554
6cb0fe2
193230c
8427bfd
c698639
0acc6af
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,12 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (typeof object !== "object" || !object || Array.isArray(object)) { | ||
| return false; | ||
| } | ||
| if (typeof property !== "string" || !object) { | ||
| return false; | ||
| } | ||
|
|
||
| return Object.hasOwn(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arrayOfArrays) { | ||
| const lookup = {}; | ||
| for (const array of arrayOfArrays) { | ||
| lookup[array[0]] = array[1]; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,24 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Adds percentage-encoded characters | ||
| queryString = decodeURIComponent(queryString); | ||
|
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. If you decode the characters so early and some the decoded characters are '=', '+', or '&', the query will be incorrectly parsed.
Author
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. updated this to resolve decoded chars at the end |
||
|
|
||
| // Replaces + with space | ||
| queryString = queryString.replaceAll("+", " "); | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,14 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Invalid array"); | ||
| } | ||
| const count = Object.create(null); | ||
| array.forEach((item) => { | ||
| if (item in count) { | ||
| 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,13 @@ | ||
| 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.
"a"and["a"]are not keys of["a", "1"], but"0"and"1"are. You should use either "0" or "1" as key to ensure the function returns false because the first parameter is an array.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.
updated and rerun tests to pass