Trouble with recreating lodash invert exercise

Hi! This is my first post so I hope I don’t just ask something redundant. I’m currently working through the “Re-creating the Lodash Library” module: (https://www.codecademy.com/paths/web-development/tracks/web-dev-js-arrays-loops-objects/modules/pjs-javascript-capstone/projects/lodash)

I’m on Step 23, where I am trying to implement the invert method, but I’m getting a Error. Here is my code for the method:

const _ = {
  invert(obj) {
    let arr = [];
    for (const [key, value] of Object.entries(obj)) {
        arr.push([value, key]);
    }
    return Object.fromEntries(arr);
  },
};

When I try to run the test, I get an Error as follows:

/home/ccuser/workspace/underscore-javascript-capstone/_.js:47
    return Object.fromEntries(arr);
                  ^

TypeError: Object.fromEntries is not a function
    at Object.invert (/home/ccuser/workspace/underscore-javascript-capstone/_.js:47:19)
    at Object.<anonymous> (/home/ccuser/workspace/underscore-javascript-capstone/test/invert.js:15:167)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
    at startup (bootstrap_node.js:151:9)

I’m wondering if this is a quirk with the Node.js version, or if I’m doing something wrong. I tried my code in another online JavaScript playground and it seemed to work. Any advice?

1 Like

It may well be that the LE is older than version Node.js 12.0, which is the earliest one to support the method.

Fear not, just using the very basics will still work nicely. Low overhead, too.

2 Likes

Okay! Good to know. Thanks!!

1 Like

Sam issue here :man_facepalming:t4:
I’ll just try a different solution, guess just like the real world, not all features will be supported in the work environment you are working!

Here is what I had:

_.invert = (obj) => {
  const array = Object.entries(obj);
  const reverseArray = array.map(x => x.reverse());
  const reverseObj = Object.fromEntries(reverseArray);
  return reverseObj;
}

Works fine on the Firefox console but searching the web found the same thing… Probably a version issue.