Some doubts in 6. contact list


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661


the original code is at bottom, and i have two questions
1." if (friends[keys].firstName === name ){
console.log(friends[keys]);
return friends[keys];
}"
"friends[keys].firstName" is the same as "friends.keys.firstName"? but when i use "friends.keys.firstName", the code can't run normally.

2 the outcome on the screen is that:

bill
steve
{ firstName: 'Steve',
lastName: 'Jobs',
number: '(408) 555-5555',
address: [ '1 Infinite Loop', 'Cupertino', 'CA', '95014' ] }
{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}

my question is that , why "{"firstName":"Steve","lastName":"Jobs","number":"(408) 555-5555","address":["1 Infinite Loop","Cupertino","CA","95014"]}" will be shown repeatedly.

thanks a lot!


var friends = {};
friends.bill = {
  firstName: "Bill",
  lastName: "Gates",
  number: "(206) 555-5555",
  address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
  firstName: "Steve",
  lastName: "Jobs",
  number: "(408) 555-5555",
  address: ['1 Infinite Loop','Cupertino','CA','95014']
};

var list = function(obj) {
  for(var prop in obj) {
    console.log(prop);
  }
};

var search = function(name) {
  for(var prop in friends) {
    if(friends[prop].firstName === name) {
      console.log(friends[prop]);
      return friends[prop];
    }
  }


#2

the reason why this doesn't work, is because (thanks to your for in loop), keys contains a string. the dot notation only works for property name, not a string of property name.

the exercise calls the function(s) for validation purpose, if you want to see the true output go to jsbin, open the javascript and console tab, and press run in the top right corner


#3

actually my code is as following:
// var friends = {
bill: {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ["One Microsoft Way","Redmond","WA","98052"]
},
steve: {
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-6666",
address: ["One Apple Way", "Whitemond", "WA", "98052"]
},
};

var list = function (object) {
for (var property in object) {
console.log (property);
}
}

var search = function (name) {
for (var keys in friends) {
if (friends[keys].firstName === name ){
console.log(friends[keys]);
return friends[keys];
}
}
}
list(friends);
search("Steve"); //

keys are bill and steve. they are strings, but i think any keys are strings like "name" "age" and so on.
so you mean" bill.name"is a string. and the dot notation can only be followed by a single name, no another dot notation..??
but the meaning of friends.key.firstName and friends[keys].firstName is the same?
sry, i am a newbie. thanks for you help


#4

in Js bin, the outcome is as following:
console:
"bill"
"steve"
[object Object] {
address: ["One Apple Way", "Whitemond", "WA", "98052"],
firstName: "Steve",
lastName: "Jobs",
number: "(206) 555-6666"
}

and the output is empty.

i dont understand why it shows "[object Object] " and the different order of address and number.

actually i can't figure out what is calling the function(s) for validation purpose. so sometimes the exercise doesn't show the true output?


#5

ignoring the loop for a second, there are two ways you can access objects:

friends.steve
friends["bill"]

either using object notation (first) or associative array notation (second)

because the for in loop stores the values in keys as string, you are used to use the associative array notation (the square brackets)

output is for html/css code, you are working in console. Ignore/close the output tab

The default conversion from an object to string is "[object Object]", which is why you see object object. (remember, for in loop stores the results as string)

the exercise needs to validate your code works correctly, to achieve this, function calls are made behind the scene, but this function calls do result in output in your console window


#6

thank you very much for so many helps.
i have another doubt in I have to celebrate you baby.
do i need to create a topic?


#7

wait, i should have added:

keys = "bill"
friends[keys]

to the example

Yes, preferable make a new topic. I could a have been able to clarify something


#8

i want to check again , the meaning of friends[keys].name is the same as friends.keys.name. the only difference is that the latter form is not correct?
actually, in the later exercises, i use the form like friends.keys.name, then there is no problem,.


#9

keys contains a string (thanks to your for in loop), so then you can't the dot notation

code from earlier exercises aren't re validated, does it pass the exercise or actually work?


#10

oh. so name is not string, it is the key/property, then can use dot notation.

it pass the exercise and somehow works but not actually works. problem is about another aspect which i have already post.


#11

yes, very good :slight_smile:

still something that requires explanation?

if you want to get more in depth, read this article


#12

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