Skip to content

Commit 809dc28

Browse files
committed
sprint-2-data-groups-backlog. worked on fixing code and writing code based on tdd
1 parent 4222cda commit 809dc28

11 files changed

Lines changed: 169 additions & 20 deletions

File tree

Sprint-2/debug/address.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Predict and explain first...
2-
2+
//My prediction is that it will log undefined because we used array index to access object value instead of key name.
33
// This code should log out the houseNumber from the address object
44
// but it isn't working...
55
// Fix anything that isn't working
@@ -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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Predict and explain first...
2-
2+
/* In this code the problem is that the method for...of is used for array
3+
not for object type instead we use for...in this will work through the object
4+
and list the key and for value We use author[property] */
35
// This program attempts to log out all the property values in the object.
46
// But it isn't working. Explain why first and then fix the problem
57

@@ -10,7 +12,6 @@ const author = {
1012
age: 40,
1113
alive: true,
1214
};
13-
14-
for (const value of author) {
15-
console.log(value);
15+
for (const values in author) {
16+
console.log(`${author[values]}`);
1617
}

Sprint-2/debug/recipe.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Predict and explain first...
2-
2+
/* My prediction is that on the ingredients didn't used dot notation to access the element that why it will log undefined
3+
instead using for..of loop we can print all the ingredient in new line */
34
// This program should log out the title, how many it serves and the ingredients.
45
// Each ingredient should be logged on a new line
56
// How can you fix it?
@@ -11,5 +12,7 @@ const recipe = {
1112
};
1213

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

Sprint-2/implement/contains.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1-
function contains() {}
1+
function contains(givenObject, keyString) {
2+
console.log(givenObject, keyString);
3+
if (
4+
typeof givenObject !== "object" ||
5+
givenObject === null ||
6+
Array.isArray(givenObject)
7+
) {
8+
return false;
9+
}
10+
for (const keyProperty in givenObject) {
11+
console.log(keyString);
12+
if (keyProperty === keyString) {
13+
return true;
14+
}
15+
}
16+
return false;
17+
}
18+
console.log(contains({ a: 2, b: 1 }, "a"));
219

320
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,38 @@ 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("should return false when an empty object is passed to the function", () => {
24+
expect(contains({})).toEqual(false);
25+
});
2426

2527
// Given an object with properties
2628
// When passed to contains with an existing property name
2729
// Then it should return true
28-
30+
test("the function should return true when object contains a given property", () => {
31+
expect(contains({ a: 1, b: 2, c: 3 }, "c")).toEqual(true);
32+
expect(contains({ name: "lee", age: 30 }, "name")).toEqual(true);
33+
expect(contains({ place: "spain", city: "madrid", rank: 9 }, "city")).toEqual(
34+
true
35+
);
36+
expect(contains({ zone: 2, zone: 3, zone: 4 }, "zone")).toEqual(true);
37+
});
2938
// Given an object with properties
3039
// When passed to contains with a non-existent property name
3140
// Then it should return false
41+
test("the function should return false when property don't exist in the given object", () => {
42+
expect(contains({ a: 1, b: 2, c: 3 }, "d")).toEqual(false);
43+
expect(contains({ year: 1998, month: 10, Date: 16 }, "age")).toEqual(false);
44+
expect(
45+
contains({ meal: "pizza", drink: "water", table: 3 }, "starter")
46+
).toEqual(false);
47+
});
3248

3349
// Given invalid parameters like an array
3450
// When passed to contains
3551
// Then it should return false or throw an error
52+
test("the function should return false when invalid parameter like an array is passed", () => {
53+
expect(contains([10, 23, 34, 45], "3")).toEqual(false);
54+
expect(contains("hello", "0")).toEqual(false);
55+
expect(contains(["green", "yellow", "rad"], "0")).toEqual(false);
56+
expect(contains(((30, 40, 50), 30))).toEqual(false);
57+
});

Sprint-2/implement/lookup.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
function createLookup() {
1+
function createLookup(countryCurrencyPairs) {
22
// implementation here
3+
const currencyPairs = {};
4+
for (const innerPair of countryCurrencyPairs) {
5+
const [key, value] = innerPair;
6+
currencyPairs[key] = value;
7+
}
8+
return currencyPairs;
39
}
4-
510
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

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

3-
test.todo("creates a country currency code lookup for multiple codes");
3+
test("Function should create an object that have country code as key and country currency code as value", () => {
4+
expect(
5+
createLookup([
6+
["US", "USD"],
7+
["CA", "CAD"],
8+
])
9+
).toEqual({
10+
US: "USD",
11+
CA: "CAD",
12+
});
13+
expect(
14+
createLookup([
15+
["BE", "EUR"],
16+
["BR", "BRL"],
17+
["FI", "FIN"],
18+
])
19+
).toEqual({ BE: "EUR", BR: "BRL", FI: "FIN" });
20+
expect(
21+
createLookup([
22+
["CV", "CPV"],
23+
["CN", "CHN"],
24+
["CR", "CRI"],
25+
["DE", "DEU"],
26+
["IN", "IND"],
27+
])
28+
).toEqual({ CV: "CPV", CN: "CHN", CR: "CRI", DE: "DEU", IN: "IND" });
29+
});
430

531
/*
632

Sprint-2/implement/querystring.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,31 @@ function parseQueryString(queryString) {
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
if (pair.length === 0) {
10+
continue;
11+
} else {
12+
const equalityPosition = pair.indexOf("=");
13+
14+
if (equalityPosition === -1) {
15+
queryParams[pair] = "";
16+
} else {
17+
const key = decodeURIComponent(pair.slice(0, equalityPosition));
18+
const replacedKey = key.replace("+", " ");
19+
20+
const value = decodeURIComponent(pair.slice(equalityPosition + 1));
21+
const replacedValue = value.replace("+", " ");
22+
23+
if (queryParams[replacedKey]) {
24+
if (!Array.isArray(queryParams[replacedKey])) {
25+
queryParams[replacedKey] = [queryParams[replacedKey]];
26+
}
27+
28+
queryParams[replacedKey].push(replacedValue);
29+
} else {
30+
queryParams[replacedKey] = replacedValue;
31+
}
32+
}
33+
}
1134
}
1235

1336
return queryParams;

Sprint-2/implement/tally.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1-
function tally() {}
2-
1+
function tally(myArray) {
2+
if (!Array.isArray(myArray)) {
3+
throw "Error";
4+
} else if (myArray.length === 0) {
5+
return {};
6+
}
7+
let tallyObject = Object.create(null);
8+
for (const singleItems of myArray) {
9+
if (tallyObject[singleItems] === undefined) {
10+
tallyObject[singleItems] = 1;
11+
} else {
12+
tallyObject[singleItems] += 1;
13+
}
14+
}
15+
return tallyObject;
16+
}
317
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,34 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test("The function should return empty object when an empty array is passed to the function", () => {
27+
expect(tally([])).toEqual({});
28+
});
2729

2830
// Given an array with duplicate items
2931
// When passed to tally
3032
// Then it should return counts for each unique item
33+
test("The function should return an object where each key is a unique item from the array, and each index is how many times that item duplicate", () => {
34+
expect(tally(["a", "a", "a", "b", "b", "b", "c", "c", "c"])).toEqual({
35+
a: 3,
36+
b: 3,
37+
c: 3,
38+
});
39+
expect(
40+
tally(["manchester", "manchester", "london", "london", "leeds"])
41+
).toEqual({ manchester: 2, london: 2, leeds: 1 });
42+
expect(tally([1, 1, 2])).toEqual({ 1: 2, 2: 1 });
43+
expect(tally(["a"])).toEqual({ a: 1 });
44+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
45+
});
3146

3247
// Given an invalid input like a string
3348
// When passed to tally
3449
// Then it should throw an error
50+
test("The function should throw an error when invalid input is passed", () => {
51+
expect(() => {
52+
tally("hello");
53+
}).toThrow("Error");
54+
/*expect(tally(134)).toThrow;
55+
expect(tally({ a: 1, b: 3, d: 2 })).toThrow;*/
56+
});

0 commit comments

Comments
 (0)