Repository navigation
London | 26-ITP-SEP | Russom Gebremeskel | Sprint 2 | Coursework #1531
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
5ce7d15
8f51f4c
d68c02b
f65276c
6745e82
20d911a
6f7f6d4
a922943
2491759
697d23f
33ffd60
b471137
434ca4b
d997797
734381d
215a119
c12fb12
31ba3f1
eb978b1
9a8945a
68b854a
1f53630
d0b0c30
c0cc6b7
5da7996
b772767
1374324
2b9d397
1c95b32
2f02e30
1735942
351f97b
7bb3a8f
15e8f9f
6c1939d
db24d03
7b407a4
b953e7c
585cfe1
63eae52
f025f5a
0764465
d638679
cfdd9cc
6308c6c
a1e5834
b1d7fb5
1330dc8
28f60e5
2974d2c
036874d
b6ae9d4
ff4a00c
68bc523
7a43172
d5ddc5f
101c8dd
697cfff
9e4a322
dde2ce1
37b6945
97bc04a
7d1731d
78ba804
5d3a0c9
c3a87fb
bbd2717
571ce4e
ca45fa0
f8dd126
1a0d169
f8d29b6
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 |
|---|---|---|
|
|
@@ -11,6 +11,9 @@ const author = { | |
| alive: true, | ||
| }; | ||
|
|
||
| for (const value of author) { | ||
| for (const value of Object.values(author)) { | ||
| console.log(value); | ||
| } | ||
|
|
||
| // Predict: it will return undefined. | ||
|
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 happens when you run this loop now, and does that match the prediction that it returns To "like" or "dislike" this comment, please follow this link |
||
| // Explain: objects values are accessed by including Object.values(author) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,5 +11,18 @@ const recipe = { | |
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ingredients: | ||
| ${recipe.ingredients.join("\n")}`); | ||
|
|
||
| // prediction: the above code prints: | ||
| // bruschetta | ||
| // serves 2 | ||
| // ingredients: | ||
|
|
||
| //On a new line it prints: | ||
| // title: "bruschetta", | ||
| // serves: 2, | ||
| // ingredients: "olive oil", "tomatoes", "salt", "pepper" | ||
|
|
||
| // Explain: on line 15 recipe is missing .ingredients. | ||
| // As a result it will print the whole property of recipe object. | ||
|
Comment on lines
+21
to
+28
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. These comments describe the pre-fix template, but the current template prints each ingredient on its own line and no longer prints the recipe object. The prediction and explanation should match the code now in the file. To "like" or "dislike" this comment, please follow this link |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function contains() {} | ||
| function contains(obj, property) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
| for (const key of Object.keys(obj)) { | ||
| if (key === property) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,18 +23,31 @@ as an array isn't an object | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("contains with existing property returns true", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("contains with a non-existent property returns false", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given a value that isn't an object - an array, a string, a number, | ||
| // null, or no argument at all | ||
| // When passed to contains | ||
| // Then it should throw Error("contains requires an object") | ||
| // (careful: typeof [] and typeof null are both "object") | ||
| test("contains on non-object returns contains requires an object", () => { | ||
| expect(() => contains(([1, 2, 3], "a"))).toThrow( | ||
|
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 single value does the comma expression inside To "like" or "dislike" this comment, please follow this link |
||
| "contains requires an object" | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| function createLookup(countryCurrencyPairs) { | ||
| // implementation here | ||
| const currencyLookup = {}; | ||
| for (const [country, currency] of countryCurrencyPairs) { | ||
| currencyLookup[country] = currency; | ||
| } | ||
| return currencyLookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,12 +4,19 @@ function parseQueryString(queryString) { | |
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (!pair) { | ||
| continue; | ||
| } | ||
| const everything = pair.split("="); | ||
| const key = decodeURIComponent(everything.shift().replace(/\+/g, " ")); | ||
| const value = decodeURIComponent(everything.join("=").replace(/\+/g, " ")); | ||
| if (queryParams[key]) { | ||
|
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 happens to the second To "like" or "dislike" this comment, please follow this link |
||
| queryParams[key] = [].concat(queryParams[key], value); | ||
| } else { | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(list) { | ||
| if (!Array.isArray(list)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
|
|
||
| const itemFrequencyCount = {}; | ||
|
|
||
| for (const item of list) { | ||
| if (Object.hasOwn(itemFrequencyCount, item)) { | ||
| itemFrequencyCount[item] = itemFrequencyCount[item] + 1; | ||
| } else { | ||
| itemFrequencyCount[item] = 1; | ||
| } | ||
| } | ||
| return itemFrequencyCount; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,23 +7,34 @@ | |
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| if (typeof obj !== "object") { | ||
|
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 happens when To "like" or "dislike" this comment, please follow this link |
||
| throw new Error("invert requires a valid input"); | ||
| } | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| module.exports = invert; | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // - undefined. | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // - undefined. | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // - "{1" : "a", ""2": "b"} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // - It turns object properties into an array of key-value pairs | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // - line 13 is accessing the property 'key' name directly | ||
| // - the target output is to swap properties inside the variable 'key' and 'value' | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| // Empty object | ||
| test("invert on an empty object returns an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| // Basic objects | ||
| test("invert on basic object returns inverted properties object ", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| // Single property | ||
| test("invert on a single property object returns inverted key-value object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| // Multiple properties | ||
| test("invert on multiple properties object returns inverted properties object", () => { | ||
| expect(invert({ 1: "a", 2: "b", 3: "c", 4: "d" })).toEqual({ | ||
| a: "1", | ||
| b: "2", | ||
| c: "3", | ||
| d: "4", | ||
| }); | ||
| }); | ||
|
|
||
| // String values | ||
| test("invert on string value object returns inverted key-value object", () => { | ||
| expect(invert({ a: "hello" })).toEqual({ hello: "a" }); | ||
| }); | ||
|
|
||
| // Duplicate values | ||
| test("invert on duplicate values object return inverted object", () => { | ||
| expect(invert({ a: 1, b: 1 })).toEqual({ 1: "a", 1: "b" }); | ||
| }); | ||
|
|
||
| // Invalid input | ||
| test("invert on invalid input returns invert requires a valid input", () => { | ||
| expect(() => invert()).toThrow("invert requires a valid input"); | ||
| }); |
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.
What does
address[0]produce in the original code, and what does this corrected lookup produce?To "like" or "dislike" this comment, please follow this link