Skip to content

Commit df82c4f

Browse files
committed
fixed all errors to pass all tests
1 parent 5eee074 commit df82c4f

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

Sprint-2/implement/lookup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ function Lookup(countryCurrencyPairs) {
99
if (
1010
!Array.isArray(countryCurrencyPairs) ||
1111
// check if each inner array is an array.
12-
!countryCurrencyPairs.every((pair) => Array.isArray(pair)) ||
12+
countryCurrencyPairs.some((pair) => !Array.isArray(pair)) ||
1313
// check if each inner array has two elements.
14-
!countryCurrencyPairs.every((pair) => pair.length === 2)
14+
countryCurrencyPairs.some((pair) => pair.length !== 2)
1515
// we can also use index to check the pair is an array as follows:
1616
// for (let i = 0; i < countryCurrencyPairs.length; i++)
1717
// return Array.isArray(countryCurrencyPairs[i])

Sprint-2/implement/lookup.test.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,36 @@ It should return:
4646
}
4747
*/
4848
// Given an invalid input (not an array of arrays),
49-
test('return "Invalid input: expected an array of arrays" for string input', () => {
50-
const input = "invalid input: expected an array of arrays";
51-
expect(Lookup(input)).toEqual("Invalid input: expected an array of arrays");
49+
test('given string input throw "Invalid input: expected an array of arrays"', () => {
50+
let input = "invalid input: expected an array of arrays";
51+
expect(() => Lookup(input)).toThrow(
52+
"Invalid input: expected an array of arrays"
53+
);
5254
});
5355

5456
// Given an array where its elements are not arrays,
55-
test('return "Invalid input: expected an array of arrays" for non-array elements', () => {
56-
const input = [["US", "USD"], "CA"];
57-
expect(Lookup(input)).toEqual("Invalid input: expected an array of arrays");
57+
test('given array with non-array elements throw "Invalid input: expected an array of arrays"', () => {
58+
let input = [["US", "USD"], "CA"];
59+
expect(() => Lookup(input)).toThrow(
60+
"Invalid input: expected an array of arrays"
61+
);
5862
});
5963

6064
// Given an array where its elements are arrays with more than two elements,
61-
test('return "Invalid input: expected an array of arrays" for arrays with too many elements', () => {
65+
test('given arrays with too many elements throw "Invalid input: expected an array of arrays"', () => {
6266
const input = [
6367
["US", "USD", "flag"],
6468
["CA", "CAD"],
6569
];
66-
expect(Lookup(input)).toEqual("Invalid input: expected an array of arrays");
70+
expect(() => Lookup(input)).toThrow(
71+
"Invalid input: expected an array of arrays"
72+
);
6773
});
6874

6975
// Given an array where its elements are arrays with less than two elements,
70-
test('return "Invalid input: expected an array of arrays" for arrays with too few elements', () => {
76+
test('given arrays with too few elements throw "Invalid input: expected an array of arrays"', () => {
7177
const input = [["US", "USD"], ["CA"]];
72-
expect(Lookup(input)).toEqual("Invalid input: expected an array of arrays");
78+
expect(() => Lookup(input)).toThrow(
79+
"Invalid input: expected an array of arrays"
80+
);
7381
});

Sprint-2/implement/tally.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ function tally(arr) {
22
if (!Array.isArray(arr)) {
33
throw new Error("invalid input");
44
}
5-
let result = {};
5+
// using Object.create(null) to create a plain object without prototype
6+
let result = Object.create(null);
67
for (let i = 0; i <= arr.length - 1; i++) {
78
let item = arr[i];
89
result[item] = (result[item] || 0) + 1;

Sprint-2/implement/tally.test.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,6 @@ test("tally on an empty array returns an empty object", () => {
2727
expect(tally([])).toEqual({});
2828
});
2929

30-
// Given an array with duplicate items
31-
// When passed to tally
32-
// Then it should return counts for each unique item
33-
test("tally on an empty array returns an empty object", () => {
34-
expect(tally([])).toEqual({});
35-
});
36-
3730
// Given an array with duplicate items
3831
// When passed to tally
3932
// Then it should return counts for each unique item
@@ -52,18 +45,22 @@ test("tally on an array with duplicates returns correct counts for each unique i
5245
).toEqual({ CYF: 2, AWS: 1, Capgemini: 2, Deloitte: 1, Google: 1, Slack: 1 });
5346
});
5447

48+
test("tally on an array with duplicates returns correct counts for each unique item", () => {
49+
expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
50+
});
51+
52+
// Given an input that you mentioned in the review.
53+
//test("tally on an array with duplicates returns correct counts for each unique item", () => {
54+
// expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
55+
//});
56+
5557
// Given an invalid input like a string
5658
// When passed to tally
5759
// Then it should throw an error
5860
test("given invalid input throws an error", () => {
5961
expect(() => tally("invalid")).toThrow("invalid input");
6062
});
6163

62-
// Given an invalid input like a number
63-
test("given invalid input throws an error", () => {
64-
expect(() => tally("invalid")).toThrow("invalid input");
65-
});
66-
6764
// Given an invalid input like a number
6865
test("given invalid input throws an error", () => {
6966
expect(() => tally(123)).toThrow("invalid input");

0 commit comments

Comments
 (0)