No. 7 Search // Function loop issue (solved, I think)


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-3bmfN/0/7?curriculum_id=506324b3a7dffd00020bf661#


After reading even more threads {y'all, I've been stuck here for a couple days!}, perhaps this is an issue with the program itself? I updated to this code below and the extra stuff is after my ==end== printout. And also the object[key] will log everything in the object; no need to log each key individually.

var search = function(name) {
for (var key in friends) {
if(friends[key].firstName === name) {
console.log(friends[key]);
return friends[key];
}
} console.log("None found");
};

search('Steve');
console.log("==end==");

My code seems to be looping over everything in my search function a couple times. And even though it is technically logging contact information for my search, the program isn't recognizing that.

Stumped on what's not working and would appreciate some guidance. How do I get it to only run the search('Steve') contact? Thanks!

Here's what prints:

None found.
Steve Smith 864-234-5678 21 Greenhouse Way,Greenville,SC
None found.
Steve Smith 864-234-5678 21 Greenhouse Way,Greenville,SC
None found.
Steve Smith 864-234-5678 21 Greenhouse Way,Greenville,SC
Bill Stewart 864.123.4567 12 Sugar St,Greer,SC
"Steve"


var friends = {
    bill: {
        firstName: 'Bill',
        lastName: 'Stewart',
        number: '864.123.4567',
        address: ['12 Sugar St', 'Greer', 'SC'],
    },
    
    steve: {
        firstName: 'Steve',
        lastName: 'Smith',
        number: '864-234-5678',
        address: ['21 Greenhouse Way', 'Greenville', 'SC'],
    }
};
    
    
//var list = function(friends){
//    for (var firstName in friends) {
//        console.log(firstName);
//    }
//};

var search = function(name) {
    for (var key in friends) {
        if(friends[key].firstName === name) {
            console.log(friends[key].firstName + " " + friends[key].lastName + " " + friends[key].number + " " + friends[key].address);
            return friends[key].firstName;
        } else {
            console.log("None found.");
        }
    }
};

search('Steve');


#2

you can't, given the exercise is doing validation. i put your code here, press the run button in the top right corner to run the code

you might want to place not found after the loop, currently not found will log every time the search isn't a match (unless that is your desired behavior)


#3

Awesome - thanks for your help, and for showing me that tool. The tweak I posted at the top worked much better. :raised_hands:


#4

did you also move not found to outside your for loop?

jsbin is just a website in which you can run simply code, the advantage of jsbin (there are many sites like this (jsfiddle, codepen)) is that it also has console window


#5

When I did the code I didn't put the search at the bottom outside the loops and I passed easily with the code looping once


#6

Yes - and it worked much better. Thanks again.


#7

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