Skip to content

Commit 5866558

Browse files
committed
implement createLookup()
1 parent bced4fe commit 5866558

1 file changed

Lines changed: 49 additions & 2 deletions

File tree

‎Sprint-2/implement/lookup.js‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,52 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(countryCurrencyPairs) {
2+
//if countryCurrencyPairs is not an array, throw error
3+
if (!Array.isArray(countryCurrencyPairs)) {
4+
throw new Error("Invalid input. It should be an array");
5+
}
6+
//if it is an empty array, throw error
7+
if (countryCurrencyPairs.length === 0) {
8+
throw new Error("Input should not be an empty array");
9+
}
10+
11+
//if not all elements are an array in the array, throw an error
12+
if (!countryCurrencyPairs.every(Array.isArray)) {
13+
throw new Error("Invalid input. All elements should be arrays");
14+
}
15+
16+
return Object.fromEntries(countryCurrencyPairs);
317
}
418

519
module.exports = createLookup;
20+
21+
// console.log(
22+
// createLookup([
23+
// ["US", "USD"],
24+
// ["CA", "CAD"],
25+
// ])
26+
// );
27+
28+
// console.log(createLookup([]));
29+
30+
/*
31+
When
32+
- createLookup function is called with the country-currency array as an argument
33+
34+
Then
35+
- It should return an object where:
36+
- The keys are the country codes
37+
- The values are the corresponding currency codes
38+
39+
Example
40+
Given: [['US', 'USD'], ['CA', 'CAD']]
41+
42+
When
43+
createLookup(countryCurrencyPairs) is called
44+
45+
Then
46+
It should return:
47+
{
48+
'US': 'USD',
49+
'CA': 'CAD'
50+
}
51+
52+
*/

0 commit comments

Comments
 (0)