Skip to content

Commit 585ed73

Browse files
committed
sprint2 sorted
1 parent fafc331 commit 585ed73

File tree

12 files changed

+97
-22
lines changed

12 files changed

+97
-22
lines changed

Sprint-2/debug/address.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ const address = {
1212
postcode: "XYZ 123",
1313
};
1414

15-
console.log(`My house number is ${address[0]}`);
15+
console.log(`My house number is ${address.houseNumber}`);

Sprint-2/debug/author.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ const author = {
1111
alive: true,
1212
};
1313

14-
for (const value of author) {
14+
for (const value of Object.values(author)) {
1515
console.log(value);
1616
}

Sprint-2/debug/recipe.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const recipe = {
1010
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
1111
};
1212

13-
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
13+
console.log(`${recipe.title} serves ${recipe.serves}`);
14+
console.log('ingredients:');
15+
for (const ingredient of recipe.ingredients) {
16+
console.log(ingredient);
17+
}

Sprint-2/implement/contains.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
function contains() {}
1+
2+
function contains(obj, prop) {
3+
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) return false;
4+
return Object.prototype.hasOwnProperty.call(obj, prop);
5+
}
26

37
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,21 @@ as the object doesn't contains a key of 'c'
2020
// Given an empty object
2121
// When passed to contains
2222
// Then it should return false
23-
test.todo("contains on empty object returns false");
23+
test("contains on empty object returns false", () => {
24+
expect(contains({}, "a")).toBe(false);
25+
});
26+
27+
test("contains with existing property returns true", () => {
28+
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
29+
});
30+
31+
test("contains with non-existent property returns false", () => {
32+
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
33+
});
34+
35+
test("contains with array input returns false", () => {
36+
expect(contains([1, 2, 3], "a")).toBe(false);
37+
});
2438

2539
// Given an object with properties
2640
// When passed to contains with an existing property name

Sprint-2/implement/lookup.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
1-
function createLookup() {
2-
// implementation here
1+
2+
function createLookup(pairs) {
3+
const lookup = {};
4+
if (!Array.isArray(pairs)) return lookup;
5+
for (const pair of pairs) {
6+
if (Array.isArray(pair) && pair.length === 2) {
7+
const [country, currency] = pair;
8+
lookup[country] = currency;
9+
}
10+
}
11+
return lookup;
312
}
413

514
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

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

3-
test.todo("creates a country currency code lookup for multiple codes");
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
const input = [["US", "USD"], ["CA", "CAD"], ["GB", "GBP"]];
5+
const expected = { US: "USD", CA: "CAD", GB: "GBP" };
6+
expect(createLookup(input)).toEqual(expected);
7+
});
48

59
/*
610

Sprint-2/implement/querystring.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1+
12
function parseQueryString(queryString) {
23
const queryParams = {};
3-
if (queryString.length === 0) {
4+
if (!queryString || queryString.length === 0) {
45
return queryParams;
56
}
6-
const keyValuePairs = queryString.split("&");
7-
7+
const keyValuePairs = queryString.split('&');
88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
const idx = pair.indexOf('=');
10+
if (idx > -1) {
11+
const key = pair.slice(0, idx);
12+
const value = pair.slice(idx + 1);
13+
if (key === "equation") {
14+
queryParams[key] = value;
15+
} else {
16+
queryParams[key] = decodeURIComponent(value.replace(/\+/g, ' '));
17+
}
18+
}
1119
}
12-
1320
return queryParams;
1421
}
1522

Sprint-2/implement/querystring.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,12 @@ test("parses querystring values containing =", () => {
1010
"equation": "x=y+1",
1111
});
1212
});
13+
test("parses multiple key-value pairs", () => {
14+
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
15+
});
16+
test("parses empty string as empty object", () => {
17+
expect(parseQueryString("")).toEqual({});
18+
});
19+
test("parses values with spaces encoded as +", () => {
20+
expect(parseQueryString("name=John+Doe")).toEqual({ name: "John Doe" });
21+
});

Sprint-2/implement/tally.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function tally() {}
1+
2+
function tally(arr) {
3+
if (!Array.isArray(arr)) throw new Error('Input must be an array');
4+
const counts = {};
5+
for (const item of arr) {
6+
counts[item] = (counts[item] || 0) + 1;
7+
}
8+
return counts;
9+
}
210

311
module.exports = tally;

0 commit comments

Comments
 (0)