Confused by lodash findKey test

I’m working on the lodash project in front end development and I am not understanding how the .findKey method works, partially because i don’t understand the description in the lodash library:

  • This method is like _.find except that it returns the key of the first element predicate returns truthy for instead of the element itself.*

I feel like there is a typo that prevents it from making sense and I can’t get past that even with the Codecademy explanation. I also don’t understand the solution code:

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

I’ve gone back to this section three days in a row trying to understand and I just don’t know why I can’t get it. Thank you for any insight you can give!

TL;DR: Please explain what the method does and how the code works.

Here’s a link to the project: https://www.codecademy.com/paths/front-end-engineer-career-path/tracks/fecp-javascript-syntax-part-ii/modules/fecp-practice-javascript-syntax-arrays-loops-objects-iterators/projects/lodash

Which tests are passing and which are failing?

You have a stray undefined that should removed. You also don’t need that last return. JS will return undefined automatically.

The general premise is that we pass into the function an object and a predicate function. The test provides that function so we don’t need to concern ourselves with what it is, only that as a predicate we know the return value will be true or will be false. That’s all we need to know.

We iterate over the object one key at a time. We pass the associated value to the predicate for testing. On a true return we know we have found the key for the sought after value. The key is returned and can now be used to access the object outside of the function, as needs be (we don’t need to concern over this, either).

The code is exactly as is written in the video that walks through the project. I have found at least one error in that particular video so it wouldn’t surprise me if there was random unnecessary code.

I guess the issue I am having is wrapping my brain around the term “predicate function”. I don’t understand the term therefore I can’t understand the method.

I am a highly intelligent individual and cannot explain the level of frustration I am feeling at not being able to comprehend some of the vocabulary in JS.

A predicate function is one that tests for a condition and returns true or false, a boolean outcome. We could it a conditional function, if that makes it easier to understand.

value in => value meets condition ? true or false => return outcome

Google ‘predicate’ for a dictionary definition of the term in grammar.

const isOdd = function (n) {
  return n % 2 != 1;
}

The above returns true if n is odd, and false otherwise.

Thank you! I had some sort of mental block going and could not make sense of it even googling the grammatical term.

1 Like