Javascript 5.5 finding that special someone- can't figure out whats wrong


#1

Hello.
I'm trying to figure out whats wrong with this code. It says "Oops, try again. It looks like your search function didn't log "Bob Jones" to the console. Did you remember to call search after you defined it?"

my code is here:

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

#2

You forgot the closing semicolon at the end.


#3

thanks so much for your response.
where did you mean? I have the last semicolon there after

search("Jones");

#4

Within the "search" function, you have all the closing braces but I see no semicolon anywhere. I believe you need at least one on the final closing brace.


#5

i tried running it like this but I still have the same error:

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

#6

i guess i'll ask on stack overflow. thanks anyway.


#7

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

This worked for me.


#8

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

in the for loop the < contacts.length; has to be described like this: function search(lastName) {
var contactsLenght = contacts.length;
for (var i = 0; i < contacts.length; i++) {
if(contacts[i].lastName == lastName){
printPerson(contacts[i]);
}
}
}
search("Jones");


#9

You may already have figured this out but you are so close to having it work! you have a capital letter in the var contactsLength =contacts.Length and the computer can't recognize the ".length" command if it is a ".Length" so if you change this to say

var contactsLength= contacts.length your function should work


#10

Yes, bingo:

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

#11

This is (was) not her issue. You typically do not need a closing semicolon on a function (however, I don't think it will break anything). More info here: https://www.codecademy.com/forum_questions/507f6dd09266b70200000d7e


#13

Here you go guys, hope this helps!

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