-
-
Notifications
You must be signed in to change notification settings - Fork 208
Glasgow | 25-ITP-SEP | Hanna Mykytiuk |Sprint 2| Coursework #828
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,15 @@ | ||
| function contains() {} | ||
| function contains(object, property) { | ||
| if (Object.keys(object).length === 0 ){ | ||
| return false; | ||
| } | ||
| if (object.hasOwnProperty(property)){ | ||
| return true; | ||
| } | ||
| else{ | ||
| return false; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| module.exports = contains; | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,37 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| //test.todo("contains on empty object returns false"); | ||
| test("given an empty object, it should return false", () => { | ||
| const currentOutput = contains({}); | ||
| const targetOutput = false; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("given an object with properties, when passed to contains with an existing property name it should return true", () => { | ||
| const currentOutput = contains({a: 1, b: 2}, 'a') ; | ||
| const targetOutput = true; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("given an object with properties, when passed to contains with a non-existent property name it should return false", () => { | ||
| const currentOutput = contains({a: 1, b: 2}, 'c') ; | ||
| const targetOutput = false; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("given ivalid parameters like an array, when passed to contains it should return false throw an error", () => { | ||
| const currentOutput = contains([1,'a'], 'c') ; | ||
|
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.
|
||
| const targetOutput = false; | ||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(country_currency) { | ||
| let obj = Object.fromEntries(country_currency); | ||
| return obj | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,10 +3,14 @@ function parseQueryString(queryString) { | |
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| const keyValuePairs = queryString.split("&");//method split gives us an array | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const indexForKey = pair.indexOf('='); | ||
| const key = pair.substring(0,indexForKey); | ||
| const value = pair.substring(indexForKey+1, pair.length) | ||
|
|
||
|
|
||
| queryParams[key] = value; | ||
|
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. In real query string, both
Can your function handle URL-encoded query string? Suggestion: Look up "How to decode a URL-encoded string in JavaScript". |
||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,6 @@ test("parses querystring values containing =", () => { | |
| "equation": "x=y+1", | ||
| }); | ||
| }); | ||
| test("must return empty object if query string is empty", () => { | ||
| expect(parseQueryString("")).toEqual({ }); | ||
| }); | ||
|
Comment on lines
+13
to
+15
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. What do you expect from the following function calls? parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
parseQueryString("a&b&c") |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)){ //check if array is array | ||
| throw new Error("Invalid input"); | ||
| } | ||
| if (array.length === 0 ){ | ||
| return {}; | ||
| } | ||
| const obj = {} | ||
|
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. Does the following function call returns the value you expect? Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
| for (let item of array){ | ||
| if (obj[item]){ | ||
| obj[item]+=1; | ||
| } | ||
| else{ | ||
| obj[item]=1; | ||
| } | ||
| } | ||
| return obj | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| // Given an object | ||
| // When invert is passed this object | ||
| // Then it should swap the keys and values in the object | ||
|
|
||
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| test("Given an object, then it should swap the keys and values in the object ", () => { | ||
| expect( invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"}); | ||
| }); |
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 change the code on line 15 so that the code meets the requirement on line 4?