-
-
Notifications
You must be signed in to change notification settings - Fork 221
Sheffield | 25-ITP-Sep | Xiayidan Abuxuukuer | Sprint 2 | Coursework #874
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This fulfils the requirements, now think of the below:
This will help make this more better and less cluttered! |
||
| if (!obj || typeof obj !== "object" || Array.isArray(obj)){ | ||
|
|
||
| module.exports = contains; | ||
| return false; | ||
|
|
||
| } | ||
| } | ||
|
|
||
| if (obj.hasOwnProperty(prop)){ | ||
|
|
||
| return true; | ||
|
|
||
| } | ||
| else{ | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,17 @@ | ||
| function createLookup() { | ||
| function createLookup(pairs) { | ||
| // implementation here | ||
|
|
||
| const lookup = {}; | ||
|
|
||
| for (let i = 0; i < pairs.length; i++){ | ||
|
|
||
| const country = pairs[i][0]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is cleaner way |
||
| const country = pairs[i][1]; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a typo here, |
||
| lookup[country] = currency; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does |
||
|
|
||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,27 @@ function parseQueryString(queryString) { | |
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| for (let i = 0; i < keyValuePairs.length; i++){ | ||
| const pair = keyValuePairs[i]; | ||
| const indexOfEquals = pair.indexOf("="); | ||
| if(indexOfEquals === -1){ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we handle key with no value? |
||
| } | ||
| else{ | ||
| const key = pair.slice(0, indexOfEquals); | ||
| const value = pair.slice(indexOfEquals + 1); | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
|
|
||
| //for (const pair of keyValuePairs) { | ||
| //const [key, value] = pair.split("="); | ||
| //queryParams[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,28 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
|
|
||
| if (!Array.isArray(arr)) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a typo, does |
||
| throw new Error("tally expects an array!"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| if (arr.length === 0){ | ||
|
|
||
| return counts; | ||
| } | ||
| for (let i = 0; i < arr.length; i++){ | ||
|
|
||
| const item = arr[i]; | ||
|
|
||
| if (counts[item]) { | ||
|
|
||
| counts[item]= counts[item] + 1; | ||
| } | ||
| else { | ||
| counts[item]= 1; | ||
| } | ||
|
Comment on lines
+17
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we start at |
||
| } | ||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,27 @@ function invert(obj) { | |
| } | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| {key: 1} | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| {key: 2} | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| {"1": "a", "2": "b"} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| [["a", 1], ["b", 2]] | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is asking why does the current |
||
| Object.entries gives you: the key and the value,So we can swap them easily. | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will create |
||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
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 works, however please note there are more cleaner ways to achieve this. Knowing those will definitely help in the future.