-
-
Notifications
You must be signed in to change notification settings - Fork 211
Manchester| ITP-25-SEP | Fithi Teklom| Sprint 2 | Coursework #898
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 14 commits
020a638
00d78e4
106c8d5
7246481
f31c786
85c361c
d8bb643
9547061
89482b0
1903cda
57a0316
23fc493
d0281fd
6d3cd1a
f3cafed
d67e338
be4358d
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,12 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
|
|
||
| if(typeof obj !== "object" || obj === null || Array.isArray(obj)){ | ||
| return false; | ||
| } | ||
|
|
||
| return obj.hasOwnProperty(key); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ E.g. contains({a: 1, b: 2}, 'a') // returns true | |
| as the object contains a key of 'a' | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| as the object doesn't contain a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
@@ -20,16 +20,41 @@ 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("contains an empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
|
|
||
| test("contains array-like object,invalid object and null returns false",() => { | ||
| expect(contains([], "a")).toBe(false); | ||
|
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. Arrays are objects in JavaScript, and they do have property names -- just not the same ones as objects. When testing whether the function handles arrays properly, try using a key that an array might
Author
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. okay, i get it. |
||
| expect(contains(123, "a")).toBe(false); | ||
| expect(contains(null, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("returns true for existent property", () => { | ||
| const obj = {a: 1, b: 2} | ||
| expect(contains(obj, "a")).toBe(true); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("returns false for non existent properties", () => { | ||
| const obj1= {a: 1, b:2} | ||
| expect(contains("c")).toBe(false); | ||
| }); | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,10 @@ | ||
| function createLookup() { | ||
| function createLookup(pairs) { | ||
| // implementation here | ||
| const lookup = {}; | ||
| for(const [countryCode, currencyCode]of pairs){ | ||
| lookup[countryCode] = currencyCode; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,11 +6,19 @@ function parseQueryString(queryString) { | |
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
|
|
||
| const idx = pair.indexOf("="); | ||
|
|
||
| if (idx === -1) { | ||
| // No '=' found, treat entire pair as key with empty value | ||
| queryParams[pair] = ""; | ||
| } else { | ||
| const key = pair.substring(0, idx); | ||
| const value = pair.substring(idx + 1); | ||
| queryParams[key] = value; | ||
| } | ||
|
||
|
|
||
| return queryParams; | ||
| } | ||
| return queryParams; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const result = {}; | ||
|
||
|
|
||
| for (const item of items) { | ||
| if (result[item]) { | ||
| result[item] += 1; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
|
|
||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| const invert = require("./invert"); | ||
|
|
||
| test("inverts a simple object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ "1": "a" }); | ||
| }); | ||
|
|
||
| test("inverts multiple key/value pairs", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); | ||
| }); | ||
|
|
||
| test("inverts string values", () => { | ||
| expect(invert({ x: "apple", y: "banana" })).toEqual({ | ||
| apple: "x", | ||
| banana: "y" | ||
| }); | ||
| }); | ||
|
|
||
| test("throws an error when input is not an object", () => { | ||
| expect(() => invert("not an object")).toThrow("Input must be a non-null object"); | ||
| }); | ||
|
|
||
| test("throws an error for null", () => { | ||
| expect(() => invert(null)).toThrow("Input must be a non-null object"); | ||
| }); | ||
|
|
||
| test("throws an error for arrays", () => { | ||
| expect(() => invert([1, 2, 3])).toThrow("Input must be a non-null object"); | ||
| }); |
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 use an approach that works for any number of ingredients?