-
-
Notifications
You must be signed in to change notification settings - Fork 192
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?
Changes from 8 commits
3999300
8163ad3
65b3e2d
53492c8
b8ae2b9
6cf8277
0d76de5
c64fdeb
2b867f0
9411e67
3be182c
ee8befa
96fc157
604d8ff
0e9c2c9
e3eee4c
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,11 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
| if (!obj || typeof obj !== "object" || Array.isArray(obj)) {//check if we have an object and makes sure it's not an array | ||
| return false; | ||
| } | ||
| if (Object.keys(obj).length === 0) { | ||
| return false; | ||
| } | ||
| return key in obj; | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,20 +16,37 @@ as the object doesn't contains a key of 'c' | |
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| test("returns true for an existing property", () => { | ||
| const obj = { a: 1, b: 2 }; | ||
| expect(contains(obj, "a")).toBe(true); | ||
| }); | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| test("contains on object with existing property returns true", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("contains on object with non-existent property returns false", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains on invalid parameters returns false", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
|
||
| expect(contains(null, "a")).toBe(false); | ||
| expect(contains(123, "a")).toBe(false); | ||
| expect(contains("hello", "a")).toBe(false); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const lookup = {};//start with an empty object | ||
|
|
||
| for (const pair of pairs) { | ||
| const country = pair[0];// first element is country code | ||
| const currency = pair[1];// second element is currency code | ||
|
||
| lookup[country] = currency; | ||
| } | ||
|
|
||
| return lookup; | ||
|
|
||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,29 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| const result = {}; | ||
|
|
||
| if (!queryString) { | ||
| return result; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const pairs = queryString.split("&"); | ||
|
|
||
| for (const pair of pairs) { | ||
| const index = pair.indexOf("="); // find FIRST "=" | ||
|
|
||
| if (index === -1) { | ||
| // no "=" found → key with empty value | ||
| result[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); // everything after "=" | ||
| result[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
| return result; | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
| module.exports = parseQueryString; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,12 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) {// if it is not array (string or number throw an error message | ||
| throw new TypeError("Input must be an array"); | ||
| } | ||
|
|
||
| return arr.reduce((counts, item) => { | ||
| counts[item] = (counts[item] || 0) + 1;// loops through the array and count | ||
| return counts; | ||
| }, {}); | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,11 +24,27 @@ const tally = require("./tally.js"); | |
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
|
|
||
| test("returns an empty object for an empty array", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| test.todo("tally counts each unique item in the array"); | ||
| test("counts how many times each item appears", () => { | ||
| expect(tally(["a", "b", "a", "c", "b", "a"])).toEqual({ | ||
| a: 3, | ||
| b: 2, | ||
| c: 1, | ||
| }); | ||
| }); | ||
|
Comment on lines
+35
to
+41
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
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. thank you! |
||
|
|
||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| test.todo("tally throws an error for non-array input"); | ||
| test("throws an error if input is not an array", () => { | ||
| expect(() => tally("not an array")).toThrow(TypeError); | ||
| expect(() => tally(123)).toThrow(TypeError); | ||
| expect(() => tally({})).toThrow(TypeError); | ||
| }); | ||
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?