[REPORTED] Lodash Library .invert()


I am currently doing an exercise where i am supposed to create a function that inverts the key value pairs of an object. Before viewing the solution offered, i went online and tried to create the function myself and the below is what i got.

function invert(obj) {
let newObj = {};
for (let prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    newObj[obj[prop]] = prop;
return newObj;


The above function passed the exercise. But below is the solution gotten from the get help video:

function invert(object) {
  let invertedObject = {};
  for (let key in object) {
        let originalValue = object[key];
        invertedObject = {originalValue : key}
  return invertedObject;

It obviously also passed the exercise.

However when i test both functions, mine returns an object of inverted key value pairs but the get help solution only return an object that inverts the last key value pair in the object…
Infact, using the below as an example, the function returned an array of {originalValue: ‘laptop’}
const test = {
name: ‘valerie’,
weight: 85,
height: ‘tall’,
colour: ‘grey’,
laptop: ‘dell’,
i guess i am wondering why that is.

1 Like

Hi there.

Thanks for reporting this.

The solution code in the Get Help video is… wrong. It won’t correctly invert the object passed in, as you’ve found. It only passes the minimal tests because the test suite is expecting the particular key-value pair that the video shows.

I’ll flag this up as a bug. Thanks again for raising this. :slight_smile:

Edit: Logged the bug with Codecademy. :slight_smile:


Seems an odd thing to be checking if an object has a property when we are iterating the properties of that object.

I thought the same thing but the explanation was that we have to check if the properties were defined in the object and not inherited.

In other words, if our object looks like this:

{ 1: undefined, 2: undefined, 3: undefined, 4: undefined }

Then we shouldn’t invert it?

So my question then is where does it tell us to look for this edge case?

I don’t see that in the instructions?

No, i meant for the article i read online that helped come up with my own solution.

Tbh, i can’t say. But is there any real life scenario in which one would need to create an object with all undefined values and then invert it?

Not that I am aware of, but nothing surprises me. It is not uncommon to create a data structure that is not populated with data until the run session, but that is usually arrays, not objects.

Ah, ok, that would make sense why I couldn’t see it. :slight_smile:

We know that objects cannot have duplicate keys so the process of inverting could be a way of eliminating duplicate values. Invert, then invert back. Voila, no duplicate data values.

 > const invert = function (x) {
       y = {}
       for (let key in x) {
           y[x[key]] = key;
       return y;
<- ƒ (x) {
       y = {}
       for (let key in x) {
           y[x[key]] = key;
       return y;
 > a = { 1: undefined, 2: undefined, 3: undefined, 4: undefined }
<- {1: undefined, 2: undefined, 3: undefined, 4: undefined}
 > b = invert(a)
<- {undefined: "4"}
 > a = invert(b)
<- {4: "undefined"}

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.