7.Search for a friend. Can someone explain the code please


#1


The code bellow works, but i dont really understand the function "search".If someone could explain the whole function in detail I would greatly appriciate it. Especially the If-statment. I feel like it always should be true since the argument doesn't know what the firstName is.

Thank you!


var friends = new Object();
    
    
friends.bill = new Object();
friends.bill.firstName = "Bill";
friends.bill.lastName = "Gates";
friends.bill.number = 0734735668;
friends.bill.address = ["US", "America"];
    
    
friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = 73294049;
friends.steve.address = ["Sweden", "karlstad"];
    
function list(obj){
    for(var key in obj){
        console.log(key);

    }
};

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


#2

NVM just remeberd that i didnt call the function. But still could someone tell me if i can write this "friends[prop].firstName" in another way??


#3

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