Skip to content

Conversation

@AdnaanA
Copy link

@AdnaanA AdnaanA commented Oct 30, 2025

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

Completed Sprint-2 exercises covering JavaScript challenges, error fixing code interpretation and running test-cases with Jest.

@AdnaanA AdnaanA 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 30, 2025
Comment on lines 3 to 7
* Checks if the given array contains the specified value.
* @param {Array} arr - The array to check.
* @param {*} value - The value to search for.
* @returns {boolean} - Returns true if the value is found, otherwise false.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this comment describe?

Copy link
Author

Choose a reason for hiding this comment

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

The comments from line 3 -6 describe a function that looks inside an array and tells you whether a certain value is present.

Copy link
Contributor

Choose a reason for hiding this comment

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

What function would that be? Your function does not quite match what it describe:

  • Its first parameter is not named arr.
  • Its first parameter is not an array
  • Its second parameter is not named value.

Copy link
Author

Choose a reason for hiding this comment

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

you are absolutely right it was one of those rookie mistake from my side. I have re-written the function to match the JSDoc

Copy link
Contributor

Choose a reason for hiding this comment

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

  • Is the arguments passed to contains an array?

  • Object contains keys and values. Naming the 2nd parameter 'value' could be misleading.

Copy link
Contributor

Choose a reason for hiding this comment

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

The description in contains.test.js says,

/*
Implement a function called contains that checks an object contains a
particular property
E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'
E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

Copy link
Author

Choose a reason for hiding this comment

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

No, we cannot assume the arguments passed to contains() are an array.

and i have also changed the parameter 'value' --> 'item'

Copy link
Author

Choose a reason for hiding this comment

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

I have now redone the whole contains.js assignment.

// When passed to contains
// Then it should return false or throw an error
test("contains on invalid parameters returns false", () => {
expect(contains([], "a")).toBe(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 test cannot detect (for certainty) if a function can properly check if the first parameter is an array or not.
An implementation of contains() could also return false because the property "a" cannot be found in the array.

Can you address this shortcoming?

Copy link
Author

Choose a reason for hiding this comment

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

updated the the test we now only return false for invalid types.

Copy link
Contributor

Choose a reason for hiding this comment

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

My point was, contains([], "a") will evaluate to false regardless of whether the function checks if its first parameter is an array or not. If you want to test if the function can reject array, you should write something like

contains(["A"], "0")

because "0" is a key of the array. If contains() does not reject array, then it would have returned true.

Copy link
Author

Choose a reason for hiding this comment

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

After fixing the contains.js I re-wrote the test-cases

@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 Nov 11, 2025
@AdnaanA AdnaanA added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Nov 19, 2025
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.

Almost done. One more question.

typeof obj === "object" &&
obj !== null &&
!Array.isArray(obj) &&
property in obj
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider the following two approaches for determining if an object contains a property:

  let obj = {}, propertyName = "toString";
  console.log( propertyName in obj );                // true
  console.log( Object.hasOwn(obj, propertyName) );   // false

Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.

Copy link
Author

Choose a reason for hiding this comment

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

property in obj --> will return true for own properties and inherited properties. This will give false positives in my use-cases.

While:

Object.hasOwn(obj, property) --> will return true only for properties defined directly on the object

Comment on lines 29 to 45
test("Checks if an object contains the specified property", () => {
const obj1 = { name: "Alice", age: 30 };
expect(contains(obj1, "name")).toBe(true); // The property "name" exists
expect(contains(obj1, "city")).toBe(false); // The property "city" does not exist
});

// contains({ a: 1, b: 2 }, 'a') // returns true
test("Checks if an object contains the specified property", () => {
const obj1 = { a: 1, b: 2 };
expect(contains(obj1, "a")).toBe(true); // The property "name" exists
});

// contains({ a: 1, b: 2 }, 'c') // returns false
test("Checks if an object contains the specified property", () => {
const obj1 = { a: 1, b: 2 };
expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider grouping these tests into one category.

Copy link
Author

Choose a reason for hiding this comment

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

these have been grouped

// contains({ a: 1, b: 2 }, 'a') // returns true and contains({ a: 1, b: 2 }, 'c') // returns false
test("Checks if an object contains the specified property", () => {
const obj1 = { a: 1, b: 2 };
expect(contains(obj1, "a")).toBe(true); // The property "name" exists
expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist
});

@cjyuan cjyuan removed the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Nov 19, 2025
@AdnaanA AdnaanA added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Nov 20, 2025
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.

Changes look good, and responses were clear.

Comment on lines +29 to +41
test("Checks if an object contains the specified property", () => {
const obj1 = { name: "Alice", age: 30 };
expect(contains(obj1, "name")).toBe(true); // The property "name" exists
expect(contains(obj1, "city")).toBe(false); // The property "city" does not exist
});

// contains({ a: 1, b: 2 }, 'a') // returns true and contains({ a: 1, b: 2 }, 'c') // returns false
test("Checks if an object contains the specified property", () => {
const obj1 = { a: 1, b: 2 };
expect(contains(obj1, "a")).toBe(true); // The property "name" exists
expect(contains(obj1, "c")).toBe(false); // The property "c" does not exist
});

Copy link
Contributor

Choose a reason for hiding this comment

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

These two tests are quite similar.

It would be more useful to test samples with different characteristics. For examples

  • set obj1 to {a : 'c', b: 2 } to ensure the function won't check the value of the properties.
  • Use property names with different letter case to ensure the check is case sensitive.

@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 Nov 20, 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