-
-
Notifications
You must be signed in to change notification settings - Fork 191
Birmingham | ITP-Sep-25 | Ahmad Ehsas | sprint 2 | Data groups #805
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 19 commits
96e54cd
d577680
3ecbe9f
0467d03
a9a5914
d5ca4ab
ce7733e
73e8544
f512318
22c4094
1231d9c
aa42568
ef820ad
898abfc
2bc209b
d0788b8
9940e55
ae98aa1
268774e
9c2f2f8
1f7a23d
fa7c649
e165bfe
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,16 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| return obj.hasOwnProperty(prop); | ||
| } | ||
|
|
||
| // Or we can use `for ...in loop to check for the property | ||
| // function contains(obj, prop) { | ||
| // for (const key in obj){ | ||
| // if (key === prop) return true; | ||
| // } | ||
| // return false; | ||
| // } | ||
|
|
||
| console.log(contains({ a: 1, b: 2 }, "a")); // true | ||
| console.log(contains({ a: 1, b: 2 }, "c")); // false | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,35 @@ as the object doesn't contains a key of 'c' | |
| // 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 on object 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 on object with non-existent property returns false", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains on an array returns false", () => { | ||
| expect(contains([1, 2, 3], 4)).toBe(false); | ||
| }); | ||
|
|
||
| test("contains on an array of length returns true", () => { | ||
| expect(contains([1, 2, 3, 4], "length")).toBe(true); | ||
| }); // the test returns true because 'length' is a property of the array object | ||
|
|
||
| test("contains given on array as input return false", () => { | ||
| expect(contains([1, 2, 3], "a")).toBe(false); | ||
| }); | ||
|
Comment on lines
44
to
56
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. Do these function calls return what you expect?
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. My function checks for its own property on an object. For the first input `contains([1, 2, 3], "0"); return true because arrays are objects and "0" is a property key.
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. In that case you may want to update the test descriptions on lines 44 and 52. |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,16 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(codePairs) { | ||
| const lookup = codePairs.reduce((acc, [key, value]) => { | ||
| acc[key] = value; | ||
| return acc; | ||
| }, {}); | ||
| return lookup; | ||
| } | ||
|
|
||
| console.log( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]) | ||
| ); | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const [key, value] = pair.split(/=(.+)/); // We use regex to split only at first. | ||
| queryParams[key] = value; | ||
| } | ||
|
Comment on lines
7
to
13
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. Does this function call return what you expect? In real query string, both
Can your function handle URL-encoded query string? Suggestion: Look up "How to decode a URL-encoded string in JavaScript". |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,19 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Input must be an array"); | ||
| // check if the input ia an array. if not, throw an error. | ||
| } | ||
| const tally = {}; | ||
|
||
|
|
||
| for (const item of array) { | ||
| tally[item] = (tally[item] || 0) + 1; | ||
| // If the item already exists in tally, increase its count by 1. | ||
| // If it doesn't exist, start from 1. | ||
| } | ||
|
|
||
| return tally; | ||
| } | ||
|
|
||
| console.log(tally(["a", "a", "a"])); | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,20 +10,29 @@ function invert(obj) { | |
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
| console.log(invert({ a: 1 })); // {1: "a"} | ||
| console.log(invert({ a: 1, b: 2 })); // {1: "a", 2: "b"} | ||
| module.exports = invert; | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // The current return value is {1: "a"} | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // The current return value is {1: "a", 2: "b"} | ||
|
||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // The target return value is {1: "a", 2: "b"} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| // Object.entries takes an object and return an array of key-value pairs. | ||
| // it needed because it converts the object into an array of [key, value] pairs which makes it easy to loop over both key and value at the same time. | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| // because it returns the [key, value] of an object in reverse order [value,key] | ||
|
|
||
| // 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,14 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("inverts an object with unique keys", () => { | ||
| expect(invert({ a: 1, b: 2 })).toStrictEqual({ 1: "a", 2: "b" }); | ||
| }); | ||
|
|
||
| test("inverts an object with string keys", () => { | ||
| // prettier-ignore | ||
| expect(invert({ cat: "meow", dog: "bark" })).toStrictEqual({ meow: "cat", bark: "dog"}); | ||
| }); | ||
|
|
||
| test("inverts an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); |
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.
Why introduce the if-statement on line 21? Can you justify its need?
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 I tested my code without the 'if ... statement', it worked, but the if ... statement is neccessary if there might be properties inherited from the prototype chain.
So the 'if' check doesn't change the output.
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.
keywithin the loop to see what values you get