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
.
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.
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:
- findKey grabs the first value from the object and passes it to the predicate function.
- The predicate function receives âJohnâ, determines that it doesnât start with âvâ so it returns false.
- findKey grabs the next value from the object and passes it to the predicate function
- 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
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!*
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