For...in Statement to Iterate Through the Keys of an Object

That’s the solution. Not my work. My code editor in the actual problem is the top part with no console.log + nothing written below
\

 // change code above this line
}
console.log(countOnline(users));

I think the problem is a bad edit.

There is nothing wrong with the solution. Just copy-pasting it into the middle of some other code will cause at best nonsense and at worst code that does not even run.

The sample solution has a sample object so you can see it run. That’s from an older version of the challenge. The function in that solution is correct and only differs from the current challenge in the name of the function argument.

function countOnline(usersObj) {
  // Only change code below this line

  // Only change code above this line
}

The solution doesn’t work because it includes code written below the lines maybe? I really think its a bad edit on the lesson side. Very confused about how this is supposed to work. user, users, obj usersObj. Lot of variables here and not sure which is meant to be there and not

The editing is fine.

Look carefully at the solution. The solution provides

  1. a sample users object
  2. the function from the challenge
  3. sample use of the function

The challenge only wants you to supply the function. The only difference between the function from the challenge and the function from the solution is the name of one variable.

OK, great hints! I wrote it out from scratch and it worked!
I was confused by the console.log in the solution provided. Too many variables to know why it wasn’t working. Still not sure why it has to be [bracket-notation]…because PROPs are arrays?

You need bracket notation because user is a variable and you want to use the contents of this variable. I think it was mentioned in one of the earlier lessons, but it’s easy to overlook.

Dot Notation: (won’t work with variables)

let myProperty = myObject.myProperyName

Bracket Notation: (Works with variables)

let myVariableWithProperty = "myPropertyName";
let myProperty = myObject[myVariableWithProperty]
1 Like
const person = {
     'name': 'magopolis'
};

console.log(person.name) looks for the object person, with the property of 'name'.
console.log(person.blah) looks for the object person, with the property of 'blah'


const x = 'name';
console.log(person.x) looks for the object person, with the property of 'x'
console.log(person[x]) looks for the object person, with the property of the value of x (which is 'name').
console.log(person['x']) looks for the object person, with the property x

Look at these and be sure to fully understand why you get their results.

1 Like

the obj with the record collection.
Robert Palmer: Addicted to love lol

kerafyrm,

so
console.log(person['x']) looks for the object person, with the property x
you mean [‘x’] as in the value? like Malcom X? The quotes make it a string (which is the value of the property)

console.log(person.name) looks for the object person, with the property of 'name'.

…will log “Magopolis”.

@magopolis

Look at it like this:
person['x']
and
person.x
are almost the same thing. They look for the property of the string ‘x’.

Whereas:
person[x]
would look for the property (value of x) and not string ‘x’

So:

let x = 'z';
const person = {
     'x': 'hi magopolis'
};

person.x and person['x'] both exist, because you see the property ‘x’ exists.
but
person[x] does not exist, because x === ‘z’; And person.z does not exist.

One more example:

const person = {
    'head': 1,
    'legs': 2,
    'fingers': 10
};

console.log(person.head) // 1
console.log(person['head']) // 1
let body_part = 'fingers';
console.log(person[body_part]) // 10
console.log(person.body_part) // undefined (there's only head/legs/fingers)
console.log(person['body_part']) // undefined (for same reasons above)

Adding to @kerafyrm, just in case, an object property, in default is a string, so when you are trying to access it using the bracket [] notation, you have to use the quotes ' ' or " ". Here’s an example:

const person = {
  head: 1,
  legs: 2,
  fingers: 10
}

console.log(person[fingers])/*this will spit out undefined, because there is 
no variable called "fingers" */

console.log(person["fingers"[]); /* this will return 10, because an object 
property is defined as a string.

You can also always just put it in string in the first place like @kerafyrm explained on this one

But just in case you came into this problem.

let x = 'z';
const person = {
     'x': 'hi magopolis'};
console.log();

I set this up in a codepen https://codepen.io/Magopolis/pen/bGEdGPV and surprised to see c.log(person.x); returned ‘hi magopolis’…
resembling
console.log(person.head) // 1

Why does this surprise you?

i’m always surprised when code works lately. person[x] == person['x'} It made sense when I got it into Codepen because I could see the record collection Obj in my mind. Just so many questions. Every answer is 3fr informative && 1fr confusing.

Catalitics wrote:
console.log(person["fingers"[]);
is that extra “[” a typo or some new JS thing I dont know?

I did the Web Design cert in 2 weeks but JS is a lot harder. I think I’m just hitting a wall. I wont give up!! thank you all for your help. I will be leaning on y’all a lot moving fwd

oh, and why person[x] and not obj.person[x]?
code pen returned “obj Obj” or something

That’s fair.

The extra [ is in fact a typo.

JS is tricky when you haven’t done programming before.

In this case, you have a person object, while in the challenge you have an array usersObj of user objects, so you don’t need obj.person.x.

For the code

// Declare some variables
let x = 'z';
const person = {
  'x': 'hi magopolis',
  'z': 'tricksy hobbits',
  };

console.log("Using 'person.x':");
// This looks for the property x of the object person
console.log(person.x);
console.log();

console.log("Using 'person['x']':");
// This also looks for the property x of the object person
console.log(person['x']);
console.log();

console.log("Using 'person[x]':");
// This looks for the property stored in the variable x of the object person
// i.e. looks for the property z of the object person
console.log(person[x]);
console.log();

console.log("Using 'person['x']':");
“…the property stored in the variable x of the object person…”

ahhh ok thats counter-intuitive to me because the “” around ["x"] make me think of it as a string assigned as a value to a variable called “x”.
console.log(person); // returned [object Object]
is that the computer’s generic way of logging “object” (for "x": or "z":) and “Object” (capitalized because the value Tricksy or Magopolis?). My code didn’t specify obj: Obj like 'name': 'Magopolis'

Well I’m quite sorry if I confused you, but the [ ] is another way to access an object. It’s called Bracket Notation. But doing person.head will also works, it’s another way to access an object, it’s called Dot Notation. I’m sorry again if you did get a little confused, I’m sometimes used to writing bracket notation lately for my projects that it became a habit.

Anyways,

The console returns this saying that the variable contains an object and you didn’t specify which specific Object you want to log.

Anyways Happy Coding!!! :slight_smile:

// Declare some variables
let x = 'z';
const person = {
  'x': 'hi magopolis',
  'z': 'tricksy hobbits',
  };

console.log("View the object:");
// This will log the person object
console.log(person);
console.log();

console.log("Using 'person.x':");
// This looks for the property x of the object person
console.log(person.x);
console.log();

console.log("Using 'person['x']':");
// This also looks for the property x of the object person
console.log(person['x']);
console.log();

console.log("Using 'person[x]':");
// This looks for the property stored in the variable x of the object person
// i.e. looks for the property z of the object person
console.log(person[x]);
console.log();

console.log("Using 'person.z':");
// This looks for the property z of the object person
console.log(person.z);
console.log();

console.log("Using 'person['z']':");
// This also looks for the property z of the object person
console.log(person['z']);
console.log();

Output:

View the object:
{ x: 'hi magopolis', z: 'tricksy hobbits' }

Using 'person.x':
hi magopolis

Using 'person['x']':
hi magopolis

Using 'person[x]':
tricksy hobbits

Using 'person.z':
tricksy hobbits

Using 'person['z']':
tricksy hobbits
2 Likes

no worries man. I’ve just been trudging along not asking questions this whole time and I’m making it up with a vengeance, ;). I was going to respond to you directly but I had multiple confusions going on. I got [ ] notation. Just still working out the nuances. JS is like learning an alien language. Please continue to confuse me. I need it!

2 Likes