The Lodash project, unable to get '_invert' method to work correctly

Hi everyone,
I have tried to use the code from the solution and I am still getting an error -after enough time spent on “stare and compare” i now need someone to point out the basic error I have made before my head explodes!

Link to the Lodash project - I am trying to complete the _invert method exercise:
https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/projects/lodash

Here is a screen shot of my code and the error:

here is the code so you can copy and paste to test if needed:

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

here is the error message ( number 4 fails) if the screenshot isnt clear:

$ node test/invert.js
_.invert() Tests:
1 - _.invert() is defined - Passed!
2 - Returns an object with key and value inverted for a single key-value pair - Passed!
3 - Original key is not present after the key-value pairs have been inverted - Passed!
4 - Returns an object with all keys and values inverted - Failed: _.invert({originalKey: “originalValue”})[“originalKey”]) returned undefined instead of anotherKey.

please help!
Jay

I made a very small tweak to your code so I could run it outside the lesson:

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

console.log(invert({'a': 'b', 'c': 'd'}))

the expected output:

{'b': 'a', 'd': 'c'}

this is clearly not what we are getting:

{
  originalValue: "c"
}

the problem seems to be here:

invertedObject = {originalValue : key}

you assign an object to invertedObject variable, instead of adding a key value pair to the invertedObject

1 Like

thank you - the code I have shown is the code from the solution video which passes all tests!!!
i have managed to find a solution:

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

Now I need ot try and understand how it works but I hope this is useful to anyone else who finds that the solution provided in the course video does not actually pass the test.

thanks
Jay

Same error had me stuck, thanks for posting the solution!