-
-
Notifications
You must be signed in to change notification settings - Fork 312
London | 26-ITP-May | Martin Mwaka | Sprint 2 | Exercises #1252
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
3453c9e
4399fc1
4a01f99
3a20728
3ce1f74
e0455d0
b603cf4
975a820
e773313
dab2b24
4848245
bbb2a56
87eb907
23e876b
7e09582
253201d
8a80a20
853d45b
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,13 @@ | ||
| function contains() {} | ||
| function contains(object, value) { | ||
| const isNotObject = | ||
| typeof object !== "object" || object === null || Array.isArray(object); | ||
|
|
||
| if (isNotObject) return false; | ||
|
|
||
| return Object.hasOwn(object, value); | ||
| } | ||
|
|
||
| console.log(contains({ a: 1, b: 2 }, "a")); | ||
| console.log(contains(["a", "b"], "a")); | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,84 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(data) { | ||
| validateData(data); | ||
| return data.reduce((lookup, [country, currency]) => { | ||
| lookup[country] = currency; | ||
| return lookup; | ||
| }, {}); | ||
| } | ||
|
|
||
| // check for valid pairs | ||
| const isValidPair = (value) => | ||
| Array.isArray(value) && | ||
| value.length === 2 && | ||
| typeof value[0] === "string" && | ||
| typeof value[1] === "string"; | ||
|
|
||
| // check for duplicates | ||
| const normaliseDataPair = ([country, currency]) => `${country}|${currency}`; | ||
| const isDuplicated = (data) => | ||
| new Set(data.map(normaliseDataPair)).size !== data.length; | ||
|
|
||
| const validateData = (data) => { | ||
| // check that it is an array | ||
| if (!Array.isArray(data)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
| // check that input is not empty | ||
| if (data.length === 0) { | ||
| throw new Error("Input must not be an empty array"); | ||
| } | ||
|
|
||
| // check that the values are valid | ||
| if (data.some((value) => !isValidPair(value))) { | ||
| throw new Error("Input must be valid text pairs"); | ||
| } | ||
|
|
||
| // check for duplicates | ||
| if (isDuplicated(data)) { | ||
| throw new Error("Input must not be duplicated"); | ||
| } | ||
|
|
||
| // check country code has 2 uppercase letters | ||
| if (data.some(([country]) => !/^[A-Z]{2}$/.test(country))) { | ||
| throw new Error("Country code must be 2 uppercase letters"); | ||
| } | ||
| // check currency code has 3 uppercase letters | ||
| if (data.some(([, currency]) => !/^[A-Z]{3}$/.test(currency))) { | ||
| throw new Error("Currency code must be 3 uppercase letters"); | ||
| } | ||
| }; | ||
|
|
||
| // Handle invalid cases | ||
| const INVALID_CASES = [ | ||
| [[], "Input must not be an empty array"], | ||
| ["hello", "Input must be an array"], | ||
| [[1, 2, 3], "Input must be valid text pairs"], | ||
| [ | ||
| [ | ||
| ["US", "USD"], | ||
| ["US", "USD"], | ||
| ], | ||
| "Input must not be duplicated", | ||
| ], | ||
| ]; | ||
|
|
||
| const printInvalidCases = () => { | ||
| for (const [data, expectedMessage] of INVALID_CASES) { | ||
| try { | ||
| createLookup(data); | ||
| console.log(`Unexpected success for ${JSON.stringify(data)}`); | ||
| } catch (error) { | ||
| console.log("-".repeat(50)); | ||
| console.log( | ||
| `Data: ${JSON.stringify(data)}\nExpected: ${expectedMessage}\nReceived: ${error.message}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (require.main === module) { | ||
| console.log("DATA ERRORS"); | ||
| printInvalidCases(); | ||
| console.log("-".repeat(50)); | ||
| } | ||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,35 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| return array.reduce((acc, item) => { | ||
| acc[item] = (acc[item] ?? 0) + 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. Can you explain how ?? works here?
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. This is the nullish coalescing operator. If the acc[item] is undefined it will set it to zero 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. Good answer! This task is complete now |
||
| return acc; | ||
| }, {}); | ||
| } | ||
|
|
||
| // Handle invalid cases | ||
| const INVALID_CASES = [["hello", "Input must be an array"]]; | ||
|
|
||
| const printInvalidCases = () => { | ||
| for (const [data, expectedMessage] of INVALID_CASES) { | ||
| try { | ||
| tally(data); | ||
| console.log(`Unexpected success for ${JSON.stringify(data)}`); | ||
| } catch (error) { | ||
| console.log("-".repeat(50)); | ||
| console.log( | ||
| `Data: ${JSON.stringify(data)}\nExpected: ${expectedMessage}\nReceived: ${error.message}` | ||
| ); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| if (require.main === module) { | ||
| console.log("ERROR MESSAGES"); | ||
| printInvalidCases(); | ||
| console.log("-".repeat(50)); | ||
| } | ||
|
|
||
| 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.
Good work on this extensive input validation