-
-
Notifications
You must be signed in to change notification settings - Fork 96
Westmidlands/Millena-Mesfin/Module Data Groups Sprint#2/Week 10 #485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code is very elegant!
I think there are some improvements you can make.
Feel free to mark this PR as "Complete" after you made the changes.
|
||
for (const key in author) { | ||
console.log(key); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you modify this to output the property values instead of property names?
function contains(obj,key) { | ||
return (key in obj); | ||
} |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you cj, I got to differentiate the (propertyName in obj) suits to my function.
test("contains an invalid parameter like array should return false" ,() => { | ||
const obj = ["code","your","future"]; | ||
expect(contains(obj,'age')).toBe(false); | ||
expect(contains(obj,'address')).toBe(false); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An array is a kind of object in JS.
The indexes serve as the keys (or property names) in an array. So contains(["code","your","future"], '0')
would return true
.
If the function needs to return false
when the parameter is an array, the function will need to check if the parameter is an array or not.
test("An array with invalid input should throw an error", () => { | ||
const count = ["hello", "hey", "hey", "hello", "hello", "hi"]; | ||
const expectedOutput = {hello: 3, hey: 2, hi: 1 }; | ||
|
||
expect(tally(count)).toEqual(expectedOutput); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The description and the code does not match.
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.