-
-
Notifications
You must be signed in to change notification settings - Fork 193
London | ITP-SEP-25 | Samuel Tarawally | Sprint 2 | Coursework/sprint 2 #894
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
0e5bd64
420e298
13ef0b9
5bd937e
ad67bd7
b5500ee
8470b22
a1d45f7
160eb23
a32e08b
22585d3
3812df8
fb98fcb
1862d49
bd8a3dc
071d46c
7881920
f3adf96
fbc7f3f
69034a9
b9495db
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() {} | ||
| // Checks if an object has a specific property. | ||
| // References: | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray | ||
|
|
||
| function contains(obj, propertyName) { | ||
| // Returns false for invalid inputs (null, arrays, or non-objects) | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| return obj.hasOwnProperty(propertyName); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,20 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| // Converts an array of pairs into a lookup object. | ||
| // References: | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors#bracket_notation | ||
|
|
||
| function createLookup(arrayOfPairs) { | ||
| const lookupObject = {}; | ||
|
|
||
| for (const pair of arrayOfPairs) { | ||
| const key = pair[0]; | ||
| const value = pair[1]; | ||
|
Comment on lines
+9
to
+11
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. Could also consider using array destructuring to simplify this 3 lines of code. |
||
|
|
||
| // Bracket notation allows using a variable as the property name | ||
| lookupObject[key] = value; | ||
| } | ||
|
|
||
| return lookupObject; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,26 @@ | ||
| // Parses a query string into an object. | ||
| // References: | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice | ||
|
|
||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| // Returns early if the string is empty | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| // Finds only the first "=" since values may contain "=" too | ||
| const firstEqualsIndex = pair.indexOf("="); | ||
|
|
||
| const key = pair.slice(0, firstEqualsIndex); | ||
| const value = pair.slice(firstEqualsIndex + 1); | ||
|
Comment on lines
+19
to
+22
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. What if the query string is |
||
|
|
||
| queryParams[key] = value; | ||
|
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 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,30 @@ | ||
| function tally() {} | ||
| // Counts how often each item appears in an array. | ||
| // References: | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray | ||
| // - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of | ||
|
|
||
| function tally(arrayOfItems) { | ||
| // Checks the input is actually an array | ||
| const inputIsNotAnArray = !Array.isArray(arrayOfItems); | ||
|
|
||
| if (inputIsNotAnArray) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const countObject = {}; | ||
|
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 the following function call returns the value you expect? Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| for (const item of arrayOfItems) { | ||
| // If the item is already counted, add 1; otherwise start at 1 | ||
| const itemAlreadyCounted = countObject[item] !== undefined; | ||
|
|
||
| if (itemAlreadyCounted) { | ||
| countObject[item] = countObject[item] + 1; | ||
| } else { | ||
| countObject[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return countObject; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
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.
Arrays are objects in JavaScript, and they do have property names -- just not the same ones as objects.
Which keys do arrays have, and how does that affect how reliable your test is?
When testing whether the function handles arrays properly, try using a key that an array might
realistically contain. Otherwise, you might get a passing test even if the function isn't checking for arrays at all.