8. Victory! - Further Learning [Javascript, changes to lessons code]


#1


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

I just wanted to share my further tinkering for others to look at and compare. I've figured out, after a few hours of tinkering, how to console log in the format the excercise suggested... I also figured out how to make searching for both first and last name's possible. By extrapolation, one could figure out how to search for any of the terms.

var friends = new Object();

friends.bill = {
    firstName: 'Bill',
    lastName: 'Gates',
    number: '(08) 9604 5376',
    address: ['One Microsoft Way','Redmond','WA','98052']
};

friends.steve = {
    firstName: 'Steve',
    lastName: 'Jobs',
    number: '0421 345 564',
    address: ['1 Infinite Loop','Cupertino','CA','95014']
};

friends.martin = {
    firstName: 'Martin',
    lastName: 'Shkreli',
    number: '(08) 9657 8327',
    address: ['330 Madison Avenue, Suite 661', 'New York', 'NY', '10017', 'United States']
};

friends.mark = {
    firstName: 'Mark',
    lastName: 'Zuckerberg',
    number: '',
    address: ['Facebook HQ', 'Menlo Park', 'CA', '650', 'United States']
};

friends.barack = {
    firstName: 'Barack',
    lastName: 'Obama',
    number: '911 - President',
    address: ['1600 Pennsylvania Avenue', 'NW', 'Washington, D.C.', '20500', 'U.S.']
};

friends.seals = { 
    firstName: 'Seal Team 6',
    lastName: 'DEVGRU',
    number: 'Speed Dial - 1',
    address: ['3632 Tulagi Rd, Coronado', 'San Diego', 'CA', '92118', 'United States']
}; 

friends.isis = {
    firstName: 'ISIS',
    lastName: 'Terrorists',
    number: 'White House',
    address: ['Secret cave base', 'Fallujah', 'Al Anbar, Baghdad', '', 'Iraq']
};

friends.putin = {
    firstName: 'Vladimir',
    lastName: 'Putin',
    number: '',
    address: ['Kremlin Senate', 'Moscow','Moskovski Oblast', '']
};

    // address format: [street address, city, state, postcode country] 
    // blank fields must be notated as an empty string. 
    // I.e. '',
    
var list = function(friends) {
    for (var name in friends) {
        console.log(name);
    }
}; 

var search = function(name) {
    for (var i in friends) {
        if(friends[i].firstName === name || friends[i].lastName === name) {
            console.log("First Name: " + friends[i].firstName);
            console.log("Last Name: " + friends[i].lastName);
            console.log("Number: " + friends[i].number);
            console.log("Address: " + friends[i].address[0]);
            console.log("         " + friends[i].address[1] + ", " + friends[i].address [2] + " " + friends[i].address[3]);
            if(friends[i].address[4] != null) {
            console.log("         " + friends[i].address[4]);
            }
        }
    }
};

// Embedded if loop to print out country, if that element exists in the array 

search();
    // Search your friends list as a string //

Search result example:

First Name: Vladimir
Last Name: Putin
Number:
Address: Kremlin Senate
Moscow, Moskovski Oblast

I also embedded an if loop to log the country, if that array element exists. Formatting does translate well but I've also figured out how to indent the address.

I'd like to see how anyone could improve on my code or fault any of it.


#2

I dont see any improvments to make in this code... Maybe but the persons in an object :confused:

var people = {

putin : {

}

anotherPerson: {

}

}

#3

Well, I was just tinkering with the code for the last 5 minutes and I came to a nice conclusion... This is the new code.

function search(name) {
for (var i in friends) {
var info = friends[i]
if(info.firstName === name || info.lastName === name) {
console.log("First Name: " + info.firstName);
console.log("Last Name: " + info.lastName);
console.log("Number: " + info.number);
console.log("Address: " + info.address[0]);
console.log(" " + info.address[1] + ", " + info.address [2] + " " + info.address[3]);
if(info.address[4] != null) {
console.log(" " + info.address[4]);
}
}
}
};

I found out by declaring a local variable in the function, called "info" which equates to the information contained within the for loop, //var i in friends// I have streamlined the code a little and made it easier to read. Granted, it is slightly more difficult to understand that "info" means the array element of the function friends, but once someone can grasp that stand in, things are a bit better.

I see what you're saying though... It would be easier to read as object literal notation but I prefer object constructor notation because I believe it's more beautiful and it makes creating new objects simpler.

I've also tinkered the format of functions. I like function search() {}; over; var search = function() {};


#4

You can also put all that logs to console in a seperate function.. Make your code more cleaner.


#5

How do you mean? I don't fully understand how I could separate the logs into another function... Could you show me?


#6

Actually I just went and tinkered a bit... How does this look to you?

function search(name) {
for (var i in friends) {
var info = friends[i]
if(info.firstName === name || info.lastName === name) {
log();
};

    function log() {
        console.log("First Name: " + info.firstName);
        console.log("Last Name: " + info.lastName);
        console.log("Number: " + info.number);
        console.log("Address: " + info.address[0]);
        console.log("         " + info.address[1] + ", " + info.address [2] + " " + info.address[3]);
        if(info.address[4] != null) {
        console.log("         " + info.address[4]);
        }
    }
}

};

http://jsbin.com/sahurahisu/edit?js,console

Here's the code now... I've altered it so that when you call the search function, it calls a function called "log" and then the block of console log is the function of "log."