File tree Expand file tree Collapse file tree 2 files changed +64
-6
lines changed Expand file tree Collapse file tree 2 files changed +64
-6
lines changed Original file line number Diff line number Diff line change 1
1
function createLookup ( arr ) {
2
- // const arr1 =
3
- // const arr2 =
2
+ let object = { } ;
3
+
4
+ if ( ! Array . isArray ( arr ) || arr . length < 1 ) {
5
+ throw new Error (
6
+ "The array is not array or have length less than 2 values."
7
+ ) ;
8
+ }
9
+
4
10
for ( let i = 0 ; i < arr . length ; i ++ ) {
5
- let newArray = countryCurrencyPairs [ i ] ;
6
- console . log ( newArray ) ;
11
+ // looping through an array to get arrays inside
12
+ // current an array. Using indexing.
13
+ const keyValuePair = arr [ i ] ;
14
+
15
+ if ( ! Array . isArray ( keyValuePair ) || keyValuePair . length !== 2 ) {
16
+ throw new Error (
17
+ "The arrays inside are not arrays or have length less than 2 values."
18
+ ) ;
19
+ }
20
+
21
+ const country_code = keyValuePair [ 0 ] ; // assigning properties names
22
+ const currency_code = keyValuePair [ 1 ] ;
23
+ if ( typeof country_code !== "string" || typeof currency_code !== "string" ) {
24
+ throw new Error ( "The type of key-value pair is not string." ) ;
25
+ }
26
+ object [ country_code ] = currency_code ; // populate the empty created earlier object
7
27
}
8
- return newArray ;
28
+ return object ;
9
29
}
10
30
11
31
const countryCurrencyPairs = [
12
32
[ "US" , "USD" ] ,
13
33
[ "CA" , "CAD" ] ,
14
34
] ;
15
35
const result = createLookup ( countryCurrencyPairs ) ;
36
+ console . log ( result ) ;
37
+
16
38
module . exports = createLookup ;
Original file line number Diff line number Diff line change 1
1
const createLookup = require ( "./lookup.js" ) ;
2
2
3
- test . todo ( "creates a country currency code lookup for multiple codes" ) ;
3
+ describe ( "edge cases of the lookup function" , ( ) => {
4
+ test ( "creates a country currency code lookup for multiple codes" , ( ) => {
5
+ const currentOutput = createLookup ( [
6
+ [ "country" , "currency" ] ,
7
+ [ "country1" , "currency1" ] ,
8
+ [ "country2" , "currency2" ] ,
9
+ ] ) ;
10
+ const targetOutput = {
11
+ country : "currency" ,
12
+ country1 : "currency1" ,
13
+ country2 : "currency2" ,
14
+ } ;
15
+ expect ( currentOutput ) . toStrictEqual ( targetOutput ) ;
16
+ } ) ;
17
+
18
+ test ( "creates a country currency code lookup for empty array" , ( ) => {
19
+ // const currentOutput = createLookup([]);
20
+ // const targetOutput =
21
+ expect ( ( ) => createLookup ( [ ] ) ) . toThrow (
22
+ "The array is not array or have length less than 2 values."
23
+ ) ;
24
+ } ) ;
25
+
26
+ test ( "creates a country currency code lookup for array with wrong type key-value pairs" , ( ) => {
27
+ // const currentOutput = createLookup([
28
+ // [3, 2],
29
+ // [3, 1],
30
+ // ]);
31
+ // const targetOutput = "The type of key-value pair is not string.";
32
+ expect ( ( ) =>
33
+ createLookup ( [
34
+ [ 3 , 2 ] ,
35
+ [ 3 , 1 ] ,
36
+ ] )
37
+ ) . toThrow ( "The type of key-value pair is not string." ) ;
38
+ } ) ;
39
+ } ) ;
4
40
5
41
/*
6
42
You can’t perform that action at this time.
0 commit comments