findKey() on Re-creating the Lodash Library


#1

Hi, I’m doing the project on Re-creating Lodash Library.
Now, I’m struggling with the section of findKey().
Does anyone know the hint of this??
Here is my problems. What is “predicate is not a function”?


#2

There seems to be nothing wrong with your code for findKey, although you have missed to add

return undefined;

after the for…in loop.

I am a bit suspicious though of the invoke rule for findKey() I see at the bottom of the code,
as you can see it calls findKey with a variable users and an object whilst find key calls for an object first and then a function.

This is my code for fndKey()

findKey (object, predicate) {
    // Check each key of object
    for(let key in object) {
      
      // Find the value of the object key
      let value = object[key];
      // Call the predicate function on the value of the object key
      let predicateReturnValue = predicate(value);
      
      // Check if the functions gives a truthy
      if (predicateReturnValue) {
        return key;
      };
    };
    
    // if the for...in loop gives no truthy values return undefined
    return undefined;
  
  },

#3

I still can’t see the right way…
This is the object that I created before.
And you can see how to invoke findKey() on the previous image.
Could you give me any hints??


#4

You don’t need to create an object at all and I am surprised to see you thought so, it is not in the instructions ;). The test/find-key.js runs and calls for the function findKey with its own values.

The reason why you are getting a predicate is not a function error is because the invoke section you call for findKey with two parameters both being objects… Whilst it expects an object and a function ;). Hence the error predicate is not a function. The error message even points out to line 93 saying something is wrong there.

Did you type that whole invoke section? It sure isn’t in mine ;). If so delete or Comment out that whole section run the test again. In the nodejs type:

node test/find-key.js

Hope it helps ;).


#5

Thanks a lot!
Finally, I could pass the findKey().
I’ve misunderstood at the beginning of lodash. I should’ve read the instructions carefully…
Anyway, I appreciate it.


#6

Owing to lack of time/space, the whole predicate thing likely throws a lot of learners off. Yes it is explained (as I recall) though what is not told is where is that predicate function?

The test contains it, that’s where. We should note that we can test this with any predicate of our own.

Once we build this method to work, it’s ours to use and re-use to our heart’s content. That’s the beauty of it. Pure utility.


#7

HI.

I’m doing like bear27_yuta. I always do as a function on my own to check if it works first. I was thinking there is a sens to do like this. Make a first try, see if it works then check it on the termianl thru codecademy … is it bad ?


#8

It’s all up to you, but the terminal test function is there for the purpose of testing :stuck_out_tongue: .