Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Gary Christie
5,151 PointsI know my code is wrong, but help me figure this out please
This is the question I'm trying to answer(forgive me for this being so long):
We'll be creating a function that can search for people with a specific last name and print those people out with the printPerson function.
Create a function called search that takes a parameter called lastName. Leave the list function alone.
Like with the last exercise, define a variable and store the number of items in the array in it. (Since every function has its own context, or scope, you can call this variable contactsLength, too, if you like!)
Create a for loop that runs through all of the items in the array. For this step, the code for search is identical to that of list.
The twist comes here: in the body of the loop, rather than printing out every single item in the array, add an if statement that checks to see if the lastName property of the object is equal to the lastName argument.
Have the function run printPerson on the person if and only if the lastName property of the person matches the lastName argument.
At the bottom of the file, call the search function, passing in "Jones" as the last name to search for.
Here is my code, although I know that the search function is wrong. I just cant figure it out.
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]):
}
}
var search - function(lastName) {
for ( i = 0; i < contactsLength; i ++) {
if (contacts[i].lastName === lastName) {
(printPerson(person) === lastname);
}
}
}
3 Answers
Hugo Paz
15,622 PointsHi Gary,
There were a few errors with your code. I made it work. Give it a look and if you have questions feel free to ask.
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];
//I put this variable outside so all functions have access to it
var contactsLength = contacts.length;
function printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
function list() {
for ( var i = 0; i < contactsLength; i++) {
printPerson(contacts[i]);
}
}
var search = function(lastName) {
for ( i = 0; i < contactsLength; i ++) {
if (contacts[i].lastName === lastName) {
//We are moving along the contacts array, once we get a match,
//we use that position in the array to get the information we want
printPerson(contacts[i]);
}
}
};
David Tonge
Courses Plus Student 45,640 Pointsedit: Hugo Paz provided an answer for you
Gary Christie
5,151 PointsI appreciate your help. I'm having a hard time understanding printPerson(contacts[i]; The reason I'm having a hard time with that is because in the directions its says " Have the function run printPerson on the person if and only if the lastName property of the person matches the lastName argument." Maybe I'm over looking something, I don't know, but can you explain if you don't mind please
Hugo Paz
15,622 Pointsfunction printPerson(person) {
console.log(person.firstName + " " + person.lastName);
}
The function printPerson above receives an argument, which should be a javascript object.
var contacts = [bob, mary];
contacts is an array which contains 2 objects - bob and mary.
If you go into your console and type contacts[0], you will get the bob object which can be used on the printPerson function. person is just the name of the argument we pass to the function.
If you go to your console and do something like this :
printPerson({firstName:'John', lastName:'Doe'});
youll get "John Doe" on your console because you passed a javascript object "person" to the function. If you try it like this:
printPerson(contacts[0]);
youll get "Bob Jones" on your console because contacts[0] value is bob "person" object.