Finding that special someone


#1

Hello , the weird part is that it prints out bob when i call search on .lastName but also below i get a error TypeError: Cannot read property 'lastName' of undefined. What might be wrong here ? 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) {
var contactsLength = contacts.length;
for (i = 0; contacts.length; i++) {
if (contacts[i].lastName === lastName) {
printPerson(contacts[i]);
}
}
}
search("Jones");


#2

@validusername,

Change the FOR-loop condition of your search function
from

(i = 0; contacts.length; i++)

into

(i = 0; i < contacts.length; i++)

ALSO

use

search("Jones");
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

this will give proof
that the code-checker-software also displays its result.


#3

I'm so silly , thank you


#6