"6. We made a friend!" print out "contacts[2]" cause Security Error


#1


It's OK to search object in "contacts" list.
But there was an error occur when I want to see what I just added into.
Is there any security problem that I trying to log the contacts[2] ?

Thank you !

Error Message

Bob Jones
Dan Huang
SecurityError: Blocked a frame with origin "https://www.codecademy.com" from accessing a cross-origin frame.


Code

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"*/
function search(lastName){
    for(var i = 0; i < contacts.length; i++){
        if(contacts[i].lastName === lastName){
            printPerson(contacts[i]);
        }else{
            continue;
        };
    }
}

search("Jones");

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

add("Dan", "Huang", "dan.huang@example.com", "(650) 999-9999");
search("Huang");
console.log(contacts[2]);


#2

Try this one I got it right

function add(firstName, lastName, email, phoneNumber){
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phoneNumber = phoneNumber;

contacts[contacts.length] = this;

}

add("Dan", "Huang", "dan.huang@example.com", "(650) 999-9999");
list();


#3

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