Part 8 Question


#1

I was wondering how I can separate each element in the address array with an extra space (like this: "Redmond, VA" instead of "Redmond,VA") when logged into the console.

I was also having trouble making it log "No match." into the console for just a SINGLE time when the for/in loop iterates through the entire object and finds no matching name. In my code below, it logs "No match." for every single nested object that doesn't match.

Here is my code:

// Main object
var friends = {
    // Nested objects
    bill: {
        firstName: "Bill",
        lastName: "Pifheba",
        number: "1234567890",
        address: ["One Microsoft Way", "Redmond", "WA", "98052"]
    },
    
    steve: {
        firstName: "Steve",
        lastName: "Ajfiae",
        number: "1234512345",
        address: ["2 Crap Way", "Luls", "WA", "12345"]
    },
    
    bob: {
        firstName: "Bob",
        lastName: "Dcfiwae",
        number: "8291047261",
        address: ["28 Crap Way", "What", "WA", "54321"]
    }
};

// Lists nested objects inside the main object
/*
var list = function() {
    for (var key in friends) {
        console.log(key);
    }
};
*/

// Searches for matching name
var search = function(name) {

    // Iterates through nested objects
    for (var x in friends) {
        // If match is found
        if (friends[x].firstName === name) {
            // Print contact information
            console.log("First Name: " + friends[x].firstName + "\r\nLast Name: " + friends[x].lastName + "\r\nNumber: " + friends[x].number + "\r\nAddress: " + friends[x].address);
        } else {
            console.log("No match.");
        }
    }
};

// Asks for who to search for
search(prompt("Name?"));

#2

@stevenyeung,

var friends = {
// Nested objects
bill: {
firstName: "Bill",
lastName: "Pifheba",
number: "1234567890",
address: ["One Microsoft Way", "Redmond", "WA", "98052"]
},

steve: {
    firstName: "Steve",
    lastName: "Ajfiae",
    number: "1234512345",
    address: ["2 Crap Way", "Luls", "WA", "12345"]
},

bob: {
    firstName: "Bob",
    lastName: "Dcfiwae",
    number: "8291047261",
    address: ["28 Crap Way", "What", "WA", "54321"]
}

};

// Lists nested objects inside the main object
/*
var list = function() {
for (var key in friends) {
console.log(key);
}
};
*/

// Searches for matching name
var search = function(name) {

// Iterates through nested objects
for (var x in friends) {
    // If match is found
    if (friends[x].firstName === name) {
        //Prepair address format
        var addrStr = "";
        for (var i=0, len = friends[x].address.length; i<len; i++ ) {
            addrStr += friends[x].address[i] + " ";
        }
        //console.log( addrStr );
        // Print contact information
        console.log("First Name: " + friends[x].firstName +
        "\r\nLast Name: " + friends[x].lastName + 
        "\r\nNumber: " + friends[x].number + "\r\nAddress: " +
        //friends[x].address);
        addrStr );
    } //else {
    //    console.log("No match.");
    //}
}

};

// Asks for who to search for
search(prompt("Name?","Steve"));


#3

Hi.
Your "no match" string should go after the For loop finishes. I personally added a var 'count'=0 at the beginning and increased it to 1 if a name was found. The after the loop simple check if count===0 -> no matches.

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

}
if (count===0)
    {
        console.log('no matches')
    }

}