Skip to content

Commit 47c979a

Browse files
committed
lookup function and testing it
1 parent 36938ec commit 47c979a

File tree

2 files changed

+64
-6
lines changed

2 files changed

+64
-6
lines changed

Sprint-2/implement/lookup.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
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+
410
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
727
}
8-
return newArray;
28+
return object;
929
}
1030

1131
const countryCurrencyPairs = [
1232
["US", "USD"],
1333
["CA", "CAD"],
1434
];
1535
const result = createLookup(countryCurrencyPairs);
36+
console.log(result);
37+
1638
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,42 @@
11
const createLookup = require("./lookup.js");
22

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+
});
440

541
/*
642

0 commit comments

Comments
 (0)