Skip to content

Commit e17bf30

Browse files
committed
fixed invert function,answered questions and added tests to check it works as expected
1 parent b63c6de commit e17bf30

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

Sprint-2/interpret/invert.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,54 @@
99
function invert(obj) {
1010
const invertedObj = {};
1111

12-
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
12+
if (Object.prototype.toString.call(obj) === "[object Object]") {
13+
for (const [key, value] of Object.entries(obj)) {
14+
invertedObj[value] = key;
15+
}
16+
} else {
17+
throw new Error("error invalid input entered, expecting an object");
1418
}
1519

1620
return invertedObj;
1721
}
1822

23+
module.exports = invert;
24+
25+
console.log(
26+
`the return value when invert is called with ${invert({
27+
cars: { toyota: 2, bmw: 1, benz: 4 },
28+
})}`
29+
);
30+
31+
/*
32+
for (const property in obj) {
33+
if (typeof obj[property] !== "string") {
34+
throw new Error(
35+
"error invalid input entered, expecting an object to have only strings as values"
36+
);
37+
} else {
38+
39+
}
40+
}
41+
42+
*/
43+
1944
// a) What is the current return value when invert is called with { a : 1 }
45+
//it returns a string describing the object.
2046

2147
// b) What is the current return value when invert is called with { a: 1, b: 2 }
48+
//it aso returns a string describing the object.
2249

2350
// c) What is the target return value when invert is called with {a : 1, b: 2}
51+
//the target return value is {"1": "a", "2": "b"}.
2452

2553
// c) What does Object.entries return? Why is it needed in this program?
54+
//it returns an array made up of arrays of the original objects key - value pairs.
55+
//it allows us to unpack the contents of the object into an array which can then be used to create the new object
2656

2757
// d) Explain why the current return value is different from the target output
58+
//because we used dot notation to assign a value to our key, this creates a property called key
59+
//and assigns it a value. we want our key to get its name from a variable so we need to use bracket notation.
2860

2961
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
62+
//we can fix it by using invertedObj[value] = key.

Sprint-2/interpret/invert.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const invert = require("./invert.js");
2+
3+
describe("tests to see if invert function swaps around the keys and values in a given object", () => {
4+
test("if invert is passed a value which is not an object, it should throw an error", () => {
5+
expect(() =>
6+
invert([
7+
["x", 10],
8+
["y", 20],
9+
])
10+
).toThrow("error invalid input entered, expecting an object");
11+
});
12+
13+
test("if we are passed an empty object we should return an empty object", () => {
14+
expect(invert({})).toEqual({});
15+
});
16+
17+
test("given an object with key value pairs, it should swap the keys and values in the object", () => {
18+
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
19+
expect(
20+
invert({ city: "Birmingham", population: "345908", boroughs: "20" })
21+
).toEqual({ Birmingham: "city", 345908: "population", 20: "boroughs" });
22+
});
23+
});
24+
25+
/*
26+
test("if invert is passed an object which has an array or object as one of its values, it should throw an error", () => {
27+
expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow(
28+
"error invalid input entered, expecting an object to have only strings as values"
29+
);
30+
});
31+
*/

0 commit comments

Comments
 (0)