Skip to content

Conversation

@TzeMingHo
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

I have tried to create tests for the functions, according to the TPP principles, followed by refactoring the functions to make them work.

@TzeMingHo TzeMingHo added 📅 Sprint 2 Assigned during Sprint 2 of this module Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 19, 2025
Copy link

@CameronDowner CameronDowner left a 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.

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]?

Copy link
Author

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));

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?

Copy link
Author

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));

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.map((ingredient) => `${ingredient}`).join("\n")}`);

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")}`;

Copy link
Author

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) {

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

Copy link
Author

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);

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?

Copy link
Author

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({

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?

Copy link
Author

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]));

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?

Copy link
Author

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.

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 🙂

Copy link
Author

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?


function calculateMode(list) {
// track frequency of each value
const frequencyArray = trackFrequency(list);
Copy link

@CameronDowner CameronDowner Oct 21, 2025

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?

Copy link
Author

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())

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

@CameronDowner CameronDowner added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 21, 2025
@TzeMingHo TzeMingHo added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 22, 2025
@CameronDowner
Copy link

@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

@CameronDowner CameronDowner added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 22, 2025
@TzeMingHo
Copy link
Author

@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.

@TzeMingHo TzeMingHo added Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. and removed Reviewed Volunteer to add when completing a review with trainee action still to take. labels Oct 22, 2025
@CameronDowner
Copy link

@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!

@CameronDowner CameronDowner added Complete Volunteer to add when work is complete and all review comments have been addressed. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Complete Volunteer to add when work is complete and all review comments have been addressed. 📅 Sprint 2 Assigned during Sprint 2 of this module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants