Finding that Special Someone


#1



My code succeeds and prints both full names. However I thought it should only print the full names where the last name matches "Jones". Thus NOT printing Mary Johnson.


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

search("Jones");


#2

the outputs you see are caused by the console.log on line 33:

for (var i = 0; i < contactsLength; i++) {
		printPerson(contacts[i]);

if you remove that console.log:

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

you will see no output, this is telling us something is wrong with your if condition, since there is a console.log, but nothing is logged, so if doesn't get evaluate to true, while this should happen if we search for Jones

Also, closing curly brackets of if and for shouldn't have semi-colons


#3

Thank you... however I have determined my issue appears to be that I forgot the "else" part of my if statement. I added:

} else {
		    break
		}

and it only prints Bob Jones as expected now.

(And I removed my incorrect semicolons.)


#4

are you sure that is the issue? break will break the loop, if the only change you made is adding else, when you now search for Johnson, still only bob jones will be logged to the console


#5

Correct, back to the drawing board...


#6

read my original answer again, if you need more help post an updated version of your code


#7

Thanks, I removed that line 33 and now each name returns as expected when passing their last names.


#8

so know it works as expected? Are you sure? If you want me to verify, provide me with an updated version of your code


#9

Well for those 2 names it seems to work, for anything else it returns nothing. Here is the 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 (var i = 0; i < contactsLength; i++) {
		if(contacts[i].lastName === lastName) {
		    printPerson(contacts[i])
		}
	}
}

search("Jones");

#10

this works as expected, well done :slight_smile:


#11

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