Lodash project

Hello everyone.
I was working with Lodash task and somewhere in the middle of the task, where I needed to define the invert method, I faced a problem. As you can see in the attached photo, I finished the .invert() method and when I execute it, it gives me two successful statements and one unsuccessful. The second and third statements are THE SAME and yet, one is indicating that my code is written properly and the other says its not.
It would be really good if someone can clarify this one for me.
Thanks in advance

@mathink3r - I ran into the same issue, very similar code. I read and re-read the Implement task (#25) and Step 6 in there is the issue.

  1. Still within the loop, set the value at originalValue on invertedObject to be the current key.

invertedObject inside the for … in loop is adding a key/value pair to the empty invertedObject outside the for … in loop. So poor step 6 wording aside, invertedObject inside the for … in loop should be:

invertedObject = {originalValue: obj};

if you go to the console doing

var object = { ‘a’: 1, ‘b’: 2, ‘c’: 1 };
_.invert(object);

you should get { ‘1’: ‘c’, ‘2’: ‘b’ }

but in this way you don’t get it

@systemrunner14485 - When you read through Tasks associated with the _.invert() method (23, 24 & 25), it states that you are swapping the key and value for each key/value pair. You are not inverting the object. So in your example:

var object = { ‘a’: 1, ‘b’: 2, ‘c’: 1 };

should output: {1: ‘a’, 2: ‘b’ 1: ‘c’}. Thats the way I approached it and it passed the test.

this post helped me a lot. Thanks!

1 Like

Well, actually that’s exactly what we are doing. The inverse of 4 is 1 over 4 and the inverse of that is 4. The inverse of key: value is value: key and of course the inverse of that is key: value, again.

The member does correctly show the inverse, but also shows a change in order which is non-applicable (trivial) since objects are not ordered to begin with.

1 Like

Hi,

I’m having the same problem. Hope hijacking this thread isn’t an issue.

I’m doing the invert iteration and even though I’ve copied the code exactly as the guide, I’m still getting invalid code running.

This is my code -
image

This is the instructor code -
Actually, I can’t attach it here because new users can’t attach more than 1 image. The code is on this youtube video https://www.youtube.com/watch?time_continue=34&v=av5JiKektzE at the 24 minute mark.

Please help!

new_obj[newValue] = key;

When assigning directly with = new_obj doesn’t get updated, but replaced. The above updates the object.

2 Likes

That’s exactly what I thought!!

But in the tutorial it shows this -

image

I’m still now sure what I’m doing wrong…

1 Like

The line,

    invertedObject = {originalValue: key}

assigns a single object, rather than updating it (add another key:value pair).

const _ = {
    invert(object) {
        let invertedObject = {};
        for (let key in object) {
            let originalValue = object[key];
            invertedObject[originalValue] = key;
        }
        return invertedObject;
    }
}
obj = {1:'one', 2: 'two', 3: 'three'}
_.invert(obj)
// {one: "1", two: "2", three: "3"}
4 Likes

Okay, thanks mtf! Your code works.

My issue is that in the instructors code, that works, she uses the line

and the node test works. When I do it, it does not work.

I instead use

invertedObject[originalValue] = key;

This works fine. I just can’t wrap my mind over why something was working for her and not me!!

Btw, thank you for your contribution otherwise. As I’ve been on this journey of learning, I find your answers in many FAQ’s and they’ve been really helpful. Also your quick response time :slight_smile: Blessings MTF.

2 Likes

I had a similar problem to mustu5.

The node test passed but when I tested the code separately to see how it worked it only inverted and displayed the last key/value pair in the console.

Thanks for your help mtf!

We’ll need to see your code, please.