8. Victory! Contact List


#1


https://www.codecademy.com/courses/javascript-beginner-en-3bmfN/0/8?curriculum_id=506324b3a7dffd00020bf661


Code works. This is an extra credit question from code academy and I was thinking about how I would do this and I really don't have a clue. Can anyone shed some light? Here's the question:

We did some basic logging of your contact list to the console, but we could have made it look even nicer. How might you format the output to look like this?

First Name: Steve
Last Name: Jobs
Number: (408) 555-5555
Address: 1 Infinite Loop
Cupertino, CA 95014


var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Bat",
    address: ["2 Blue", "bbb", "NY", 08347],
    number: 555-1212
}

friends.steve = {
    firstName: "Hill",
    lastName: "Bill",
    address: ["1 See rd", "EBD", "NJ", 08883],
    number: 473-1212
}

friends.me = {
    firstName: "Frank",
    lastName: "Franks",
    address: ["17 Cross St", "SRS", "PA", 08338],
    number: 1212-1212
}

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

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


Make a beautiful contact search
#2

Here is a procedural approach that can take an object, friends or any other so long as it has the four properties in this exercise.

var friends = {
  bill: {
    firstName: "Bill",
    lastName: "Gates",
    number: "(206) 555-5555",
    address: ['One Microsoft Way','Redmond','WA','98052']
  },
  steve: {
    firstName: "Steve",
    lastName: "Jobs",
    number: "(408) 555-5555",
    address: ['1 Infinite Loop','Cupertino','CA','95014']
 }
};

var pretty_print = function (obj) {
    var order = ['firstName', 'lastName', 'number', 'address', 'address2'];
    var formats = {
        firstName: "\nFirst Name: ",
        lastName:  "\nLast Name:  ",
        number:    "\nNumber:     ",
        address:   "\nAddress:    ",
        address2:  "\n            "
    };
    for (var key in obj) {
        var s = "";
        var u = obj[key];
        for (var i = 0; i < order.length; i++) {
            s += formats[order[i]];
            if (i < 3) {
                s += u[order[i]];
            } else            
            if (i === 3) {
                s += u[order[i]][0];
            } else {
                s += u[order[i-1]].slice(1).join(', ');
            }
        }
        console.log(s);
    }
};

pretty_print(friends);

Output:

First Name: Bill
Last Name:  Gates
Number:     (206) 555-5555
Address:    One Microsoft Way
            Redmond, WA, 98052

First Name: Steve
Last Name:  Jobs
Number:     (408) 555-5555
Address:    1 Infinite Loop
            Cupertino, CA, 95014

Here is an alternate conditional control flow construct:

    for (var key in obj) {
        var s = "";
        var u = obj[key];
        for (var i = 0; i < order.length; i++) {
            s += formats[order[i]];
            switch (i) {
                case 0:
                case 1:
                case 2: s += u[order[i]]; break;
                case 3: s += u[order[i]][0]; break;
                default: s += u[order[i-1]].slice(1).join(', ');
            }
        }
        console.log(s);
    }

#3

Thank you, MTF. Very helpful.


#4

Not as clean/formatted as mtf's but here was my take on it

var search  = function(name){
    for(var key in friends){
        if(friends[key].firstName === name){
	        for (var i in friends[key] ) {
	  		console.log(i+": "+ friends[key][i] );
		}
		return friends[key];
        }
    }
};

#5

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