Recreat lodash invert()

i was Implementing invert() the way it was guided
invert(object){

let invertedObject = {};

for(let key in object){

  const originalValue = object[key];

  invertedObject = {originalValue: key}

}

return invertedObject

}

there was a failure at end of it for invertedObject = {originalValue: key}; , i checked guidance video and it was exactly the same but hers passed anyways i found the right way to do it and it passed i just want to know why mine didn’t pass because i don’t wanna just find an answer and move on.

Hello,

I just went back and reviewed the video and I see what you mean. The biggest problem with your code (and the code in the video) is that it’s only good at inverting the object if there is only one key.

Notice in your code, you’re replacing the entire object you plan on returning each iteration.

invertedObject = {originalValue: key}

This means that if the object you’re inverting has more than one key, only the last one will remain in the object since the object storing the new pairs gets replaced.

My guess is that at the time of the video, their tests were a little more limited and they were only testing with an object with a single key:value pair.

You mentioned that you’ve already found a solution that passes, so I’ll show how to fix the one you posted:

invertedObject[originalValue] = key;

This allows each iteration to store the inverted key:value pair without replacing the entire object.

Thank you very much :slightly_smiling_face: