London | 26-ITP-May | Raihan Sharif | Sprint 2 | Exercises#1220
London | 26-ITP-May | Raihan Sharif | Sprint 2 | Exercises#1220RaihanSharif wants to merge 26 commits into
Conversation
Can account for special characters in key or value, unless fundametally unparseable, like & in key or value; or & in value.
…re are multiple values.
|
|
||
| function contains(object, key) { | ||
| if (isObject(object)) { | ||
| return key in object; |
There was a problem hiding this comment.
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.
hasOwn excludes inherited properties. I hadn't considered that we'd want to exclude inherited properties.
I suppose in this case, we do want to exlcude inherited properties.
There was a problem hiding this comment.
The spec didn't specify. So as long as you are aware of the difference, that's enough.
|
|
||
| if value at key is an array, and contains the incoming value, ignore. Else, push it to the array. | ||
| */ | ||
| if (Object.prototype.hasOwnProperty.call(queryParams, key)) { |
There was a problem hiding this comment.
Could also use Object.hasOwn()
| acc[curr] = (acc[curr] ?? 0) + 1; | ||
| return acc; | ||
| }, {}); |
There was a problem hiding this comment.
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, or
- use
Object.hasOwn()
| if (Object.prototype.hasOwnProperty.call(invertedObj, String(value))) { | ||
| throw new Error(`Duplicate value "${value}" found — cannot invert`); | ||
| } | ||
| invertedObj[String(value)] = key; |
There was a problem hiding this comment.
All values (except Symbol) when used as keys, are converted implicitly to string. So a cast to string is optional.
… properties in prototype chain
|
Changes look good. Well done. |
Learners, PR Template
Self checklist
Changelist
Done all exercises. For the invert exercises, made decision to prevent inverting where multiple keys have the same value. e.g. {a: "f", b: "f"} as this would produce {f: "b"}, thereby losing some information.