Skip to content

Commit 559b018

Browse files
committed
sprint 2 coursework
1 parent de6889c commit 559b018

12 files changed

Lines changed: 204 additions & 23 deletions

File tree

‎Sprint-2/debug/address.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
// Predict and explain first...
2+
/*
3+
Objects don't use indexes like arrays. If we try to access address[0], it returns undefined because address
4+
is an object, not an array. Objects store values using keys (property names). To retrieve a value, we can use
5+
either dot notation, such as address.houseNumber, or bracket notation, such as address["houseNumber"].
26
7+
*/
38
// This code should log out the houseNumber from the address object
49
// but it isn't working...
510
// Fix anything that isn't working
@@ -12,4 +17,4 @@ const address = {
1217
postcode: "XYZ 123",
1318
};
1419

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

‎Sprint-2/debug/author.js‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// Predict and explain first...
2-
2+
/*
3+
author is an object. The original code tries to use a for...of loop directly on the object, but normal objects
4+
cannot be directly iterated using for...of. Since we only want the property values, we can use Object.values(author)
5+
to get all the values from the object.
6+
*/
37
// This program attempts to log out all the property values in the object.
48
// But it isn't working. Explain why first and then fix the problem
59

10+
11+
612
const author = {
713
firstName: "Zadie",
814
lastName: "Smith",
915
occupation: "writer",
1016
age: 40,
1117
alive: true,
1218
};
19+
console.log(Object.values(author));
1320

14-
for (const value of author) {
15-
console.log(value);
16-
}

‎Sprint-2/debug/recipe.js‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
// Predict and explain first...
2-
2+
/*
3+
The printing of title and serves is absolutely correct, but to print ingredients on a new line isn't correct.
4+
To retrieve values, we use Object.values(recipe). To print ${recipe} isn't the right way. We can use a for...of
5+
loop to print each ingredient on a new line.
6+
*/
37
// This program should log out the title, how many it serves and the ingredients.
48
// Each ingredient should be logged on a new line
59
// How can you fix it?
@@ -10,6 +14,7 @@ const recipe = {
1014
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
1115
};
1216

13-
console.log(`${recipe.title} serves ${recipe.serves}
14-
ingredients:
15-
${recipe}`);
17+
console.log(`${recipe.title} serves ${recipe.serves}`);
18+
for(let values of Object.values(recipe.ingredients)){
19+
console.log(values);
20+
}

‎Sprint-2/implement/contains.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
function contains() {}
1+
function contains(obj, item) {
2+
if (typeof obj !== "object" || obj === null || Array.isArray(obj)){
3+
return false;
4+
}
5+
let getKey = Object.keys(obj);
6+
for (let element of getKey){
7+
if(element === item){
8+
return true
9+
}
10+
}
11+
return false;
12+
}
213

314
module.exports = contains;

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,37 @@ E.g. contains({a: 1, b: 2}, 'c') // returns false
1111
as the object doesn't contains a key of 'c'
1212
*/
1313

14-
// Acceptance criteria:
15-
1614
// Given a contains function
1715
// When passed an object and a property name
1816
// Then it should return true if the object contains the property, false otherwise
17+
test("return true if object's property exists otherwise false", () => {
18+
expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true);
19+
});
1920

2021
// Given an empty object
2122
// When passed to contains
2223
// Then it should return false
23-
test.todo("contains on empty object returns false");
24+
test("return false if object is empty", () => {
25+
expect(contains({}, "a")).toBe(false);
26+
});
2427

2528
// Given an object with properties
2629
// When passed to contains with an existing property name
2730
// Then it should return true
31+
test("return true if object's property contains the property", () => {
32+
expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true);
33+
});
2834

2935
// Given an object with properties
3036
// When passed to contains with a non-existent property name
3137
// Then it should return false
38+
test("return false if property is non-existent", () => {
39+
expect(contains({name: "maryam", city: "Derby"}, "age")).toBe(false);
40+
});
3241

