Skip to content

Commit 99e811b

Browse files
committed
Fix revert files for Sprint 2
1 parent 8e5fb57 commit 99e811b

6 files changed

Lines changed: 10 additions & 114 deletions

File tree

‎Sprint-2/debug/address.js‎

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,6 @@
44
// but it isn't working...
55
// Fix anything that isn't working
66

7-
/*const address = {
8-
houseNumber: 42,
9-
street: "Imaginary Road",
10-
city: "Manchester",
11-
country: "England",
12-
postcode: "XYZ 123",
13-
};
14-
15-
console.log(`My house number is ${address[0]}`); */
16-
17-
// The line "address[0]" is wrong. Because address is an object not an array, so "address[0]" is undefined. The output will be "My house number is undefined".
18-
19-
// Corrected code
207
const address = {
218
houseNumber: 42,
229
street: "Imaginary Road",
@@ -25,4 +12,4 @@ const address = {
2512
postcode: "XYZ 123",
2613
};
2714

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

‎Sprint-2/debug/author.js‎

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// This program attempts to log out all the property values in the object.
44
// But it isn't working. Explain why first and then fix the problem
55

6-
/* const author = {
6+
const author = {
77
firstName: "Zadie",
88
lastName: "Smith",
99
occupation: "writer",
@@ -14,21 +14,3 @@
1414
for (const value of author) {
1515
console.log(value);
1616
}
17-
*/
18-
19-
// Prediction and Explanation
20-
/* The line "for (const value of author) {console.log(value);}" will not work because for-of is used only for iterable things, e.g: arrays, strings, maps, sets.
21-
But author is a plain object (created with {}), and plain objects are not iterable. So Javascript throws: TypeError: author is not iterable because objects don't have a natural order to loop through. */
22-
23-
// Corrected code
24-
const author = {
25-
firstName: "Zadie",
26-
lastName: "Smith",
27-
occupation: "writer",
28-
age: 40,
29-
alive: true,
30-
};
31-
32-
for (const value of Object.values(author)) {
33-
console.log(value);
34-
}

‎Sprint-2/debug/recipe.js‎

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// Each ingredient should be logged on a new line
55
// How can you fix it?
66

7-
/*const recipe = {
7+
const recipe = {
88
title: "bruschetta",
99
serves: 2,
1010
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
@@ -13,26 +13,3 @@
1313
console.log(`${recipe.title} serves ${recipe.serves}
1414
ingredients:
1515
${recipe}`);
16-
*/
17-
18-
// Prediction and Explanation
19-
/*
20-
The line inside the template string: ${recipe} does not print the ingredients.
21-
Instead, JavaScript converts the entire recipe object into a string, which becomes "[object Object]". This happens
22-
because plain objects {} are not automatically converted into readable text. The program should print each ingredient on a new line, but the current code
23-
never loops through recipe.ingredients, so nothing is printed correctly.
24-
*/
25-
26-
// Corrected code
27-
const recipe = {
28-
title: "bruschetta",
29-
serves: 2,
30-
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
31-
};
32-
33-
console.log(`${recipe.title} serves ${recipe.serves}
34-
ingredients:`);
35-
36-
for (const ingredient of recipe.ingredients) {
37-
console.log(ingredient);
38-
}

‎Sprint-2/implement/contains.js‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
function contains(obj, prop) {
2-
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
3-
return false;
4-
}
5-
6-
return obj.hasOwnProperty(prop);
7-
}
1+
function contains() {}
82

93
module.exports = contains;
10-

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

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,20 @@ as the object doesn't contains a key of 'c'
1616
// Given a contains function
1717
// When passed an object and a property name
1818
// Then it should return true if the object contains the property, false otherwise
19-
test("returns true when object contains the property", () => {
20-
const obj = { a: 1, b: 2 };
21-
expect(contains(obj, "a")).toBe(true);
22-
});
23-
24-
test("returns false when object does not contain the property", () => {
25-
const obj = { a: 1, b: 2 };
26-
expect(contains(obj, "c")).toBe(false);
27-
});
28-
2919

3020
// Given an empty object
3121
// When passed to contains
3222
// Then it should return false
33-
// test.todo("contains on empty object returns false");
34-
test("contains on empty object returns false", () => {
35-
expect(contains({}, "a")).toBe(false);
36-
});
23+
test.todo("contains on empty object returns false");
3724

3825
// Given an object with properties
3926
// When passed to contains with an existing property name
4027
// Then it should return true
41-
test("returns true when object contains the property", () => {
42-
const obj = { a: 1, b: 2 };
43-
expect(contains(obj, "a")).toBe(true);
44-
});
4528

4629
// Given an object with properties
4730
// When passed to contains with a non-existent property name
4831
// Then it should return false
49-
test("returns false when object does not contain the property", () => {
50-
const obj = { a: 1, b: 2 };
51-
expect(contains(obj, "c")).toBe(false);
52-
});
5332

5433
// Given invalid parameters like an array
5534
// When passed to contains
5635
// Then it should return false or throw an error
57-
test("returns false for invalid parameters like an array", () => {
58-
expect(contains([], "a")).toBe(false);
59-
expect(contains(null, "a")).toBe(false);
60-
expect(contains(123, "a")).toBe(false);
61-
expect(contains("hello", "a")).toBe(false);
62-
});

‎Sprint-2/interpret/invert.js‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Then it should swap the keys and values in the object
66

77
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8-
/*
8+
99
function invert(obj) {
1010
const invertedObj = {};
1111

@@ -15,31 +15,15 @@ function invert(obj) {
1515

1616
return invertedObj;
1717
}
18-
*/
1918

20-
// a) What is the current return value when invert is called with { a : 1 } - The current return value is { key: 1}
19+
// a) What is the current return value when invert is called with { a : 1 }
2120

22-
// b) What is the current return value when invert is called with { a: 1, b: 2 } - The current return value is { key: 2}
21+
// b) What is the current return value when invert is called with { a: 1, b: 2 }
2322

24-
// c) What is the target return value when invert is called with {a : 1, b: 2} - The target return value is { "1": "a", "2": "b" }
23+
// c) What is the target return value when invert is called with {a : 1, b: 2}
2524

2625
// c) What does Object.entries return? Why is it needed in this program?
27-
/* Object.entries(obj) returns an array of [key, value] pairs. Example: Object.entries({ a:1, b:2 }) returns [["a", 1], ["b", 2]].
28-
It is needed because the loop "for (const [key, value] of Object.entries(obj))" let's each pair destructure easily.
29-
*/
3026

31-
// d) Explain why the current return value is different from the target output - It is because this line "invertedObj.key = value;" uses literal string "key instead of the variable key" which is a wrong property name,
32-
// instead of this "invertedObj[value] = key;"
27+
// d) Explain why the current return value is different from the target output
3328

3429
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
35-
function invert(obj) {
36-
const invertedObj = {};
37-
38-
for (const [key, value] of Object.entries(obj)) {
39-
invertedObj[value] = key;
40-
}
41-
42-
return invertedObj;
43-
}
44-
45-
module.exports = invert;

0 commit comments

Comments
 (0)