Skip to content

Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups#1109

Open
Pretty548 wants to merge 26 commits intoCodeYourFuture:mainfrom
Pretty548:sprint-2
Open

Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups#1109
Pretty548 wants to merge 26 commits intoCodeYourFuture:mainfrom
Pretty548:sprint-2

Conversation

@Pretty548
Copy link
Copy Markdown

@Pretty548 Pretty548 commented Mar 26, 2026

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

  • Fixed parseQueryString implementation and handled edge cases
  • Fixed contains function bug
  • Implemented tally function with validation
  • Fixed calculateMedian to handle invalid inputs

@github-actions

This comment has been minimized.

4 similar comments
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@Pretty548 Pretty548 changed the title Sprint 2 Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups Mar 26, 2026
@github-actions

This comment has been minimized.

@Pretty548 Pretty548 marked this pull request as draft March 27, 2026 11:32
@Pretty548 Pretty548 marked this pull request as ready for review March 27, 2026 11:32
@Pretty548 Pretty548 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 27, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 27, 2026
@Pretty548 Pretty548 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 27, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 27, 2026
@Pretty548 Pretty548 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 27, 2026
@@ -1,3 +1,7 @@
function contains() {}
function contains(obj, key) {
if (!obj) return false;
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.

Supposedly, when obj is one of the following types of value, the function should return false.

[1,2,3], 123, true, "string"

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.

Good catch! I updated the function to ensure it only accepts plain objects by checking for non-object types, null, and arrays. It now correctly returns false for values like arrays, numbers, Booleans, and strings.

function contains(obj, key) {
if (!obj) return false;

return Object.prototype.hasOwnProperty.call(obj, key);
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.

Can also consider Object.hasOwn().

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! I updated the implementation to use Object.hasOwn() and ensured the function returns false for non-object inputs instead of throwing an error.

Comment on lines +27 to +30
test("returns false for an array", () => {
expect(contains([], "a")).toBe(false);
});

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.

This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains([], "a") could also return false simply because "a" 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.

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.

Good point! I’ve updated the test to use a non-empty array with a valid index as the key, so it confirms the function returns false specifically because the input is an array.

Comment on lines +7 to +19
pairs.forEach((pair) => {
if (!pair) return;

const index = pair.indexOf("=");

if (index === -1) {
result[pair] = "";
} else {
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
result[key] = value;
}
});
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.

For the following function call, does your function return the value you expect?

parseQueryString("key1=value1&&key2=value2")

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.

Yes, the function correctly handles this case. The double "&&" creates an empty string when splitting, and the check if (!pair) return; ensures it is ignored. The function returns the expected result with both key-value pairs.

Comment on lines +6 to +14
const result = {};

items.forEach((item) => {
if (result[item]) {
result[item]++;
} else {
result[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 returns the value you expect?

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

Suggestion: Look up an approach to create an empty object with no inherited properties.

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.

Good catch! I updated the implementation to use Object.create(null) so the result object has no inherited properties. This ensures keys like "toString" are handled correctly.

Comment on lines +30 to +53
const invert = require("./invert");

test("inverts a simple object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("inverts an object with multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
1: "a",
2: "b",
});
});

test("returns empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});

test("handles non-string values as keys in the inverted object", () => {
expect(invert({ a: 1, b: true, c: null })).toEqual({
1: "a",
true: "b",
null: "c",
});
});
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.

Shouldn't this be implemented in invert.test.js?

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.

Good catch, thanks! You're right — these tests should live in invert.test.js. I'll move them there to keep the test structure consistent and separated from implementation.

@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 Mar 28, 2026
@Pretty548 Pretty548 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
@github-actions

This comment has been minimized.

@github-actions github-actions bot removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
@Pretty548 Pretty548 added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Mar 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants