6. We made a friend


#1

This isn't working. Anyone? Thanks

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        printPerson(contacts[i]);
    }
}

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName) {
    var contactsLength = contacts.length;
    for(var i = 0; i < contactsLength; i++) {
        if(contacts[i].lastName === lastName) {
            printPerson(contacts[i]);}
        else{console.log("No contacts found.");   } 
        };
        };


function add(firstName, lastName, email, phoneNumber){
    var Jim = {
        firstName: "Jim",
        lastName: "Smith",
        email: "jim.smith@ggg.com",
        phoneNumber: "01020020202"
    };
};
add();

#2

you seem to have missed step 3 of the instructions:

Add this new contact object to the contacts array


#3

I've just tried that but I'm told that 'Jim is not defined'.


#4

okay, you tried something, but you are not going to show me what you tried? So i have to guess what you tried? :stuck_out_tongue: Please show me what you tried


#5

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

var contacts = [bob, mary, Jim];

function printPerson(person) {
    console.log(person.firstName + " " + person.lastName);
}

function list() {
    var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        printPerson(contacts[i]);
    }
}

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName) {
    var contactsLength = contacts.length;
    for(var i = 0; i < contactsLength; i++) {
        if(contacts[i].lastName === lastName) {
            printPerson(contacts[i]);}
        else{console.log("No contacts found.");   } 
        };
        };


function add(firstName, lastName, email, phoneNumber){
    var Jim = {
        firstName: "Jim",
        lastName: "Smith",
        email: "jim.smith@ggg.com",
        phoneNumber: "01020020202"
    };
};
add();

#6

robert81 is rigth -> you have missed add function as well.
I did like this:

var bob = {
firstName: "Bob",
lastName: "Jones",
phoneNumber: "(650) 777-7777",
email: "bob.jones@example.com"
};

var mary = {
firstName: "Mary",
lastName: "Johnson",
phoneNumber: "(650) 888-8888",
email: "mary.johnson@example.com"
};

var contacts = [bob, mary];

function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}

function list() {
var contactsLength = contacts.length;
for (var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}

/*Create a search function
then call it passing "Jones"*/
var search = function(lastName) {
contactsLength = contacts.length;
for (i = 0; i< contactsLength; i++) {
if (lastName === contacts[i].lastName) {
printPerson(contacts[i]);
}
}
}

search("Jones");

var add = function (firstName,lastName,email,phoneNumber) {
contacts[contacts.length] = {
firstName : firstName,
lastName : lastName,
email : email,
phoneNumber : phoneNumber,
}
}

add("Brad", "Pitt", "braddyboy@yahoo.com", "+1 123 1324 123");

list();


#7

here:

var contacts = [bob, mary, Jim];

Jim is not yet defined at this point? Look at what code is execute up to this point:

var bob = {
    firstName: "Bob",
    lastName: "Jones",
    phoneNumber: "(650) 777-7777",
    email: "bob.jones@example.com"
};

var mary = {
    firstName: "Mary",
    lastName: "Johnson",
    phoneNumber: "(650) 888-8888",
    email: "mary.johnson@example.com"
};

anything about Jim? Not yet. So js can't know what Jim is, so it throws an error

Where do we define Jim? in the add function, so it is the task of the add function to add Jim to the array, see the example in the exercise on how they push something into the array. (you are free to use .push() as well, which you learned earlier)


#8

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