Sheffield | 26-Jan-ITP | Daniel Aderibigbe | Sprint 2 | Coursework/sprint 2#1053
Sheffield | 26-Jan-ITP | Daniel Aderibigbe | Sprint 2 | Coursework/sprint 2#1053Dan2Clouted wants to merge 4 commits intoCodeYourFuture:mainfrom
Conversation
| @@ -1,3 +1,5 @@ | |||
| function contains() {} | |||
| function contains(object, property) { | |||
| return Object.prototype.hasOwnProperty.call(object, property); | |||
There was a problem hiding this comment.
-
The function is expected to throw an error or return
falsewhen object is not an object or is an array. -
Could also explore
Object.hasOwn().
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains with invalid parameters returns false", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
| }); |
There was a problem hiding this comment.
expect(contains([], 'a')).toBe(false) may fail to verify whether
contains() can detect its first argument is an array.
contains([], "a") could also return false because "a" is not a property (or key) of an array.
However, "0", "1", "2" are keys of [1, 2, 3], so it is better to specify the test as
expect(contains([1, 2, 3], "1")).toBe(false); (to ensure you are checking what you describe).
You could also add a few more samples to ensure the function can properly handle arguments of different types.
| const indexOfEquals = pair.indexOf("="); | ||
|
|
||
| if (indexOfEquals === -1) { | ||
| queryParams[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, indexOfEquals); | ||
| const value = pair.slice(indexOfEquals + 1); | ||
| queryParams[key] = value; | ||
| } |
There was a problem hiding this comment.
For each of the following function calls, does your function return the value you expect?
parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
Note: (You don't have to implement this)
-
In real query string, both
keyandvalueare percent-encoded or URL encoded in the URL.
For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
(whether it is a key or value in the query string), we need to call a function to decode it. -
You can also explore the
URLSearchParamsAPI.
| const result = {}; | ||
|
|
||
| for (const item of items) { | ||
| if (result[item]) { | ||
| result[item]++; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| } |
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.
| function countWords(str) { | ||
| const result = {}; | ||
|
|
||
| if (str.length === 0) { | ||
| return result; | ||
| } | ||
|
|
||
| const words = str.split(" "); | ||
|
|
||
| for (const word of words) { | ||
| if (result[word]) { | ||
| result[word]++; | ||
| } else { | ||
| result[word] = 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } |
There was a problem hiding this comment.
Can you check if your function returns what you expect in the following function calls?
countWords("Hello,World! Hello World!");
countWords("constructor constructor");
countWords(" Hello World ");
Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check.
Learners, PR Template
Self checklist
Changelist
Completed all Exercises.