London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953
London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953mohsenzamanist wants to merge 1 commit intoCodeYourFuture:mainfrom
Conversation
| ingredients: | ||
| ${recipe}`); | ||
| console.log(`${recipe.title} serves ${recipe.serves}\ningredients:`); | ||
| recipe.ingredients.forEach((ingredient) => console.log(ingredient)); |
There was a problem hiding this comment.
Could also explore using Array.prototype.join() to produce the desired string .
| const keys = Object.keys(obj); | ||
| return keys.includes(property); |
There was a problem hiding this comment.
This work.
Suggestion: Look up these two alternatives and find out their differences
JS "in" operator vs Object.hasOwn
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| it("contains returns false or throws an error if given parameter is not a valid object", () => { | ||
| expect(contains([], "key1")).toEqual(false); |
There was a problem hiding this comment.
Array is a kind of object where indexes serve as its keys.
With your current implementation,
contains([], "key1") returns false because "key1" is not a key of the empty array.
However, contains(["A", "B"], "1") would return true.
| expect(() => contains(undefined, "key1")).toThrow( | ||
| "The parameter given is not a plain JS object." | ||
| ); | ||
| expect(() => contains(null, "key1")).toThrow( | ||
| "The parameter given is not a plain JS object." | ||
| ); |
There was a problem hiding this comment.
Number is not a plain JS object but it is given a different treatment.
Wouldn't it be "friendlier to the caller" if the function can be designed to behave consistently for any value that is not an object or is an array?
| // Then it should return false or throw an error | ||
| it("contains returns false or throws an error if given parameter is not a valid object", () => { | ||
| expect(contains([], "key1")).toEqual(false); | ||
| expect(contains("key1:value1", "key1")).toEqual(false); |
There was a problem hiding this comment.
With your current implementation, this test (with the 2nd argument changed to "1") would fail even though the first argument is a string.
expect(contains("key1:value1", "1")).toEqual(false);
| const existingKeys = Object.keys(queryParams); | ||
| if (key === "" && value === "") continue; | ||
| if (existingKeys.includes(key)) { |
There was a problem hiding this comment.
We could just check queryParams[key] directly. This way, we don't have to keep creating an array of keys.
There was a problem hiding this comment.
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"?
| function tally(list) { | ||
| if (!Array.isArray(list)) throw new Error("Not an array."); | ||
| return list.reduce((acc, curr) => { | ||
| 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.
| // 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.
"Target return value" refers to the expected return value (when the function is correctly implemented).
| function countWords(string) { | ||
| const noPunctuationStr = string.replace(/[.,!?]/g, ""); | ||
| const wordArray = noPunctuationStr.split(" "); | ||
| let wordCount = new Map(); | ||
| for (let word of wordArray) { | ||
| wordCount.set(word, (wordCount.get(word) || 0) + 1); | ||
| } | ||
| const sortedWordCount = [...wordCount.entries()].sort((a, b) => b[1] - a[1]); | ||
| return sortedWordCount; |
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(" Hello World ");
Learners, PR Template
Self checklist
Changelist
Complete Sprint 2 exercises.
Questions