-
-
Notifications
You must be signed in to change notification settings - Fork 270
London |ITP-Jan-2016 | Ping Wang | Sprint 2 |Coursework #1023
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?
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,15 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (typeof object !== 'object' || object === null || Array.isArray(object)) { | ||
| return false; | ||
| } | ||
| return object.hasOwnProperty(property); | ||
| } | ||
|
|
||
|
|
||
|
|
||
| const object={name:'jin', age:13} | ||
|
|
||
| console.log (contains(object, 'age')) | ||
| console.log (contains(object, 'nice')) | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,30 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| object={} | ||
| console.log(object,'') | ||
| test.todo("contains on empty object returns false"); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| object={name:'jin', age:13} | ||
| console.log (contains(object, 'age')) | ||
| test.todo("contains age property in object return true") | ||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| object={name:'jin', age:13} | ||
| console.log (contains(object, 'nice')) | ||
| test.todo("contains no nice property in object return false") | ||
|
|
||
|
|
||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| object={name:'jin', age:13} | ||
| console.log (contains(object, '[1,2,5]')) | ||
| test.todo("contains no array property in object return false") | ||
|
Comment on lines
+38
to
+49
Contributor
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.
Can you implement all the tests in all the |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const countryCodeCurrency={} | ||
| for (const[country, currency] of countryCurrencyPairs){ | ||
| countryCodeCurrency[country]=currency | ||
| } | ||
|
|
||
| return countryCodeCurrency | ||
| } | ||
|
Comment on lines
+1
to
8
Contributor
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. The spacing around the operators is not quite consistent. Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode, |
||
|
|
||
| console.log(createLookup([['GB','GBP']])) | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| const result=createLookup([['US','USD'],['CA','CAD']]) | ||
| console.log (result) | ||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
|
|
||
| /* | ||
|
|
@@ -33,3 +35,12 @@ It should return: | |
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
|
|
||
| // create single country currency in countryCurrencyPairs | ||
| const result1=createLookup([['GB','GBP']]) | ||
| console.log(result1) | ||
| test.todo('creates a single county code currency ') | ||
|
|
||
| //given an empty country currency pair/array | ||
| console.log(createLookup([[]])) | ||
| test.todo('return an empty object with empty pair given') | ||
|
Comment on lines
+40
to
+46
Contributor
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.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,24 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| if (typeof queryString!=="string"||queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const equalIndex = pair.indexOf("="); | ||
|
|
||
| const key = pair.slice(0, equalIndex); | ||
| const value = pair.slice(equalIndex + 1); | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
|
|
||
| console.log(parseQueryString("color=blue&quality=good")) | ||
| console.log(parseQueryString("equation=x=y+1")) | ||
| console.log(parseQueryString("")) | ||
|
Comment on lines
+20
to
+22
Contributor
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. For each of the following function calls, does your function return the value you expect? Note:
|
||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,27 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Invalid input: expected an array"); | ||
| } | ||
|
|
||
| const count={} | ||
| for(item of array){ | ||
| if (count[item]){ | ||
| count[item] += 1 | ||
| } | ||
| else{ | ||
| count[item] = 1 | ||
| } | ||
| } | ||
| return count | ||
| } | ||
|
Comment on lines
+2
to
+16
Contributor
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.
Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| console.log(tally([ 2,"bee",2,"apple","apple",2, "banana"])) | ||
|
|
||
| try { | ||
| console.log(tally('morning')); | ||
| } catch (err) { | ||
| console.error(err.message); | ||
| } | ||
|
|
||
|
|
||
| module.exports = tally; | ||
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.
Alternate approach to explore:
Since ingredient values are separated by '\n' in the output, we could also use
Array.prototype.join()to construct the equivalent string and then output the resulting string.