12. Victory!


#1


I have completed the lesson but I'm curious, how can I improve my code to get something as "clean" as this:

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

(without the quotes in the name and having the firstName, lastName with a space?)

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Waterrson",
    number: 011-2345678,
    address: ['23, USJ 3/4P, Subang Jaya']
};

friends.steve = {
    firstName: "Steve",
    lastName: "Carrell",
    number: 03-12345678,
    address: ['B-11-25, Apartment Kampung Tunku, Bukit Jelutong']
};

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

list (friends);
search ("Steve");


#3

Not that hard at all. It's simple, have a look at the code below you'll get the out put like you asked for.

var search = function(name){     
        for (var i in friends){
            if(friends[i].firstName === name){

               return  'First Name : '+friends[prop].firstName+'<br> Last Name  : '+friends[prop].lastName+'<br> Number     : '+friends[prop].number+'<br> Age        : '+friends[prop].age+'<br> Address    : '+friends[prop].address;           
                
            }
        }
    }

You'll get the expected results in the console however Codecademy won't accept the answer as they are looking for a specific set of codes to validate the output.


#4

Ah i see. Thanks so much for the tip!


#5

Hey @myssandria,

I read your post earlier this morning.

On a scale of 1 -10 I've got to say my knowledge is at about 3 / 4, meaning im not too sure but I can take a guess as to why its not good to write "03-12345678"

Im not sure how and why you got into trouble for helping, but I would appreciate your thoughts on this as I'm fairly new to coding. So do please share :slight_smile:


#6

I had a long and winded explanation prepared that I promptly didn't send it through, but my suggestion was very similar to @netsolver58780's, with exception of a few tweaks. Since this is supposed to be a newbie friendly place, I try to create code that may have a couple of more additional lines, but looks a bit cleaner and easier to follow. I added a variable to hold each property in the friends object, then placed in separate console.log lines in the format you put in your post.

I also added two elements in the address array, instead of one, so we could easily separate the address and city, state, #zipcode into the next line. As far as the phone number, without quotation marks, it is treated as a subtraction, not a string of text, and I wanted to make sure you realized or noticed that. I will write my code on the next post, without a wall of text explaining things, and if you want clarification on a particular part of it, just let me know!


#7

Here it is:

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Waterrson",
    number: "(011) 234-5678", // "prettied-up" phone number format
    address: ['23, USJ 3/4P, Subang Jaya', 'City, ST 81930'] // address['street','city, state zip#'];
};

friends.steve = {
    firstName: "Steve",
    lastName: "Carrell",
    number: "(031) 234-5678",
    address: ['B-11-25, Apartment Kampung Tunku, Bukit Jelutong', "City, ST 62381"]
};

var list = function (obj) {
    for (var prop in obj) {
        console.log(prop);
    }
};
var search = function (name) {
    for (var prop in friends) {
        if (friends[prop].firstName === name) {
            var fName = friends[prop].firstName;
            var lName = friends[prop].lastName;
            var phone = friends[prop].number;
            var address = friends[prop].address[0];
            var citystate = friends[prop].address[1];
           // Technically, these could all be in the same console line
           // The "/n" is for newline
            console.log("First Name: "+fName+"\n");
            console.log("Last Name: "+lName+"\n");
            console.log("Number: "+phone+"\n");
            console.log("Address: "+address+"\n");
            console.log(citystate+"\n");
            return friends[prop];
        }
    }
};

list (friends);
search ("Steve");

Outputs:

bill
steve
First Name: Steve
Last Name: Carrell
Number: (031) 234-5678
Address: B-11-25, Apartment Kampung Tunku, Bukit Jelutong
City, ST 62381


#8

Thank you so much for this. Its very clearly written.

So it's really adding a var to the search function to manipulate how the data is presented.

With regard to the "-" in phone number, I did understand it as possibly being a subtraction symbol though I did find some users saying its also aesthetics as well as provides ease for mobile users to click the number and have it appear in their mobile keypads.

If you dont mind, could you also explain why we use [prop]. When I excluded it from my code it didnt work. can [prop] be replaced with something else (that I "make up")?


#9

So long you wrap the whole thing in quotes, for visual aesthetic, that's fine.

Sure. There's a type of loop (already covered in the previous JavaScript lessons), that loops through objects. The structure of that loop is as follows:

foreach( property in object)

Where property is a variable you set (it could be var prop, or var item, etc.) and object is the object being looped through (like friends). And foreach is just "for each item/property you go through inside the given object, do whatever is within the code block (or the curly braces)".

So, if thought prop wasn't very descriptive, you could just change to, I don't know, person:

for (var person in friends) {

Our friends object has two properties, bill and steve. Those two are also objects, with 5 properties each (firstName, lastName, etc.).

The way you access properties in dot notation is from right to left. Say we wanted to print out Steve's last name to the console:

console.log(friends.steve.lastName); //  lastName is in --> steve is in --> friends

The way you access properties in bracket notation is similar to accessing indexes in Arrays. Say we wanted to print out Bill's phone number to the console:

console.log(friends["bill"]["number"]);

So, in our search function, we use a combination of bracket and dot notation, and we loop through every friends[property], just like the list() function does. Then, we compare the firstName property to see if it is equal to the name string we passed.

If it is, then we access the properties we want, much like the two examples above, and store it in a variable--or, if we wanted, we could just print something like friends[prop].firstName, and friends[prop].address[0], and all of those straight to the console. This is handy because can you imagine if we had 30 friends? We'd have to write 30 for loops comparing names! Instead, we use the foreach loop to write just one.


#10

Thanks so much. I'll keep all this in mind as I continue coding.

Thanks also for the tip on reading dot notation from right to left. I've actually been doing it left to right where i would say in friends --> there is this steve --> with a lastName of...


#11

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