Check if and Object has a property

Tell us what’s happening:
I’ve tried everything I know to get this function to return False, but I can’t get that to happen.

  **Your code so far**

let users = {
Alan: {
  age: 27,
  online: true
},
Jeff: {
  age: 32,
  online: true
},
Sarah: {
  age: 48,
  online: true
},
Ryan: {
  age: 19,
  online: true
}
};

function isEveryoneHere(userObj) {
// Only change code below this line
if ('Alan'&&'Jeff'&&'Sarah'&&'Ryan') {
  return true;
} else {
  userObj(!'Alan'&&'Jeff'&&'Sarah'&&'Ryan') {
return false;
  };
};
// Only change code above this line
}

console.log(isEveryoneHere(users));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36

Challenge: Check if an Object has a Property

Link to the challenge:

This is not a valid logical conditional for this challenge. What would you like this to do?

This is not valid syntax. What would you like this to do?

I would like to get a return of false if not all users are present. I have the true part figured out. I know it’s not valid syntax, but that was my last attempt. I really have no idea how to get a return of false.

no, you don’t have the return true figured out, as your condition makes so that only true is returned

how do you check if an object has a property?

You do not have the true part figured out, unfortunately. You have a invalid condition that is always true.

I’m confused why the first portion of the tests are all marked correct then. The output is true at the bottom. Just can’t figure out what this challenge actually wants, as it doesn’t seem to relate to the actually lesson using .hasOwnProperty or ‘in’ keyword. Again, I know nothing about any of this as a basic beginner, just trying to find a solution to study and understand in order to progress.

We aren’t going to give you an answer to copy. We want to help you write your own solution that works. You’ll learn more that way.

Your solution passes those tests, but your code does not actually function correctly, which is why you do not pass the other tests.

This condition is wrong. It will always evaluate as true, so your function always returns true.

You need to change this to check if the userObj has those 4 users.

the challenge asks you to check an object if it has all four names as properties(and only them). The two methods shown in the lesson are a good way of doing that.
Currently you check if the strings ''Alan", ''Jeff", “Sarah” and “Ryan” return true(which is always the case) and then the entire else block is syntatically invalid(it makes no sense).
Try to log parts of this code in console and see what they output, that way you know if they return what they are designed to

1 Like

The challenge says

Finish writing the function so that it returns true if the object passed to it contains all four names, Alan , Jeff , Sarah and Ryan and returns false otherwise.

I’ve rewritten your code to the following to help make things more clear. Written this way the problem is broken down into smaller pieces that are easier to solve individually.

function isEveryoneHere(userObj) {
// Only change code below this line

// update the lines
const hasAlan = false  
const hasJeff = false
const hasSarah = false
const hasRyan = false

const hasAllNames = hasAlan && hasJeff && hasSarah && hasRyan

if (hasAllNames) {
  return true;
} 

return false;
// Only change code above this line
}

Tip: Sometimes it helps to write pseudo code before actually writing the real code. That way you can workout the logic without worrying about the syntax.

Not sure why you think that? Your code doesn’t use anything shown in the challenge text.

  1. Did you try using hasOwnProperty or in at all? If so post that code instead.

  2. The parameter userObj is an object, not a function. Using it as a function doesn’t make much sense here. But even if it was a function the value you are passing it just evaluates to false.

1 Like

I appreciate everyones help and suggestions. Unfortunately, I still can’t figure this one out. Nothing is working correctly. This was my biggest fear of jumping into the coding world and it’s coming true lol. Maybe one day something will click.

The lesson part of the challenge provides excellent guidance on what to do.
See:

const obj={
  Alan:1,
  Jeff:1
}
console.log('Alan' in obj); //prints true to console
console.log('Alice' in obj); //prints false to console

Now you have a good understanding of &&, but each subcondition needs altering. When you write if ('Alan'), that is not the same as checking if an object has the property of ‘Alan’. And you can tell because you never had to involve the variable name of the object in the if statement, how can the compiler know to check the object for ‘Alan’, if you don’t tell it to in the if statement?

It is completely normal to be overwhelmed by learning programming for the first time.
Just take your time learning and take breaks when things get to crazy.
Then you can revisit it with a fresh perspective and energy to tackle it again.

1 Like

Please post your current code where you use the techniques shown in the challenge. Even if it is not working. We have to see your attempts.

As said, coding is just hard. You will keep moving the bar up so the problems you are tackling become more and more difficult. No matter how good you get it will always be a challenging, demanding, and at times frustrating process. It is what it is, so get comfortable with it.

1 Like