After reading several posts on #7/Search for a Friend, still need help


#1



With this code, I produce no error message, but it isn't correct. The console log produces Bill while I am searching for only "Steve." When I see other people's code and solution, (like the one I use below) they say these codes work, but they don't mention why Bill comes up when you're searching for Steve. (I tried my own code, then I tried various codes I found in various forums)

Also I haven't found a good explanation that clarifies why Steve shows up 4 times. Does anyone know?

Thank you in advance!


    /*object literal*/
var friends = {
    bill:{
        firstName:"Bill",
        lastName:"Cunningham",
        number:"(205)739-8324",
        address:['One Micro Street','Seattle', 'WA']
        },
    steve:{
        firstName:"Steve",
        lastName:"Hamn",
        number:"(205)739-9999",
        address:['Two Micro Street','Seattle', 'WA']
        },
    };
    
var search=function(name){
    for(var look in friends){
        if(friends[look].firstName===name){
            console.log(friends[look]);
            return friends[look];
            }
        }
    
    };
    
    search("Steve");

Console Log Result:

{ firstName: 'Steve',
lastName: 'Hamn',
number: '(205)739-9999',
address: [ 'Two Micro Street', 'Seattle', 'WA' ] }
{ firstName: 'Steve',
lastName: 'Hamn',
number: '(205)739-9999',
address: [ 'Two Micro Street', 'Seattle', 'WA' ] }
{ firstName: 'Steve',
lastName: 'Hamn',
number: '(205)739-9999',
address: [ 'Two Micro Street', 'Seattle', 'WA' ] }
{ firstName: 'Bill',
lastName: 'Cunningham',
number: '(205)739-8324',
address: [ 'One Micro Street', 'Seattle', 'WA' ] }
{"firstName":"Steve","lastName":"Hamn","number":"(205)739-9999","address":["Two Micro Street","Seattle","WA"]}


#2

yea, the checking script calls some of the function for validation purposes, but due to console.log() this will show up in your console window, i put your code in a bin:
link to bin

there it only shows what you do


#3

Thank you. I got my code to work using this too but I don't understand it. Two questions about the code below:

1) In the search function below, I don't understand why it's acceptable to define "look" inside the if-statement. There is no key look in the property friends and no global variable look, so how does the program know to access the firstName key within friends? What does friends[look].firstName mean on its own?

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

2) I tried to make this an if-else statement to accommodate a response for a name that wasn't in the array but the program would not run that way. Any suggestions on why not?

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "555-555-5555",
        address: ['One Microsoft Way','Redmond','WA','98052']
        },
    
    steve: {
        firstName: "Steve",
        lastName: "Jobs",
        number: "555-555-5555",
        address: ['34 Apple St','Palo Alto','CA','90214']
        },
        
    max: {
        firstName: "Max",
        lastName: "Hunter",
        number: "555-555-5555",
        address: ['1122 Boogeywoogie Ave','Scaresville','TX','55654']
        },
        
        
    lucy: {
        firstName: "Lucy",
        lastName: "Chambers",
        number: "555-555-5555",
        address: ['1 Pine Lane','Philadelphia','PA','11912']
        }
    
};


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]);
        return friends[key];
    
    } else {
        return "Name not found";
    };
    };
    };


search("Steve");

#4
  1. because of your for loop:

for(var look in friends)

look will now hold each property inside friends:

for(var look in friends){
   console.log(look);
}

so friends[look] will access the objects nested inside friends.

  1. see the changes i made here, a function ends the moment a return keyword is reached, which happened in the first run of the loop, i placed return not found outside the loop, so that only happens after the loop has finished running

#5

Hi Stetim94-

So the box (what you call the "checking script"?) on the right in codecademy is not necessarily the console. (please confirm)

And if we insert our code into sites like codepen or JS Bin, we can see the true outcome? I must have missed that, it's just that this is the first exercise where I ran into this problem.

To be honest I don't know what you meant in your last sentence, "there it only shows what you do" - can you clarify?
'There' as in JS Bin?


#6

the window on the right is the console. the checking script as i call it is official called SCT (submission correctness test), it is not visible to users. (so, not the same as the console

yes, since sites like jsbin/codepen don't have a exercise to validate, the output is only what you log to the console


#7

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