Please help


#1



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


The code should search for a lastName in the contacts, and if it finds it - give the contact's details. In this case we search for "Jones", who is one of the contacts.
But the result is the two contacts, and it plants "Jones" also in Mary's name. So, actually, it looks like it ignores the "if", and does - (contacts[i].lastName = lastName). Print it.
What's wrong there?
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 search (lastName) {
        var contactsLength = contacts.length;
    for (var i = 0; i < contactsLength; i++) {
        if (contacts[i].lastName = lastName) 
        console.log(printPerson(contacts[i]));}
        

}
search("Jones");


#2

Make sure the if statement sets contacts[i].lastName as equal value to lastName, as opposed to equals.

if (contacts[i].lastName === lastName)

Also, take a look at the console.log statement. It's logging printPerson, and the printPerson function already runs a console.log to print out first and last name. If you remove the console.log statement from around the printPerson(contacts[i]); it should just print one name.

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

}
search("Jones");

#3

Thanks so so much!
It all works!
(The =/=== is confusing. Maybe I should just put === all the time)


#4

Hey Jaels,

I used to have issues with = and === in Javascript as well!

The = is an Assignment Operator as opposed to === which is a Comparison Operator in Javascript.

The example below shows the use of a Comparison Operator in which you are seeing if x (which is assigned to 5) is equal to 5, in the if statement.

var x = 5; // Assigning x to the value of 5.
if (x === 5) { // Here you are comparing the variable x to 5.
        console.log("X is equal to 5"); // Prints if True
} else {
        console.log("X is not equal to 5"); // Prints if False
}

As displayed in our example above, var x is indeed equal to five and therefore it would display that X is equal to five.

Hope you understand it better now!


#5