Lodash Javascript - not defined error

Hello everyone,

This exercise drives me a bit crazy.
I keep having this error code for invert :

  • Returns an object with all keys and values inverted - Failed: _.invert({originalKey: “originalValue”})[“originalKey”]) returned undefined instead of anotherKey.

and for findkey method :
1 - _.findKey() is defined - Failed: _.findKey() was not properly defined.

What should I look for when having those errors ? The ‘not properly defined’ one I had it for numerous methods of this exercise. That’s my main issue, I understand what I’m doing, but I don’t understand the error codes, and what to look for to solve by myself the error.

Thanks a lot,

const _ = {

clamp(number, lower, upper){
  var lowerClampedValue = Math.max(number, lower);
  var clampedValue = Math.min(lowerClampedValue, upper);
  return clampedValue;
},


inRange(number, start, end){
  if(end === undefined){
    end = start
    start = 0
  }
  if(start > end){
    var temp = end 
     end = start
     start = temp
  }
  var isInRange = start <= number && number < end
  return isInRange
},

words(string){
  const words = string.split(' ');
  return words;

},

pad(string, length){
  if(string.length >= length){
    return string;
  };
  const startPaddingLength = Math.floor((length - string.length) / 2);

  const endPaddingLength = length - string.length - startPaddingLength;

  const paddedString = ' '.repeat(startPaddingLength) + string + ' '.repeat(endPaddingLength);
  return paddedString;
},

has(object, key) {
  const hasValue= object[key];
  if(hasValue != undefined){
    return true;
  } return false;

},

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

},

findkey(object, predicate){
  for(let key in object){
    let value = object[key];
    let predicateReturnValue = predicate(value);
    if(predicateReturnValue){
      return key;
    };
  };
  undefined 
   return undefined;

}






};






// Do not write or modify code below this line.


module.exports = _;

If you can still edit, please insert all your code (the complete _ object.).

1 Like

There is a problem. We should be updating the object, not replacing it.


What is this about?

Sorry, I don’t think I get it. Should it be :
invertedObject = {object : key} ?

No, because that is just another object with only one key-value pair. Think in terms of subscript assignment.

I was also wondering, first I only wrote " return undefined", but trying to find the issue I used the help video, which used this, left me puzzled.

What is subscript ? English is not my first language, I’m lost now. Do you mean it’s related to the scope ?

It makes sense to return something, but the stray text above the return line leaves a question mark. Technically that return is not needed. JS will return undefined on its own.

Okay, this makes more sense, thanks.

object[key]

It is how we access the values of an object.

So,

invertedObject = originalValue

Since originalValue is object[key] ?

It is the value accessed at that key.

obj = {
  one: 1,
  two: 2,
  three: 3
}
invObj = {}
for (let key in obj) {
  let value = obj[key]
  invObj[value] = key
}

Look closely.

1 Like

I think it’s starting to make sense,

then could it be ?

invertedObject[originalValue] = key
1 Like

I’m still stuck with my undefined error on findkay, even without the unneeded undefined…

Let’s have another look at that method, just the one, this time.

Be sure to save before every test.

Ok great, all good for invert() ! Thank you for your exemple earlier, big big help.

1 Like

Extra Study

If we invert, and then invert again we effectively remove duplicate values. Their keys will also be gone. The one remaining key will be the last one in the heap. See if you can play this one out, when you get the spare time.

1 Like