Repository navigation
London | 26-ITP-Sep | Chandramani Gaire | Sprint 2 | Module-Data-Group #1515
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
816c4d1
21f4801
d598feb
b28c7c1
6f70a1a
6980b95
4da133e
4703c3f
e56354c
9c0640c
afd1d85
87890a4
1ad7ce2
92a706f
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 (object === null || typeof object !== "object" || Array.isArray(object)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
| return Object.hasOwn(object, property); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,15 @@ | ||
| function createLookup() { | ||
| function createLookup(pairs) { | ||
| // implementation here | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of pairs) { | ||
| const countryCode = pair[0]; | ||
| const currencyCode = pair[1]; | ||
|
|
||
| lookup[countryCode] = currencyCode; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,32 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (!pair) { | ||
| continue; | ||
| } | ||
|
|
||
| const [key, ...rest] = pair.split("="); | ||
| const value = rest.join("="); | ||
|
|
||
| const decodedKey = decodeURIComponent(key.replace(/\+/g, " ")); | ||
| const decodedValue = decodeURIComponent(value.replace(/\+/g, " ")); | ||
|
|
||
| if (decodedKey in queryParams) { | ||
| if (Array.isArray(queryParams[decodedKey])) { | ||
| queryParams[decodedKey].push(decodedValue); | ||
| } else { | ||
| queryParams[decodedKey] = [queryParams[decodedKey], decodedValue]; | ||
|
Comment on lines
+21
to
+25
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 a query string contains a key such as To "like" or "dislike" this comment, please follow this link |
||
| } | ||
| } else { | ||
| queryParams[decodedKey] = decodedValue; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("tally requires an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| const key = String(item); | ||
|
|
||
| if (Object.prototype.hasOwnProperty.call(counts, key)) { | ||
| counts[key] += 1; | ||
| } else { | ||
| Object.defineProperty(counts, key, { | ||
| value: 1, | ||
| writable: true, | ||
| enumerable: true, | ||
| configurable: true, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,20 +10,26 @@ function invert(obj) { | |
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| // it's the error | ||
| // invertedObj.key = value; | ||
| // fixed the code | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
| module.exports = invert; | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
|
|
||
| // ==>The return value when invert is called is "a":"1" | ||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
|
||
| // ==>The return value when invert is called are "a":"1", "b":"2" | ||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
|
||
| // ==>The target return value when invert is called with{a:1, b: 2} are "1": "a", "2": "b" | ||
| // c) What does Object.entries return? Why is it needed in this program? | ||
|
|
||
| // ==>It turns an object into an array of [key, value] pairs. Object.entries({ a: 1, b: 2 }) gives [["a", 1], ["b", 2]]. It’s needed because a for...of loop can’t go through a plain object directly, and the function needs both the key and the value of each property. Object.entries provides both, and const [key, value] unpacks each pair. | ||
| // d) Explain why the current return value is different from the target output | ||
|
|
||
| // ==>first it used the dot notation instead of square bracket which is accessing the property using a literal name | ||
|
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 issue with To "like" or "dislike" this comment, please follow this link |
||
| // ==>second it is not inverted it still uses key value instead of value key so that it returns the inverted one. | ||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // ==>i have fixed the code and for testing i made the another file and make the edges cases. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("swaps keys and values for the example in the exercise", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); | ||
|
|
||
| test("works with a single key", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("works with two keys", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); | ||
| }); | ||
|
|
||
| test("returns an empty object for an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("string values become keys", () => { | ||
| expect(invert({ first: "one", second: "two" })).toEqual({ | ||
| one: "first", | ||
| two: "second", | ||
| }); | ||
| }); | ||
|
|
||
| test("original numeric keys come back as strings", () => { | ||
| expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" }); | ||
| }); | ||
|
|
||
| test("when two keys share a value, the last key wins", () => { | ||
| expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); | ||
| }); | ||
|
|
||
| test("does not change the original object", () => { | ||
| const original = { a: 1, b: 2 }; | ||
| invert(original); | ||
| expect(original).toEqual({ a: 1, b: 2 }); | ||
| }); | ||
|
|
||
| test("inverting twice gives back the original when values are unique strings", () => { | ||
| const original = { a: "x", b: "y" }; | ||
| expect(invert(invert(original))).toEqual(original); | ||
| }); |
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.
Now that this case has an implementation test, what will Jest still report from the earlier
test.todo? The same leftover TODO is present inlookup.test.js.To "like" or "dislike" this comment, please follow this link