forked from CodeYourFuture/Module-Data-Groups
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvert.js
More file actions
53 lines (40 loc) · 1.88 KB
/
invert.js
File metadata and controls
53 lines (40 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Let's define how invert should work
// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
function invert(obj) {
const invertedObj = {};
if (Object.prototype.toString.call(obj) === "[object Object]") {
for (const [key, value] of Object.entries(obj)) {
if (
value === null ||
Array.isArray(value) ||
(typeof value === "object" && !Array.isArray(value))
) {
throw new Error(
"error invalid input entered, expecting an object to have only strings as values"
);
}
invertedObj[value] = key;
}
} else {
throw new Error("error invalid input entered, expecting an object");
}
return invertedObj;
}
module.exports = invert;
// a) What is the current return value when invert is called with { a : 1 }
//it returns a string describing the object.
// b) What is the current return value when invert is called with { a: 1, b: 2 }
//it aso returns a string describing the object.
// 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"}.
// c) What does Object.entries return? Why is it needed in this program?
//it returns an array made up of arrays of the original objects key - value pairs.
//it allows us to unpack the contents of the object into an array which can then be used to create the new object
// d) Explain why the current return value is different from the target output
//because we used dot notation to assign a value to our key, this creates a property called key
//and assigns it a value. we want our key to get its name from a variable so we need to use bracket notation.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
//we can fix it by using invertedObj[value] = key.