Another exercise 7 thread... :(


#1



I have read through many of the threads on the forum but still quite confused by this exercise. To be honest I was clinging on at exercise 6 but my comprehension of what's going on has really slipped at this point.

I got a pass for this code but what prints is '["One Apple Place","Apple Street","Los Angeles","California"]' - no names.

I'm really confused by the introduction of the square brackets within the search function, I only thought to put them in due to the threads I had read on here. Why do they go here and why are they square brackets?! Has this been explained in a previous exercise? I looked at some other threads but still can't figure it out!

Also why am I not getting the console.log results I am expecting?


var friends = new Object();

friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = "(305) 6786-150";
friends.bill.address = ["PC World", "Computers House", "Hard Drive Lane", "New York", "New York"];

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "(206) 555-5555";
friends.steve.address = ["One Apple Place", "Apple Street", "Los Angeles", "California"];

var list = function(data) {
    for (var contact in friends);
    console.log(contact);
}

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


#2

the search function never gets executed, you never call it:

search("Bill");

please tell me i don't have to explain a function call? Either way, if put your function call inside a console.log:

console.log(search("Bill"));

you can remove the console.log inside your search function, since now what is returned is logged to the console thanks to the console.log on your function call


#3

argh yeah just realised that - doh!

still q confused by the structure of the function itself though and the use of the square brackets


#4

the square brackets where? In the if statement? Not surprising, many people would struggle with that. In js this is tricky, so far my understanding is correct, is this the explanation:
objects are also associative arrays in js. which allows you to use both the . and square brackets notation. Because of the for in loop, you are forced to use the square brackets (not sure why, js is confusing under the hood)


#5

got you, yeah it's a little confusing and quite hard to trace where it's covered in the course up until this point. will do a little more research on associative arrays.


#7

let me help you a little:
http://www.quirksmode.org/js/associative.html