Type error .findkey method

Hi, please help me how to fix the error and understand my problem

Hello, and welcome to the forum!

The method you should be creating is named findKey with an uppercase K, so I’m surprised the test didn’t fail immediately for that.

Did you happen to write some code to test it yourself in _.js? My guess is that you wrote code to test findkey() yourself but didn’t include a function as the 2nd argument, so it is displaying this error when the test\find-key.js tries to load _.js.

After doing what you said When I hit Node _.js , it still gives typing errors


You wrote code yourself to test the function here, but you didn’t include a function for the second argument so it generates the error. Last time you posted the error displayed by running test/find-key.js which loaded _.js, but now you are running _.js directly but seeing the same error.

If you take out these lines, then you could run test/find-key.js to see if your tests pass.

If you did want to test it yourself, you’ll need to call it correctly. findKey takes an object and a predicate function.

  • You created an object (users) but tried to send only users.name
  • You need a predicate function (a function that takes in a value and returns either true or false)

An example of a predicate function from the test itself:

const startsWithV = string => string.startsWith('v');

It returns true if the value passed to it begins with ‘v’ and false if it doesn’t. The function name startsWithV (without the parenthesis) would get passed as the 2nd argument to findKey so that findKey can call it for each key in the object it was sent.

I did what you said, but it still gives an error .I feel stupid

Don’t feel stupid at all. It may feel like you’re battling the same error because the message ends up being similar, but the nature of this one is different. You’re making progress. Let me explain:

The example predicate function I posted came directly from the test\find-key.js file. I didn’t make it clear enough that it was just an example of what it means to have a predicate function. The one in the find-key.js test isn’t a very robust or complete function for complex objects, or even simple objects with more than just string values.

In this case what is happening:

  1. findKey grabs the first value from the object and passes it to the predicate function.
  2. The predicate function receives “John”, determines that it doesn’t start with “v” so it returns false.
  3. findKey grabs the next value from the object and passes it to the predicate function
  4. The predicate function receives the number 30, then tries to use the startsWith function on it but that’s a built-in function only designed for Strings. Error is displayed

Here’s an example of a slightly more robust version that won’t fail if a number is sent to it. Making predicate functions or even writing your own tests weren’t part of the lesson, but in case you want to try your hand at fixing the example one now that you know why it failed, I’m going to blur this one

const startsWithV = string => {
  if (typeof string === 'string') {
    return string.startsWith('v');
  return false;

Remember that if findKey returns undefined then that is fine. It means it didn’t find a result. This extremely limited predicate function is only looking for a value that begins with a lowercase ‘v’. You could change the name to “vincent” (notice the lowercase).

In the end, re-creating these lodash functions is a big accomplishment and shows you know a lot about working with objects.

my friend Thank you for your encouraging words :heartpulse:
When I replaced the word you said , node .js is running correctly but node test/find-key .js It still gives an error .Please explain my mistake.
.findKey() Tests:
1 - _.findKey() is defined - Passed!
2 - Returns the corresponding key of a value that returns truthy from the predicate function - Failed: _.findKey({“key”: “value”}, startsWithV) returned value instead of key.
3 - Returns undefined if an object has no values that return truthy from the predicate function - Passed!*

1 Like

Excellent, now you’re at the part where you can debug the method you were working on:


The failure reason is telling us that it received value back instead of key. Your function is working but it’s returning the wrong part of the key/value pair. Looking at your code, the confusion is because the variable names don’t reflect what they represent.

for(let variable in object) means that the keys of the object will be iterated through and each key will be stored in the variable variable as it loops. variable is actually the object’s key.

Next you are doing let Key = object[variable] to get the value for that particular key. So “Key” is storing the value.

Right now you are returning “Key” but that’s actually the value. You want to return the object’s key that the predicate determined was a match, so you need to return variable instead

If you decide to go back and rename your variables, be very careful. predicate(Key) is being used correctly because you are passing it the value.

Thank you very much for your help :ok_hand:

1 Like