Skip to content

London | 26-ITP-Jan | Carlos Abreu |Sprint 2 | Data Module Groups - #1139

Closed
carlosyabreu wants to merge 5 commits into
CodeYourFuture:mainfrom
carlosyabreu:coursework/sprint-2
Closed

London | 26-ITP-Jan | Carlos Abreu |Sprint 2 | Data Module Groups#1139
carlosyabreu wants to merge 5 commits into
CodeYourFuture:mainfrom
carlosyabreu:coursework/sprint-2

Conversation

@carlosyabreu

Copy link
Copy Markdown

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

PR for Sprint 2 Data Group Module

@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
Comment on lines +9 to +22
const counts = {};

// Iterate through each item in the array
for (let i = 0; i < items.length; i++) {
const item = items[i];

// If the item already exists in counts, increment it
// Otherwise, initialize it to 1
if (counts[item]) {
counts[item]++;
} else {
counts[item] = 1;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the following function call return the value you expect?

tally(["toString", "toString"]);

Suggestion:

  • Look up an approach to create an empty object with no inherited properties.
  • Use Object.hasOwn().
  • Use Map object.

@carlosyabreu carlosyabreu Apr 6, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks for your wise input.
It made me to rethink completely the implementation.
Going to the point.

Looking at this code, the function call tally(["toString", "toString"]) will not return the expected result of
{ "toString": 2 }. The issue is with the object property access and JavaScript's prototype chain. When the code checks if (counts[item]), it's not just checking if the object has that own property, it's checking if the value at that key is truthy.
However, for the string "toString", when counts["toString"] is accessed, JavaScript looks up the prototype chain and finds Object.prototype.toString (a function). Since functions are truthy, the condition if (counts[item]) evaluates to true even though counts doesn't have its own "toString" property.

This causes the code to try to increment counts["toString"]++, which is problematic because:

  1. It's not possible to increment a function
  2. This would set counts["toString"] to NaN

Using the modern approach of Object.hasOwn() implementation:

function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be array");
}
const counts = Object.create(null); // Empty object with no prototype

for (let i = 0; i < items.length; i++) {
const item = items[i];

if (Object.hasOwn(counts, item)) {
  counts[item]++;
} else {
  counts[item] = 1;
}

}

return counts;
}

Object.hasOwn() properly checks for own properties rather than looking up the prototype chain.

@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 Apr 2, 2026
@carlosyabreu carlosyabreu added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Apr 6, 2026
@cjyuan

cjyuan commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Changes look good. Well done.

@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. Reviewed Volunteer to add when completing a review with trainee action still to take. labels Apr 6, 2026
@illicitonion

Copy link
Copy Markdown
Member

Closing PR because the January ITP run has finished. Feel free to re-open if you're still working on it.

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.

3 participants