Computer not reading code!


#1


https://www.codecademy.com/courses/building-an-address-book/0/6?curriculum_id=506324b3a7dffd00020bf661#


For some reason, the computer cannot read my object variable. the console says: "ReferenceError: Can't find variable: yt."
The level says:"Oops, try again. Make sure the contacts array still exists. Your code threw the following error when checking contacts: TypeError: undefined is not an object (evaluating 'contacts.length')"
I don't understand why it's not working!


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"
};

function add(firstName, lastName, email, phoneNumber){
    var yt = new Object();
    yt.firstName = add.firtName;
    yt.lastName = add.lastName;
    yt.email = add.email;
    yt.phoneNumber = add.phoneNumber;
};

var contacts = [bob, mary, yt];

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"*/
function search(lastName){
   var contactsLength = contacts.length;
   for (var i = 0; i < contactsLength; i++) {
       if (lastName = contacts[i].lastName);
       printPerson(contacts[i]);
   };
   }
   
 add("Steve", "Jobs", "stevejobs@gmail.com", " " )
 list();


#2

Use contacts[contacts.length] instead var yt = new Object();

Do it like this:

contacts[contacts.length] = {
firstName: firstName,
lastName: lastName,
phoneNumber: phoneNumber,
email: email
};
(Assuming you defined the add function with the parameters firstName, lastName, phoneNumber, and email.)

Function add should be:

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

Then look here:

function search(lastName){
   var contactsLength = contacts.length;
   for (var i = 0; i < contactsLength; i++) {
       if (lastName = contacts[i].lastName); // wrong operator: use = instead === and remove ;
          printPerson(contacts[i]);
   };
}

and here:

var contacts = [bob, mary, yt];

yt created inside function add, outside fuction it doesn't exist. should be:

var contacts = [bob, mary];

EDITED.

 if (lastName = contacts[i].lastName)

It's if statement. Syntax:

if(condition) {
  // code;
}

#3

I don't understand where those are supposed to go


#4

Usually when we start a lesson from the beginning, read the lesson and instructions, and follow the instructions, we progressively get to know and understand what our code is expected to do. What we see here is somebody trying to understand the errors in code that was copied and pasted. We can help with the solution, but we can not mitigate the unfortunate ignorance and confusion that results. The best thing I can suggest is start the unit over from the very beginning, and take the time to complete each step, including review and further background study.


#5

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