Hi I am not able to find out last name using "search" function


#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];

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 {
if(contacts[i].lastName == lastName)
{
printPerson(contacts[i].lastName);
}
}
}
search("Jones");


#2

looks like your for loop is missing the closing parenthesis and a lot of stuff in side of the parentheses. Also the printPerson function just needs to be called on contacts[i], not on the last name. for checking equivalence I also used three equal signs (===) but I think it might also work with just two like you have.


#3

@vedant591, just wanted to tack to @digitalcoder99109's answer to elaborate on == vs ===... In this case, as Keigh predicts, your program will probably work correctly with two; generally I believe you'd best use three.
If I understand correctly, when you compare two things with == and they are not the same type (for example a string and a number), one of them will be forced to change its type; how these comparisons will behave is not always immediately clear and are therefore best avoided.