London | 26-ITP-May | Alex Jamshidi | Sprint 2 | Exercises#1226
London | 26-ITP-May | Alex Jamshidi | Sprint 2 | Exercises#1226Alex-Jamshidi wants to merge 15 commits into
Conversation
cjyuan
left a comment
There was a problem hiding this comment.
Some of the code is not consistently formatted or indented.
Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode, as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?
| }; | ||
|
|
||
| console.log(`My house number is ${address[0]}`); | ||
| console.log(`My house number is ${address.street}, ${address.city}, ${address.country}, ${address.postcode}`); |
There was a problem hiding this comment.
Your code outputs every parts of an address except the "house number".
There was a problem hiding this comment.
Added house number.
I had installed prettier and enabled the formatting, however it wasn't working because I hadn't set as my default formatter. I have formatted this code now. (and gone back through all my old PRs and formatted too!)
thanks :)
| if (typeof object !== 'object' || Array.isArray(object)) {return false;} | ||
| if (typeof property !== 'string') {return false;} |
There was a problem hiding this comment.
Line 2 cannot yet reject null.
There was a problem hiding this comment.
updated to reject null
written tests to confirm
| it("given invalid parameter (an array) returns false or throws an error", () => { | ||
| const object = []; | ||
| const property = []; | ||
| expect(contains(object, property)).toEqual(false) |
There was a problem hiding this comment.
Your function can correctly return false when the first argument is an array.
However, we write tests not only to verify our current implementation, but also to ensure that future changes do not alter the function's expected behavior.
The current test cannot yet confirm the function can correctly return false when the first argument is an array
because contains([], []) could also return false simply because [] is not a key of the array.
Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid key to ensure the function returns false specifically because the input is an array, not because the key is missing.
There was a problem hiding this comment.
Thanks for this, I understand.
I wrote several tests to cover all options for arrays (filled or empty).
| // implementation here | ||
| function createLookup(arrayOfArrays) { | ||
| const lookup = {} | ||
| for (array of arrayOfArrays) { |
There was a problem hiding this comment.
thanks, have declared with const
| // currently without adding this database, an unknown PEC could cause the code to get stuck in an infinite loop | ||
|
|
||
| function changePEC(string) { | ||
| const PEC = {"%24": "$", "%2F": "/"} |
There was a problem hiding this comment.
There are more than two percentage encoded characters.
Consider using the built-in function to decode PEC's in the string instead.
There was a problem hiding this comment.
I didn't know there was a function for this facepalm
I have updated to use the function, it was a lot easier :)
Thanks
| const [key, value] = [pair.slice(0, index), pair.slice(index + 1)] | ||
| queryParams[key] = value; | ||
| } | ||
| else {}; |
There was a problem hiding this comment.
Why leave a do-nothing else block in the function?
There was a problem hiding this comment.
It's a good question that doesn't have a good answer, I have removed it.
Thanks
| tally = {} | ||
| array.forEach((item) =>{ | ||
| if (item in tally) {tally[item] += 1} | ||
| else tally[item] = 1; | ||
| }) | ||
| return tally |
There was a problem hiding this comment.
Does the following function call returns the value you expect?
tally(["toString", "toString"]);
Suggestion:
- Look up an approach to create an empty object with no inherited properties, or
- use
Object.hasOwn()
There was a problem hiding this comment.
that was a tough one
Wrote test for case and updated code to pass
Learners, PR Template
Self checklist
Changelist
Completed exercises for Sprint 2