8. Victory, how to format the output to come back clean without [] and {} etc


#1

on #8 it challenges us to think of how to change the output to format the contact information with just the answer and not with the extra symbols we inputted. My first thought was to just delete the

console.log(friends[key]);

but that didn't work at all and i'm wondering why? and so do you always have to use console.log() to then use return ; ?

Many Thanks


#2

Could i see your full code? I need to see what exactly the output is in order to help


#4

i modified the search to be a little more specific and since its not going to show 'firstName' anymore i added my own in.

var search = function(name) {
for(var prop in friends) {
if(friends[prop].firstName === name) {
console.log("FirstName: " + friends[prop].firstName);
console.log("LastName: " + friends[prop].lastName);
console.log("Number: " + friends[prop].number);
console.log("Address: " + friends[prop].address);
return friends[prop];
}
}
};

i also had to change the address a little bit to kind of fit in.. im not sure if it is the best way to do it but it worked

address: ['One Microsoft Way\n \ Redmond WA',' 98052']


#5

@denkigroove I was able to format the search function code to not need to change the address format, just thought I'd share it

var search = function(name) {
for(var searching in friends) {
if(friends[searching].firstName == name){
console.log("First Name: " + friends[searching].firstName);
console.log("Last Name: " + friends[searching].lastName);
console.log("Phone: " + friends[searching].number);
console.log("Address: " + friends[searching].address[0]);
console.log(" " + friends[searching].address[1] + ", " + friends[searching].address[2] + " " + friends[searching].address[3]);

Output:

bill
steve
First Name: Bill
Last Name: Gates
Phone: 555 555-5555
Address: One Microsoft Way
Redmond, WA 98052

You can treat these nested objects much like the 2d arrays, or nested arrays. This means you can access each individual portion of the "address" array, it doesn't quite look so pretty(and there's probably a better way to do it than this), but it doesn't require changing the format of the address value.

*in my acutal output, the spaces I added to the console.log comand made the "Redmond" even with the "One Microsoft Way", but that doesn't seem to translate to the block quote format of these forums


#6

yup thats a better way i like it!