Skip to content

Comments

London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953

Open
mohsenzamanist wants to merge 4 commits intoCodeYourFuture:mainfrom
mohsenzamanist:feature/sprint-2
Open

London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953
mohsenzamanist wants to merge 4 commits intoCodeYourFuture:mainfrom
mohsenzamanist:feature/sprint-2

Conversation

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

Complete Sprint 2 exercises.

Questions

@mohsenzamanist mohsenzamanist added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 10, 2026
Comment on lines 45 to 50
expect(() => contains(undefined, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
expect(() => contains(null, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Number is not a plain JS object but it is given a different treatment.

Wouldn't it be "friendlier to the caller" if the function can be designed to behave consistently for any value that is not an object or is an array?

Comment on lines 30 to 38
function countWords(string) {
const noPunctuationStr = string.replace(/[.,!?]/g, "");
const wordArray = noPunctuationStr.split(" ");
let wordCount = new Map();
for (let word of wordArray) {
wordCount.set(word, (wordCount.get(word) || 0) + 1);
}
const sortedWordCount = [...wordCount.entries()].sort((a, b) => b[1] - a[1]);
return sortedWordCount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you check if your function returns what you expect in the following function calls?

countWords("Hello,World! Hello World!");
countWords("          Hello    World      ");

@cjyuan cjyuan 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 Feb 20, 2026
@mohsenzamanist mohsenzamanist 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 Feb 22, 2026
@@ -1,3 +1,9 @@
function contains() {}
function contains(obj, property) {
if (Object.prototype.toString.call(obj) !== "[object Object]") return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach to check if obj is an object is not 100% accurate. For example,

  const map = new Map()
  console.log(map instanceof Object);              // true
  console.log(Object.prototype.toString.call(map)) // [object Map]

map is an object but your approach would not recognise it as an object.

This test description in contains.test.js is a bit vague because an array is a kind of object in JavaScript:

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error

Can you interpret "invalid parameter" as "it is not an object OR it is an array"?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the code only need to check the plain objects (excluding Map)?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know the intent of the person who wrote the test description. My interpretation is, accept all objects except arrays.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach you were using is not a reliable way to check for pure Object. Without a more specific description of what is expected of such object, let's just practice on "accept any object that is not an array"

Comment on lines 39 to 40
const sortedWordCount = new Map([...wordCount].sort(([, a], [, b]) => b - a));
return sortedWordCount;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The syntax of the expected output on line 13 seems to suggest the return value should be a plain JS object instead of a Map object.

  • On line 39, it would make the statement more readable if a and b are replaced by more meaningful identifiers.

@cjyuan cjyuan 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 Feb 23, 2026
@mohsenzamanist mohsenzamanist 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 Feb 23, 2026
Copy link
Contributor

@cjyuan cjyuan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you remove the debugging code to keep the code clean?

.split(/[.,!? ]+/)
.filter(Boolean);
let wordCount = new Map();
const wordCount = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: You could just change the code on line 39 and keep wordCount as a Map object.

if (Object.prototype.toString.call(obj) !== "[object Object]") return false;

if (Array.isArray(obj) || obj === null || typeof obj !== "object") {
console.log("here");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debugging code should be removed to make the code cleaner.

@cjyuan cjyuan 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 Feb 23, 2026
@mohsenzamanist mohsenzamanist 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 Feb 23, 2026
@cjyuan cjyuan 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 Feb 24, 2026
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants