Repository navigation
Cape Town | 26-ITP-Sept | Leigh Ross | Sprint 2 | Coursework sprint 2 #1503
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
6ec28c5
8c3b517
28469a6
5117a51
76e3bb4
0b1de93
87d5708
a349638
c43a62e
80fc275
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,11 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(input, key) { | ||
| if (input === null || typeof input !== "object") { | ||
| throw new Error("contains requires an object") | ||
| } else {for (const keys in input){ | ||
| if (keys === key) { | ||
| return true | ||
| } | ||
| }} return false | ||
| } | ||
| console.log(contains({a: 1}, 'a')) | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(input) { | ||
| return Object.fromEntries(input) | ||
|
|
||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,27 @@ function parseQueryString(queryString) { | |
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| let keyValuePair = keyValuePairs; | ||
|
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. The name |
||
| if (queryString.includes("+")) { | ||
| keyValuePair = keyValuePairs.map((keyValuePair) => | ||
| keyValuePair.replaceAll("+", " ") | ||
| ); | ||
| } else if(queryString.includes("%")) { | ||
| keyValuePair = keyValuePairs.map((keyValuePair) => | ||
| decodeURIComponent(keyValuePair)) | ||
| }; | ||
|
Comment on lines
+8
to
+15
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 (const pair of keyValuePair) { | ||
| if (pair === "") continue; | ||
| const eqIndex = pair.indexOf("="); | ||
| if (eqIndex === -1) { | ||
| queryParams[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, eqIndex); | ||
| const value = pair.slice(eqIndex + 1); | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,16 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)){ | ||
| throw new Error('tally requires an array') | ||
| } | ||
| const result = {}; | ||
| for (const item of array){ | ||
| if (Object.hasOwn(result, item)) { | ||
| result[item] = result[item] + 1 | ||
| } else { | ||
| result[item] = 1 | ||
| } | ||
| } | ||
| return result | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("object with 1 key-value pair. returns object with key-value pairs swopped.", () => { | ||
| expect(invert({a: 1})).toEqual({'1': 'a'}) | ||
| }) | ||
|
|
||
| test("object with 2 key-value pairs. returns object with key-value pairs swopped.", () => { | ||
| expect(invert({a: 1, b: 2})).toEqual({'1': 'a', '2': 'b'}) | ||
| }) | ||
|
|
||
| test("object with 3 key-value pairs. returns object with key-value pairs swopped.", () => { | ||
| expect(invert({a: 1, b: 2, z: 12})).toEqual({'1': 'a', '2': 'b', '12': 'z'}) | ||
| }) | ||
|
|
||
| test("object with 0 key-value pair. returns empty object.", () => { | ||
| expect(invert({})).toEqual({}) | ||
| }) | ||
|
|
||
| test("object with invalid input. throw error: 'invert requires an object.", () => { | ||
| expect(() => invert("bones")).toThrow('invert requires an object.') | ||
| }) |
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.
Could you also test arrays and other non-object values?