-
-
Notifications
You must be signed in to change notification settings - Fork 93
ITP-Jan | NW | Jovy So | Module-Data-Groups | WEEK 2 #508
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 generally good.
I left some comments and suggestions.
Sprint-2/implement/contains.js
Outdated
if (!propertyName) return false; | ||
if (!object) return 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.
This two statements would not return false when object
is "ABC"
or 123
.
Sprint-2/implement/contains.test.js
Outdated
test("contains invalid parameters return false", () => { | ||
expect(contains([1,2,3])).toEqual(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.
Array is a kind of objects in JS, where its keys are its indexes.
With your current implementation, contains([1,2,3], '0')
would return true
.
Please note that "ABC".hasOwnProperty('0') would also return true.
To properly reject parameters that is one of these
- not an object
- null
- is an array
you have to "improve" your check in contains()
.
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 for you comments! I have improved the function and the test in contains()
. Please feel free to have a look. Thank you.
Sprint-2/implement/querystring.js
Outdated
const [key, value] = pair.split(/=(.*)/s); | ||
queryParams[key] = value; |
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.
Interesting approach.
Please note that in real querystring, both key
and value
are percent-encoded or URL encoded in the URL. For example, the string "5%" will be encoded as "5%25". So to get the actual value of "5%25" (whether it is a key or value in the querystring), you should call a function to decode it.
May I suggest looking up any of these terms, and "How to decode URL encoded string in JS"?
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 for your suggestion. I have found some results and tried to improve the function and I am gonna explore more.
|
||
// d) Explain why the current return value is different from the target output | ||
// There are two problems in the current function | ||
// 1. key and value haven't been swap | ||
// 2. invertedObj.key is unable to return the key of the object |
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 statement invertedObj.key is unable to return the key of the object
is not quite clear.
May I suggest asking ChatGPT the difference between obj.key
and obj[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.
Of course, let me try to explain after asking ChatGPT.
When I know the key name, I can use obj.key
, it treats key
as a literal property name ("key"
)
When key name is dynamic or in a variable, I need to use obj[key]
, it treats key
as a variable.
In this case, I don't know the actual name of the key (because it can be 'x' / 'y' / 'a' or something else), so I have to use obj[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.
Changes look good.
if (!object) return false; | ||
// if (!propertyName) return false; | ||
// if (!object) return false; | ||
if (typeof object !== "object" || object === null || Array.isArray(object) || typeof object === "string") return 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.
This check is, typeof object === "string"
, is not needed because a string is not an object; the first condition would already be true.
Learners, PR Template
Self checklist
Changelist
Briefly explain your PR.
Questions
Ask any questions you have for your reviewer.