For in loop - error message

I’ve looked at the project walkthrough video and my code is exactly the same. Yet, I’m getting an error message returning for the following code:

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

};
// Do not write or modify code below this line.
module.exports = _;

The following error message is returning:

4 - Returns an object with all keys and values inverted - Failed: _.invert({originalKey: “originalValue”})[“originalKey”]) returned undefined instead of anotherKey.

    invertedObject = {originalValue : key};

In this line, you are Reassign the whole invertedObject to a new object so in the last loop, your invertedObject will only be an object with the last key, value.

you suppose to add a new (key, value), not reassigning the whole object.
so just change this line to:

invertedObject[originalValue] = key;
2 Likes