JavaScript
Write a function named "reverse_kvs" that has a key-value store as a parameter and returns a new key-value store. Your function should add each key-value pair in the parameter to the new key-value store EXCEPT reversing the key and value. For example, if the key-value "extreme":45 were in the parameter then the returned key-value store should contain the key-value pair 45:"extreme".
The following code doesn't work:
function reverse_kvs(d) { var new_d = {}; for (var k in d) { new_d[d[k]] = k; } return new_d; }
Error message:
returned: {}
expected: {33: 'particle', 75: 'fear', -36: 'champion'}
const d = {"particle":33, "fear":75, "champion":-33}
function reverse_kvs(d)
{
const keys =
Object.keys(d);
const new_d =
{};
for (const key
of keys) {
new_d[d[key]] = key;
}
return
new_d;
}
console.log(reverse_kvs(d));
If you have any doubts please comment and please don't dislike.
Get Answers For Free
Most questions answered within 1 hours.