Skip to content

Commit 7385171

Browse files
committed
sprint2 implement tasks are done
1 parent de6889c commit 7385171

8 files changed

Lines changed: 110 additions & 83 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1-
function contains() {}
1+
function contains(keyInput,valueInput) {
2+
if (Array.isArray(keyInput) == true) {
3+
return false;
4+
} else {
5+
for (const key in keyInput) {
6+
if (key == valueInput) {
7+
return true;
8+
}
9+
}
10+
}
11+
return false;
12+
}
13+
14+
}
215

316
module.exports = contains;
Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
const contains = require("./contains.js");
22

3-
/*
4-
Implement a function called contains that checks an object contains a
5-
particular property
3+
test("Give a contains with object and property and if the object contains the property returns true", () => {
4+
expect(contains({ a: 1, b: 2 }, "b")).toEqual(true);
5+
});
66

7-
E.g. contains({a: 1, b: 2}, 'a') // returns true
8-
as the object contains a key of 'a'
7+
test("Give a contains with empty object and when passed to contains it returns false", () => {
8+
expect(contains({}, "y")).toEqual(false);
9+
});
910

10-
E.g. contains({a: 1, b: 2}, 'c') // returns false
11-
as the object doesn't contains a key of 'c'
12-
*/
11+
test("Give a contains with object and property and if the object contains the property returns true", () => {
12+
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
13+
});
1314

14-
// Acceptance criteria:
15+
test("Give a contains with object and property and if the property is non-existent returns false", () => {
16+
expect(contains({ a: 1, b: 2 }, "z")).toEqual(false);
17+
});
1518

16-
// Given a contains function
17-
// When passed an object and a property name
18-
// Then it should return true if the object contains the property, false otherwise
19-
20-
// Given an empty object
21-
// When passed to contains
22-
// Then it should return false
23-
test.todo("contains on empty object returns false");
24-
25-
// Given an object with properties
26-
// When passed to contains with an existing property name
27-
// Then it should return true
28-
29-
// Given an object with properties
30-
// When passed to contains with a non-existent property name
31-
// Then it should return false
32-
33-
// Given invalid parameters like an array
34-
// When passed to contains
35-
// Then it should return false or throw an error
19+
test("Give invalid parameter in this case array when passed to contains it returns false", () => {
20+
expect(contains(["a", 2, "b", 3, 2], 2)).toEqual(false);
21+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
1-
function createLookup() {
1+
function createLookup(countryCurrencyPairs) {
22
// implementation here
3+
pairs = {};
4+
for (const [country, currency] of countryCurrencyPairs) {
5+
pairs[country] = currency;
6+
}
7+
return pairs;
38
}
9+
console.log(
10+
createLookup([
11+
["UK", "GBP"],
12+
["ET", "ETB"],
13+
["CA", "CAD"],
14+
["FR", "EUR"],
15+
])
16+
);
417

518
module.exports = createLookup;

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

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

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
5-
/*
6-
7-
Create a lookup object of key value pairs from an array of code pairs
8-
9-
Acceptance Criteria:
10-
11-
Given
12-
- An array of arrays representing country code and currency code pairs
13-
e.g. [['US', 'USD'], ['CA', 'CAD']]
14-
15-
When
16-
- createLookup function is called with the country-currency array as an argument
17-
18-
Then
19-
- It should return an object where:
20-
- The keys are the country codes
21-
- The values are the corresponding currency codes
22-
23-
Example
24-
Given: [['US', 'USD'], ['CA', 'CAD']]
25-
26-
When
27-
createLookup(countryCurrencyPairs) is called
28-
29-
Then
30-
It should return:
31-
{
32-
'US': 'USD',
33-
'CA': 'CAD'
34-
}
35-
*/
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
expect([
5+
["US", "USD"],
6+
["CA", "CAD"],
7+
["UK", "GBP"],
8+
]).toEqual({ US: "USD", CA: "CAD", UK: "GBP" });
9+
});

‎Sprint-2/implement/querystring.js‎

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,53 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
if (!queryString) {
44
return queryParams;
55
}
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
// Skip completely empty segments from '&&' or trailing '&'
10+
if (!pair) continue;
11+
12+
// 2. Split on '=' separating key and value
13+
const eqIndex = pair.indexOf("=");
14+
let rawKey, rawValue;
15+
16+
if (eqIndex === -1) {
17+
// Key with no '=' (e.g. "key")
18+
rawKey = pair;
19+
rawValue = "";
20+
} else {
21+
rawKey = pair.slice(0, eqIndex);
22+
rawValue = pair.slice(eqIndex + 1);
23+
}
24+
25+
// 3. Replace '+' with ' ' first, then decode URL percent-encoded values
26+
const key = decodeURIComponent(rawKey.replace(/\+/g, " "));
27+
const value = decodeURIComponent(rawValue.replace(/\+/g, " "));
28+
29+
// 4. Handle identical keys (stretch goal)
30+
if (queryParams.hasOwnProperty(key)) {
31+
if (Array.isArray(queryParams[key])) {
32+
queryParams[key].push(value);
33+
} else {
34+
queryParams[key] = [queryParams[key], value];
35+
}
36+
} else {
37+
queryParams[key] = value;
38+
}
1139
}
1240

1341
return queryParams;
1442
}
1543

44+
console.log(parseQueryString("equation=a=b-2")); // "equation: a=b-2"
45+
console.log(parseQueryString("key1=value&&key2=value2&")); // key1:"value1" key2:"value2"
46+
console.log(parseQueryString("=value")); // "":value
47+
console.log(parseQueryString("key=")); // key:""
48+
console.log(parseQueryString("=")); // {"":""}
49+
console.log(parseQueryString("%24half=1%2F2")); //{$half: "1/2",}
50+
console.log(parseQueryString("full+name=John+Doe")); //{"full name": "John Doe",}
51+
console.log(parseQueryString());
52+
1653
module.exports = parseQueryString;

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

88
test("should parse values containing '='", () => {
99
expect(parseQueryString("equation=a=b-2")).toEqual({
@@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => {
3636
"full name": "John Doe",
3737
});
3838
});
39-
40-
// Stretch exercise: Handling query strings that contain identical keys
41-
42-
// Delete this test if you are not working on this optional case
43-
test("should store values of a key in an array when the key has 2 or more values", () => {
44-
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
45-
key: ["value1", "value2", "value3"],
46-
foo: "bar",
47-
});
48-
});

‎Sprint-2/implement/tally.js‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function tally() {}
2-
1+
function tally(items) {
2+
const result = {};
3+
for (const item of items) {
4+
result[item] = (result[item] || 0) + 1;
5+
}
6+
return result;
7+
}
8+
console.log(tally(["a", "b", "a", "c"]));
9+
console.log(tally([])); // {}
10+
console.log(tally("never mind"));
311
module.exports = tally;

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,22 @@ const tally = require("./tally.js");
1919
// Given a function called tally
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
22+
test("given an array of items returns an object containing the counter for each item", () => {
23+
expect(["a", "a"]).toEqual({ a: 2 });
24+
});
2225

2326
// Given an empty array
2427
// When passed to tally
2528
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
27-
29+
test("tally on an empty array returns an empty object", () => {
30+
expect([]).toEqual({});
31+
});
2832
// Given an array with duplicate items
2933
// When passed to tally
3034
// Then it should return counts for each unique item
31-
35+
test("given an array of duplicated items returns an object containing the counter for each item", () => {
36+
expect(["a", "a"]).toEqual({ a: 2 });
37+
});
3238
// Given an invalid input like a string
3339
// When passed to tally
3440
// Then it should throw an error

0 commit comments

Comments
 (0)