0

While running the code below, without any function calls, I would immediately get this output

["1122","3rd St","Seattle","WA","92838"]

The closest thread that addressed this code was Need Explanation: Organization with Objects in a Contact List (Javascript, Codecademy) but it didn't quite address my concern.

I'm sure that the way I had added key,value pairs to the objects is somehow yielding this output, but I can't seem to explain why, especially when running the code, there is no function call included.

When actually trying to call search (e.g. search("steve")), it would fail but it would work on search("bill"). I thought it might be related to the javascript console but I checked using Chrome's console with the same results. Any help would be much appreciated.

var friends={};
friends.bill = {};
friends.steve={};
friends.bill["firstName"]="Bill";
friends.bill["lastName"]="Gates";
friends.bill.number="333.222.3937";
friends.steve["firstName"]="Steve";
friends.steve.lastName="Ballmer";
friends.steve["number"]="829.383.3939";
friends.bill["number"]="232.8392.2382"
friends.bill.address=['5353','Cook Ave','Bellevue','CA','94838']
friends.steve.address=['1122','3rd St','Seattle','WA','92838']


    var search=function(name)
    {
    for(var i in friends){
        if (name==i["firstName"])
    {
        console.log(friends[i])
        return friends[i]
    }
        else{
            return "no match"
        }

    }
}
1
  • I really don't see the need for it to be an object like this instead of an array, you see, you don't consider the keys as theirs first names, nor do you compare the provided input name to those keys. Or else, make your if statement like this: if (name==friends[i]["firstName"] || name==i) Commented Sep 12, 2013 at 4:47

2 Answers 2

2

try changing:

for(var i in friends){
    if (name==i["firstName"])
        ...

to

for(var i in friends){
    if (name == friends[i]["firstName"])
        ...

You meant something like:

for(var i in friends){
    if( name == i ) { //i would be either "bill" or "steve" and check against name variable
        console.log("Data for:" + i + " firstname is:" + friends[i]["firstName"]);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick response! Unfortunately, doing that then makes search("bill") not work either. As to why that happens, I haven't the foggiest. If it did get the function call search("steve") to log and return the person's contact information, then I would've been at that as well and might've had to ask since I don't see how in the context of a condition "i['firstName']" differs from "friends['i']['firstName']". Thanks, again.
@Choong-KyuKim see my added answer, you mean something like that.?
yes, that is what I'm looking for. In my original code, calling search("bill") would work but search("steve") would not even though logging friends.bill and friends.steve seemingly work equally well.
0

You are seeing this output:

["1122","3rd St","Seattle","WA","92838"]

Because it is what is currently stored in $_ (Most browser based JS interpreters will spit out what is in $_ on the prompt).

$_ is the value of the last evaluated expression, which in your case is:

friends.steve.address=['1122','3rd St','Seattle','WA','92838']

The "var search ..." is actually a declaration and will therefore not be stored in $_.

Also, your search function should probably look like this:

var search = function(name) {
  for(var i in friends) {
    if (name.toLowerCase() == friends[i]["firstName"].toLowerCase()) {
      console.log(friends[i])
      return friends[i]
    }
  }
  return "no match";
};

@cherniv might have beat me to the punch on moving the return out.

1 Comment

thanks, masahji. That helps clarify as to why that puzzling output is given. I thought I was going nuts for a second there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.