For...in not working as intended

Hello,

just as the title says, my for…in loop is doing something odd and I’m not sure what the cause is.

I’ve written an object method that returns the first key that has a value that returns a truthy value from a predicate function. But when I write my for…in loop and declare the variable inside the brackets, the whole statement inside the bracket remains blue. I’m not sure if my explanation is good enough to give a proper visualization.

findKey(object, predicate) {
  for (let key in object) //*the entire loop statement stays blue//{
    let value = object[key];
    let predicateReturnValue = predicate[value];
    if (predicateReturnValue) {
      return key;
    }
  }
  undefined;
  return undefined;
}

I hope that’s enough information to share the problem that I’m having. I followed a walkthrough video at the bottom after the issue came up and the code was identical, except my code isn’t behaving like the code written in the walkthrough.

HI @sachadesjardins55743
it’s still the lodash project, right? It’s always helpful to provide a link to the exercise. However, I got it still open…

The parameter ‘predicate’ expects a function as its argument.
Therefore this

is the wrong syntax.

What is this? Is that still part of your method or did you just want to write which output you get?

1 Like

Hey Sacha

Unfortunately there are some mistakes. first findkey is not a function unless you declare it like so.
Also, you trapped your opening curly bracket of the loop inside the comment :stuck_out_tongue:

The undefined in the wild doesn’t do anything, just entropy.

At the end of the loop you don’t need to state return undefined, because functions do that automatically if you don’t return anything.

your code corrected:

function findKey(object, predicate){
  for (let key in object){ //*the entire loop statement stays blue
    let value = object[key];
    let predicateReturnValue = predicate[value];
    if (predicateReturnValue){
      return key;
    };
  }
}
1 Like

I simply followed the walkthrough video at the bottom and copied the code exactly as written by the instructor. You can see the code at 28:53.

I know you said you had the project opened already but here it is.

.findKey() takes two arguments: an object and a predicate function — a function that returns a boolean value

That means this

should be a function invocation:
predicate(value)

1 Like

It is a method within the already declared object _
This is just extracted from an already quite long block of code within this object.

Thank you for taking the time to answer.

The findKey() that I wrote is a method within an object, so I think it’s not necessary to write function beforehand.
Please correct if I’m wrong though, I just haven’t had an issue doing that thus far.

And about the undefined in the wild, I was just following instructions.

Ah alright! Didn’t know that :slight_smile: @sachadesjardins55743 ignore my first point then :wink:

2 Likes

Oh alright, interesting, what purpose does it have? Does the exercise say?

oh my. I feel so silly, your attention to detail is amazing.

That solved the issue, thank you.

However, I’m still curious as to why the ‘object’ in the for…in loop remains blue. I thought it would change color in order to be identified differently.

This is what it’s supposed to do, and does so now that mirja found the issue.

I incorrectly wrote with [value]

let predicateReturnValue = predicate[value];```

Instead of correctly calling the function with (value)

let predicateReturnValue = predicate(value);

You said:

about the undefined in the wild, I was just following instructions.

So i asked:

interesting, what purpose does it have? Does the exercise say?

But your answer does not really answer me. Does it?

1 Like

oh, sorry about that, I misunderstood.

It’s to address all cases where no truthy values were returned from the predicate.Here is a link to the exercise. It begins at step 27 (Implement _.findKey()) and ends at step 30.

You mean the syntax highlighting? The object is still an object. You are just saving its value to a new variable.

1 Like

Thanks again for the help!

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.