-
-
Notifications
You must be signed in to change notification settings - Fork 191
London | 25-ITP-Sep| Sophia Mohamed | Sprint 2 | Sprint 2 Coursework #861
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
cjyuan
left a comment
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.
Can you check if any of this general feedback can help you further improve your code?
https://github.com/CodeYourFuture/Module-Data-Groups/blob/general-review-feedback/Sprint-2/feedback.md
Doing so can help reviewers speed up the review process. Thanks.
Now the function can decode a URL-encoded string in JavaScript
Now the function can test if the array is empty
|
I have updated all the codes |
Sprint-2/debug/recipe.js
Outdated
| ${recipe.ingredients[0]} | ||
| ${recipe.ingredients[1]} | ||
| ${recipe.ingredients[2]} | ||
| ${recipe.ingredients[3]}`); |
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.
Can you figure out an approach that could work for any number of ingredients?
Sprint-2/implement/contains.test.js
Outdated
| test("contains on invalid parameters returns false", () => { | ||
| expect(contains([], "a")).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.
Which of the parameters on line 48 is "invalid"?
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.
You’re right — in that test, neither parameter is actually invalid.
[ ] is still an object in JavaScript, and "a" is a valid key, so the test title didn’t match the behaviour.
I’ve now updated the test to use invalid parameters (such as strings, numbers, null, and undefined)
Sprint-2/implement/lookup.js
Outdated
| for (const pair of pairs) { | ||
| const country = pair[0];// first element is country code | ||
| const currency = pair[1];// second element is currency code |
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.
Could consider using array destructuring syntax to simplify the code on lines 4-6.
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.
Thanks for the feedback!
I’ve updated the loop to use array destructuring so the code is cleaner and easier to read.
| function parseQueryString(query) { | ||
| const result = {}; | ||
| if (!query) return result; | ||
|
|
||
| const pairs = query.split("&"); | ||
|
|
||
| for (let pair of pairs) { | ||
| const index = pair.indexOf("="); | ||
|
|
||
| // Key has no "=" → value is empty string | ||
| if (index === -1) { | ||
| const key = decodeURIComponent(pair); | ||
| result[key] = ""; | ||
| continue; | ||
| } | ||
|
|
||
| // Key is before "=", value is EVERYTHING after "=" | ||
| const rawKey = pair.slice(0, index); | ||
| const rawValue = pair.slice(index + 1); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const key = decodeURIComponent(rawKey); | ||
| const value = decodeURIComponent(rawValue); | ||
|
|
||
| result[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| return result; | ||
| } |
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 work well.
Could also consider explore and exploit a built-in function to parse query string.
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 the suggestion!
I have now explored the built-in URLSearchParams
| test("counts how many times each item appears", () => { | ||
| expect(tally(["a", "b", "a", "c", "b", "a"])).toEqual({ | ||
| a: 3, | ||
| b: 2, | ||
| c: 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.
Does tally(["constructor", "constructor"]) return what you expect?
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!
tally(["constructor", "constructor"]) didn’t work properly because I was using a normal {} object, so "constructor" clashed with the built-in constructor property.
I used Object.create(null) so "constructor" behaves like normal keys and doesn’t clash with JavaScript’s internal properties.
cjyuan
left a comment
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.
Excellent work! You clearly know what the code does.
And thanks for the detailed responses.
Learners, PR Template
Self checklist
Changelist
I have implemented the required functions, and run the required tests
Questions
No questions, thank you