Check if an Object has a Property - only true or false

Why is it only returning true or false? Sorry I forgot how to edit this properly.

function isEveryoneHere(userObj) {

for(let u in userObj) {
     if(u in userObj) {
    return true
  } else {
    return false

Hey there :wave: :slightly_smiling_face:

Type two rows of three backticks, and insert your code between them:
your code

A few things here:

Given you only have a return true and a return false, what else could it return?
I actually don’t see a way this could return false so it might be more accurate to ask why it only returns true. You have a for loop that goes through every attribute in the object and asks if it has that attribute. To put this in simpler terms:

Imagine you have a shopping list:


Now go through each item and ask if that item is in the list. I can’t see an outcome asides from yes.
Given this, you’ll have to look at another way to check each property.


Perhaps an array containing each property? You could loop through it and compare it to userObj.

Next thing you should look at is the logical flow of the program. You have a for loop to go through each attribute, but regardless of how the if is evaluated it returns on the first iteration. How do you think this could be avoided? One idea would be a flag variable that starts as false and is set to true if someone is missing.At the end of the loop it would return based on this.
But that’s hardly the best way to do things. What if they first person is missing? Now you still have to loop through the whole thing. I can thing of a better way but I don’t wanna spoil it for you. :wink:

As an extra note... It was probably just missed when you copy pasted, but I think your function is currently missing a closing bracket.

Thanks for the help.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).


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