-
-
Notifications
You must be signed in to change notification settings - Fork 220
NW | 25-ITP-Sep | TzeMing Ho | Sprint 2 |sprint-2-exercises #797
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
Conversation
CameronDowner
left a comment
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.
This is looking really strong - I've asked some questions to test your understanding 🙂
Let me know if you have any questions
|
|
||
| // I guess the bug could be related to how to access property in an object. | ||
| // address[0] may not be able to access houseNumber as a key | ||
| // if we change address[0] to address['houseNumber'] or address.houseNumber, it may resolve the problem. |
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 would it not be able to access via address[0]?
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.
address[0] can't access houseNumber because address is an object. When address[0] is read, it is looking for a key named 0 inside the address, which leads to "undefined" since the key 0 doesn't exist.
| for (const value of author) { | ||
| console.log(value); | ||
| } | ||
| console.log(Object.values(author)); |
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 if we wanted to console.log each property at a time?
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.
If we wanted to print each property line by line, we could get the properties in an array by Object.keys(author), map each property, and print them out one by one.
Object.keys(author).map((key) => console.log(key));
Sprint-2/debug/recipe.js
Outdated
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
| ${recipe.ingredients.map((ingredient) => `${ingredient}`).join("\n")}`); |
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.
If we extract this value, is there a way we couple simplify this?
`${recipe.ingredients.map((ingredient) => `${ingredient}`).join("\n")}`;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.
Yeah. You are right. We can simplify this line to ${recipe.ingredients.join("\n")}, which can also print each ingredient on each line.
| @@ -1,5 +1,16 @@ | |||
| function createLookup() { | |||
| // implementation here | |||
| function createLookup(arr) { | |||
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.
This is looking really good
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.
Yeah~
| describe("testing invert function", () => { | ||
| const errorMessage = "Input should be an object"; | ||
| it("should throw error message when input is not an object", () => { | ||
| expect(() => invert("abc")).toThrow(errorMessage); |
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 do we put this invert call inside an arrow function?
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.
We put this invert function inside an arrow function because we expect it will fail with invalid data input. If the invert function is called as usual, like invert("abc"), the function will be fired up immediately. We don't have time to catch the error and compare it with an error message.
So, the () => arrow function is doing a referencing job, call the invert function when we expect it to fail.
| "[1]": "a", | ||
| "[2]": "b", | ||
| }); | ||
| expect(invert({ a: { b: 1 }, c: { d: 2 } })).toStrictEqual({ |
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's the difference between toStrictEqual and toEqual used earlier in Sprint-2/implement/querystring.test.js?
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.
toStrictEqual is stricter than toEqual. It will check the types of data, as we are testing undefined as a value, we should use toStrictEqual. Otherwise, the test will ignore undefined like toEqual does.
| for (element of strArray) { | ||
| result[element] = (result[element] || 0) + 1; | ||
| } | ||
| return Object.fromEntries(Object.entries(result).sort((a, b) => b[1] - a[1])); |
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's the benefit of .sort here?
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.
The sort function can rearrange the words' order based on the frequency they appear. We use a desc sort to move the words that appear more to the top of the list.
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.
Objects appear to keep the order we set - and in most cases they will - but technically JavaScript doesn’t guarantee that order (especially for number keys).
So while it looks sorted when you log it, you shouldn’t always rely on that
For this exercise though, it works perfectly 🙂
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.
So, if we prefer order matters, we should choose an array for testing?
Sprint-2/stretch/mode.js
Outdated
|
|
||
| function calculateMode(list) { | ||
| // track frequency of each value | ||
| const frequencyArray = trackFrequency(list); |
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.
Is this an array if the trackFrequency function returns a Map?
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.
Actually not. I didn't know it before. A map is actually an object map, but it works like an array because it has a built-in iterator to loop through the map.
If we wanted to convert the frequency map into an array, we could [...frequency.entries()] or Array.from(frequency.entries())
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.
You're right - in this case Map behaves like an object but has an iterator - so it has an order to the elements
|
@TzeMingHo Changes are looking good 🙂 Could you also answer the questions I've asked in the comments? Not problem if you don't know all the answers, but would be good to see what you think |
Sorry to get back to you late. I forgot to submit my replies. So you couldn't see them yet. |
No problem 🙂 Easily done - all looking really good! |
Learners, PR Template
Self checklist
Changelist
I have tried to create tests for the functions, according to the TPP principles, followed by refactoring the functions to make them work.