Where is Error?


#1

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,hakob];

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 a = contacts.length;
for(i=0; i < a; i++){
if(contacts[i].lastName === lastName){
printPerson(contacts[i]);
};
}
}

search("Jones","Johnson");

var add = function(firstName, lastName, email,phoneNumber){
var hakob = {
firstName: "Hakob",
lastName: "Manukyan",
email: "hakobmanukyan1999@mail.ru",
phoneNumber: "+37494160640"
}
contacts[contacts.length] = hakob;
};
add("Mary","Johnson","(650) 888-8888","mary.johnson@example.com");

Error: Oops, try again. Did you create a function called add?

We'll be creating a function that allows us to add our new friend to the address book.

Create a function called add with the parameters firstName, lastName, and email, phoneNumber.
In this new function, you want to create a new contact object like bob and mary. Instead of having this object's property values be filled with strings though, set them to the appropriate function parameters passed in.
Add this new contact object to the contacts array.
Call add with whatever first name, last name, phone number, and email arguments you like.
Make sure you call the list function, to check if your new entry is added. And delete any other function that logs output in the console, i.e 'search' function.


#2

I think it's because of this section;

It look' s like you're trying to define an object inside the "add" function.


#3

Instruction does not demand that?


#4

var contacts = [bob, mary,hakob]; // remove hakob

.

    var add = function(firstName, lastName, email,phoneNumber){
     var hakob = {
     firstName: "Hakob", // Change value  to firstName
     lastName: "Manukyan", // Change value to lastName. Same for the other values email and phonNumber
     email: "hakobmanukyan1999@mail.ru",
     phoneNumber: "+37494160640"
     }
     contacts[contacts.length] = hakob; // Looks good
    };
    add("Mary","Johnson","(650) 888-8888","mary.johnson@example.com");

Instructions

  1. In this new function, you want to create a new contact object like bob and mary. Instead of having this object's property values be filled with strings though, set them to the appropriate function parameters passed in.

It is good that you do a new object. Maybe give it a other Name like newContact :wink:


#5

They do, but you don't want them to have specifics strings attached to them yet. ex: firstName: "Hakob"

I'm a javascript noob so someone plz correct me if i'm giving bad info, but I think you want something like this;

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

That way when you call the add function and pass in perimeters, it will create a new contact. Right now when you try to call the add function and add marry, It doesn't know where to add the info. Let me know if this makes since or not Hakob!


#6

Please.write completely brother.
when I see I'll understand


#7

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

search("Jones");

var firstName = prompt("Enter First Name");
var lastName = prompt("Enter Last Nname");
var email = prompt("Enter Email Address");
var telephone = prompt("Enter Telephone Number");
add(firstName, lastName, email, telephone);

This is my working code


#8

Oops, try again. It looks like you didn't log "Hakob Manukyan" to the console. Did you remember to call list() after adding your new contact?


#9

Try adding this at the end

list();


#10

Thank you very much, it solved my problem