Lodash - _.invert() method unexpected result


#1

Hello, recently i have been doing the Lodash project under web development. there is however 1 method that still confuses me and it is the ._invert() method. basically, the terminal claims that i have successfully passed all tests for the method (shown in the image below in bash) however, my output is shown as: { originalValue: 'd' } instead of what i though would be something more along the lines of: { 1: a'}. It appears as my originalValue variable does not display the object key and instead displays itself. I have a feeling that a fix for this is blatantly obvious, and i apologise if so, but is this output as expected, or am i doing something wrong? Any help is greatly appreciated, Thank you.

My code + output (visual studio code editor)


#2

Tests can only show that no bug was found, not that there is a lack of bugs.
Some tests are better than others. This one is extremely limited. It does for example not test if your result is === to the expected outcome, and its input only has one key-value pair. It only inspects two very specific things. The descriptions printed don’t match what it tests.

It’s not a display issue, no. You’re discarding the original value, and replacing it with that literal string.
There’s another bug too, which will become apparent if you test your function with an object containing more than one key-value pair.


#3

This is the problem I have run into. I can see that on each iteration is inverting the pairs through an internal console.log but when I log what should be the finished object I am only left with a single inverted key:value pair. How do I get it to add a new key:value pair to the new object on each iteration instead of overwriting the one and only pair?


#4

Found a partial fix? I now end up with a full object but the key:value pairs are in an unexpected order…

for (let key in obj) {
const originalValue = obj[key];
newObj[originalValue] = key;
console.log(newObj); //Added to show step by step inversions
}
return newObj;
}


#5

Not until recently did Python begin to index insertion order of items in a dictionary. If you are running this code in Python 3.5 or earlier, there is no order to how dictionaries are polled.


#6

I’m in Javascript. I guess the order of the key:value pairs doesn’t matter as they are accessed via the key VS a position in an array. Are there scenarios where it might matter?


#7

I have no idea how that post ended up here. It was a reply to an entirely different topic. Now I’m rather vexed as to whom I am replying.


#8

Interesting. Well, the random response lead me down a thought path to the info I needed so thanks none the less!