3342
// Given invalid parameters like an array
3443
// When passed to contains
3544
// Then it should return false or throw an error
45+
test("return false if it's not an object", () => {
46+
expect(contains([], 6)).toBe(false);
47+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
function createLookup() {
1+
function createLookup(arr) {
22
// implementation here
3+
const obj = Object.fromEntries(arr);
4+
return obj;
35
}
46

57
module.exports = createLookup;

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
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+
expect(createLookup([['US', 'USD'], ['CA', 'CAD'], ['PAK', 'PKR'], ['SAUDI', 'RIYAL']])).toEqual({
5+
'US': 'USD',
6+
'CA': 'CAD',
7+
'PAK' : 'PKR',
8+
'SAUDI' : 'RIYAL'
9+
});
10+
});
11+
test("the array is empty", () =>{
12+
expect(createLookup([])).toEqual({});
13+
});
14+
test("creates a country currency code lookup for single code", () =>{
15+
expect(createLookup([['Uk','POUND']])).toEqual({'Uk': 'POUND'});
16+
});
417

518
/*
619

‎Sprint-2/implement/querystring.js‎

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,48 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
34
if (queryString.length === 0) {
45
return queryParams;
56
}
7+
68
const keyValuePairs = queryString.split("&");
79

810
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
11+
if (pair === "") {
12+
continue;
13+
}
14+
15+
const checkSeparator = pair.indexOf("=");
16+
17+
let key;
18+
let value;
19+
20+
if (checkSeparator === -1) {
21+
key = pair;
22+
value = "";
23+
} else {
24+
key = pair.slice(0, checkSeparator);
25+
value = pair.slice(checkSeparator + 1);
26+
}
27+
28+
key = key.replace(/\+/g, " ");
29+
value = value.replace(/\+/g, " ");
30+
31+
key = decodeURIComponent(key);
32+
value = decodeURIComponent(value);
33+
34+
if (Object.prototype.hasOwnProperty.call(queryParams, key)) {
35+
if (Array.isArray(queryParams[key])) {
36+
queryParams[key].push(value);
37+
} else {
38+
queryParams[key] = [queryParams[key], value];
39+
}
40+
} else {
41+
queryParams[key] = value;
42+
}
1143
}
1244

1345
return queryParams;
1446
}
1547

16-
module.exports = parseQueryString;
48+
module.exports = parseQueryString;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,33 @@ test("should store values of a key in an array when the key has 2 or more values
4646
foo: "bar",
4747
});
4848
});
49+
// Multiple normal key-value pairs
50+
test("should parse multiple key-value pairs", () => {
51+
expect(parseQueryString("name=Maryam&age=25")).toEqual({
52+
name: "Maryam",
53+
age: "25",
54+
});
55+
});
56+
57+
test("should return empty object for empty query string", () => {
58+
expect(parseQueryString("")).toEqual({});
59+
});
60+
61+
test("should replace multiple '+' characters with spaces", () => {
62+
expect(parseQueryString("name=John+Michael+Doe")).toEqual({
63+
name: "John Michael Doe",
64+
});
65+
});
66+
67+
test("should decode encoded keys and values", () => {
68+
expect(parseQueryString("hello%20world=good%20morning")).toEqual({
69+
"hello world": "good morning",
70+
});
71+
});
72+
73+
test("should ignore multiple empty key-value pairs", () => {
74+
expect(parseQueryString("name=Maryam&&&age=25&&&")).toEqual({
75+
name: "Maryam",
76+
age: "25",
77+
});
78+
});

‎Sprint-2/implement/tally.js‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
function tally() {}
2-
1+
function tally(arr) {
2+
const result = arr.reduce((obj, item) => {
3+
if (obj [item]) {
4+
obj[item] = obj[item] + 1;
5+
}
6+
else{
7+
obj[item] = 1;
8+
}
9+
return obj;
10+
}, {});
11+
12+
return result;
13+
}
314
module.exports = tally;

0 commit comments

Comments
 (0)