Lodash project - invert()

hello,

i’m working on the invert() method of the Lodash project.

i don’t understand why this code is not accepted when i run the test file.
especially because i copy this code from the step by step solution video.

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

thank for your help

Hello,

Unfortunately, the code for the invert() method in the solution video for this project is incorrect for a couple reasons, and the test has been updated since the production of that video.

For reference: Lodash invert(object) documentation
" Creates an object composed of the inverted keys and values of object . If object contains duplicate values, subsequent values overwrite property assignments of previous values."

Example input / output from documentation:

var object = { 'a': 1, 'b': 2, 'c': 1 };
 
_.invert(object);
// => { '1': 'c', '2': 'b' }

Solution Video Code Breakdown
If we run the same example input object through the method provided in the solution video, the output would be:

{ originalValue: 'c' }

That illustrates both issues with the solution code:

  1. originalValue” was part of the method’s source code, it wasn’t a key or a value of the object passed in, and shouldn’t be part of the output object.

This is caused by this line: invertedObject = {originalValue: key} not having square brackets around originalValue to tell JavaScript to evaluate it rather than use that as the key name.

If the line had been invertedObject = {[originalValue]: key}, then the output would have been: { '1': 'c' }. Still incorrect because it only includes 1 key/value pair instead of the 2 we were expecting, but closer.

  1. Even when the issue outlined in the first point is fixed, the output would still be limited to only one key/value pair no matter how many different key/value pairs were in the object passed to it. The problem is caused by the same line of code: invertedObject = {originalValue: key}. This is replacing the value of invertedObject with every iteration of the for loop. Since this is the object being used to store the output, it will only ever have the key/value stored from the last iteration.

Essentially, it forgets every key/value that came before the last iteration because of the replacement. The function would only produce the desired output if the input object has only one key/value pair. This can be fixed by changing the line to:

invertedObject[originalValue] = key;

With this change, each iteration of the for loop will build upon the object rather than replace it entirely. The output would be:

{ '1': 'c', '2': 'b' }

In the end, that one line of code in the solution video was the only thing that needed to be changed, for more than one reason.

Final version for completeness:

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

thanks a lot for the explanation.

Brieuc