-
-
Notifications
You must be signed in to change notification settings - Fork 96
LONDON | GISLAINE DELLA BELLA | Module-Data-Groups | Week-2 #164
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 pretty solid.
I left a few comments. I marked the more important ones with Todo .
Note: Your current branch also contains modified files in the Sprint-1
folder.
// Check if the object is valid and has the property | ||
|
||
function contains(obj, property) { | ||
if (typeof obj === "object" && obj !== null && property 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.
Suggestion
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( obj.hasOwnProperty(propertyName) ); // false
Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs hasOwnProperty
.
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.
From my search - Key Difference
- Use in if you want to check all properties, including inherited ones.
- Use hasOwnProperty if you only care about the object's own properties.
What I understood I should use “hasOwnProperty “as I want to check only on the object and not on inherited ones. But the description says I need to check using “in”. ?
Therefore chnaging the function to:
function contains(obj, property) { // Check if obj is an object (not null) and not an array if ( typeof obj === "object" && obj !== null && Object.prototype.hasOwnProperty.call(obj, property) ) { return true; // Property exists as an own property } return false; // Property doesn't exist or obj is not a valid object }
it fails when rejecting checking if t is an Array.
return true; | ||
} else { | ||
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.
if (condition === true)
return true;
else
return false;
could be simplified as return (condition === true);
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.
function contains(obj, property) {
// Check if obj is an object (not null) and not an array
if (
typeof obj === "object" && obj !== null &&!Array.isArray(obj) && property in obj
) {
return (condition === true);
} else {
return false;
}
} // when I change to return (condition === true); one of my codes fail.
const arrayInput = ["value1", "value2"]; | ||
expect(contains(arrayInput, "key")).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.
Todo
Can you try also expect(contains(arrayInput, "1")).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.
Hi Cjyuan,
I have add!Array.isArray(obj) for the arrays to be considered as aa "object".
Test Suites: 1 passed, 1 total
Tests: 5 passed, 5 total
expect(result).toEqual(expectedTally); // Check if the result matches the expected output | ||
}); | ||
|
||
|
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.
Only one test?
// '1': 'a', '2': 'b', | ||
|
||
// c) What is the target return value when invert is called with {a : 1, b: 2} | ||
//key: 2 |
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.
Todo: Can you recheck the answers to questions (b) and (c)?
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.
a) What is the current return value when invert is called with { a : 1 }= return is { '1': 'a' }
b) What is the current return value when invert is called with { a: 1, b: 2 } = { '1': 'a', '2': 'b' }
c) What is the target return value when invert is called with {a : 1, b: 2} = { '1': 'a', '2': 'b' }
const input = { a: 1, b: 2 }; | ||
const expected = { 1: "a", 2: "b" }; | ||
expect(invert(input)).toEqual(expected); | ||
}); |
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.
What do you expect from invert( { a: 1, b: 1 } )
?
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 code will return true.
@@ -8,24 +8,25 @@ function totalTill(till) { | |||
let total = 0; | |||
|
|||
for (const [coin, quantity] of Object.entries(till)) { | |||
total += coin * quantity; | |||
// Remove 'p' and convert to number | |||
const numericCoin = parseInt(coin.replace("p", ""), 10); |
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.
parseInt()
can also work on the string value of coin
without first removing "p" from the string. You may want to find out how parseInt()
work.
} | ||
|
||
return queryParams; | ||
} |
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.
Todo
For each of the following function calls, does your function return the value you expected?
parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
parseQueryString("a%25b=c%26d`)
Note: the %25
and %26
are URL encoded or percent encoded characters.
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.
Hi Cyuan,
I did make some changes but honestly, it is another level to me. I'm studying these days JS things I kow I left behind and theses tests are one of them.
…sidering as a "object'
Learners, PR Template
Self checklist
Changelist
Good evening, Volunteer,
Here are my code exercises from Week 2. I'm still finding it a little confusing, but I’m getting better at reading and explaining the code. However, I’m not completely comfortable writing everything on my own just yet.
Thank you for your time and support,
Gislaine
Questions
Ask any questions you have for your reviewer.