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
56 lines (37 loc) · 1.74 KB
/
invert.js
File metadata and controls
56 lines (37 loc) · 1.74 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
54
55
56
// 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 = {};
for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
}
return invertedObj;
}
// a) What is the current return value when invert is called with { a : 1 }
console.log(invert({ a: 1 }));
// The current return value is { key: 1 }
// b) What is the current return value when invert is called with { a: 1, b: 2 }
console.log(invert({ a: 1, b: 2 }));
// The current return value is { key: 2 }
// 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?
// Object.entries returns an array of a key, value pairs, and also an object to array of arrays
// It is needed in this program to access and iterate over the key value pairs of the object,allowing us to process both the property names and their corresponding values.
// d) Explain why the current return value is different from the target output
/*The current return value is different from the target output
because in line 13 the invertedObj.key is assigning the string "key" as a property name, Not the actual value of the key variable.*/
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
function invert(obj) {
const invertedObj = {};
for (const [key, value] of Object.entries(obj)) {
invertedObj[value] = key;
}
return invertedObj;
}
// Tests
console.log(invert({ a: 1 }));
console.log(invert({ a: 1, b: 2 }));