Skip to content

Commit a15e7d1

Browse files
author
Enice-Codes
committed
modified work
1 parent 45df013 commit a15e7d1

5 files changed

Lines changed: 44 additions & 8 deletions

File tree

‎Sprint-2/implement/lookup.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(pairs) {
2+
const lookup = {};
3+
for (const [countryCode, currencyCode] of pairs) {
4+
lookup[countryCode] = currencyCode;
5+
}
6+
return lookup;
37
}
48

5-
module.exports = createLookup;
9+
module.exports = createLookup;

‎Sprint-2/implement/lookup.test.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const createLookup = require("./lookup.js");
22

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
const countryCurrencyPairs = [["US", "USD"], ["CA", "CAD"]];
5+
const result = createLookup(countryCurrencyPairs);
6+
expect(result).toEqual({ US: "USD", CA: "CAD" });
7+
});
58
/*
69
710
Create a lookup object of key value pairs from an array of code pairs

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Unfortunately, it contains several bugs!
33
// Below are some test cases the implementation doesn't handle well.
44
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.
5+
56
const parseQueryString = require("./querystring");
67

78
test("should parse values containing '='", () => {
@@ -29,6 +30,7 @@ test("should decode percent-encoded characters", () => {
2930
$half: "1/2",
3031
});
3132
});
33+
3234
test("should replace '+' by ' '", () => {
3335
expect(parseQueryString("full+name=John+Doe")).toEqual({
3436
"full name": "John Doe",
@@ -41,6 +43,7 @@ test("should return {} for an empty string", () => {
4143

4244
// Stretch exercise: Handling query strings that contain identical keys
4345

46+
// Delete this test if you are not working on this optional case
4447
test("should store values of a key in an array when the key has 2 or more values", () => {
4548
expect(
4649
parseQueryString("key=value1&key=value2&key=value3&foo=bar")

‎Sprint-2/implement/tally.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
function tally() {}
1+
function tally(arr) {
2+
const counts = {};
3+
for (const item of arr) {
4+
counts[item] = (counts[item] || 0) + 1;
5+
}
6+
return counts;
7+
}
28

3-
module.exports = tally;
9+
module.exports = tally;

‎Sprint-2/implement/tally.test.js‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,35 @@ const tally = require("./tally.js");
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
2222

23+
test("tally on an empty array returns an empty object", () => {
24+
expect(tally([])).toEqual({});
25+
});
26+
2327
// Given an empty array
2428
// When passed to tally
2529
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
30+
test("tally on an array with duplicate items returns counts for each unique item", () => {
31+
expect(tally(["a"])).toEqual({ a: 1 });
32+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
33+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
34+
});
2735

2836
// Given an array with duplicate items
2937
// When passed to tally
3038
// Then it should return counts for each unique item
3139

40+
test("tally on an array with duplicate items returns counts for each unique item", () => {
41+
expect(tally(["a"])).toEqual({ a: 1 });
42+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
43+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
44+
});
45+
46+
3247
// Given an invalid input like a string
3348
// When passed to tally
3449
// Then it should throw an error
50+
test("tally on an array with duplicate items returns counts for each unique item", () => {
51+
expect(tally(["a"])).toEqual({ a: 1 });
52+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
53+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
54+
});

0 commit comments

Comments
 (0)