Repository navigation
London | 26-ITP-Sept | Fung Nin Lee | Sprint 2 | Data Group #1526
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
46f5612
323e17f
851a28d
ed6358c
5bd55e4
1f1e59f
78485fb
6f72c2a
221cf1a
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,4 +1,5 @@ | ||
| // Predict and explain first... | ||
| // Ingredients will return the whole recipe before fix. | ||
|
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. What does JavaScript render for To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
|
|
@@ -12,4 +13,4 @@ const recipe = { | |
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ${recipe.ingredients.join("\n")}`); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function contains() {} | ||
| function contains(testObject, keyword) { | ||
| if ( | ||
| typeof testObject === "object" && | ||
| testObject !== null && | ||
| Array.isArray(testObject) === false | ||
| ) { | ||
| return Object.hasOwn(testObject, keyword); | ||
| } | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| function createLookup(lookUpPairs) { | ||
| // implementation here | ||
| return Object.fromEntries(lookUpPairs); | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,14 +3,38 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (!pair.includes("=") && pair.length > 0) { | ||
| queryParams[pair] = ""; | ||
| continue; | ||
|
Comment on lines
+10
to
+12
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. When a bare key such as To "like" or "dislike" this comment, please follow this link |
||
| } | ||
|
|
||
| const key = decodeURIComponent(pair.slice(0, pair.indexOf("="))).replace( | ||
| "+", | ||
| " " | ||
|
Comment on lines
+16
to
+17
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. What does the second To "like" or "dislike" this comment, please follow this link |
||
| ); | ||
| const value = decodeURIComponent(pair.slice(pair.indexOf("=") + 1)).replace( | ||
| "+", | ||
| " " | ||
|
Comment on lines
+19
to
+21
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. How should an encoded literal plus such as To "like" or "dislike" this comment, please follow this link |
||
| ); | ||
|
|
||
| if (pair.indexOf("=") !== -1) { | ||
| queryParams[key] = value; | ||
| } | ||
|
|
||
| if (key.length === 0 && value.length === 0) { | ||
| continue; | ||
| } | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| console.log(parseQueryString("full+name=John+Doe")); | ||
|
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. What output appears when another module imports this parser? To "like" or "dislike" this comment, please follow this link |
||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,17 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
| const counter = {}; | ||
| for (const item of items) { | ||
| if (Object.hasOwn(counter, item) === false) { | ||
| counter[item] = 1; | ||
| } else { | ||
| counter[item]++; | ||
| } | ||
| } | ||
|
|
||
| return counter; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,12 +23,28 @@ const tally = require("./tally.js"); | |
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
| test("return empty object for []", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| test("return a list of items and count the frequency of each item", () => { | ||
| expect(tally(["a", "a", "a", "b", "b"])).toEqual({ a: 3, b: 2 }); | ||
| }); | ||
|
|
||
| // Given an invalid input like a string, a number, or no argument at all | ||
| // When passed to tally | ||
| // Then it should throw Error("tally requires an array") | ||
| test("throw error for 'It is a string'", () => { | ||
| expect(() => tally().toThrow("tally requires an array")); | ||
| }); | ||
|
|
||
| test("throw error for 10", () => { | ||
| expect(() => tally(10).toThrow("tally requires an array")); | ||
| }); | ||
|
|
||
| test("throw error for ''", () => { | ||
| expect(() => tally().toThrow("tally requires an array")); | ||
|
Comment on lines
+41
to
+49
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. Where is To "like" or "dislike" this comment, please follow this link |
||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| describe("invert", () => { | ||
| test("inverting key and value of object", () => { | ||
| expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" }); | ||
| }); | ||
|
|
||
| test("inverting empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("inverting object with either key or value is empty", () => { | ||
| expect(invert({ "": 1, b: "" })).toEqual({ 1: "", "": "b" }); | ||
| }); | ||
|
|
||
| test("inverting object with duplicate value", () => { | ||
| expect(() => invert({ a: 1, b: 1 })).toThrow( | ||
| 'Duplicate key found after inversion: "1"' | ||
| ); | ||
| }); | ||
| }); |
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.
When the original
address[0]expression runs, does it throw an error or produce a value?To "like" or "dislike" this comment, please follow this link