Serach for a friend


#1



Unit 7, Search for a friend


my code says "Oops, try again. It looks like your search function doesn't return contact information for Steve."

can someone elaborate or check my code and reply with the answer and the best code,

thank you


Replace this line with your code. 

var friends = {
    Bill: {
        firstName: "Bill",
        lastName: "Gates",
        number: "0506625295",
        address: ['One Microsoft way', 'Redmond', 'WA', '98052']
    },
    
    Steve: {
        firstName: "Steve",
        lastName: "George",
        number: "0506625294",
        address: ['One Microsoft way 6', 'Redmond 7', 'CA', '980566']
    }
};
    
var list = function (friends){

for(var firstName in friends){
 console.log(firstName);
 }
 };
 list(friends);
   
var search = function(friends) {
    for (var firstName in friends) {
        if (friends.firstName === name) {
            console.log(friends.firstName);
            return friends.firstName;
        }
        else {
            return "Sorry!";
        }
    }
};
search("Steve");


#2

you have taken same name for prameter and object

here

var friends // varible name friends
and
var list = function (friends) //parameter name friends

change it :slight_smile:


#3

var friends = {
Bill: {
firstName: "Bill",
lastName: "Gates",
number: "0506625295",
address: ['One Microsoft way', 'Redmond', 'WA', '98052']
},

Steve: {
    firstName: "Steve",
    lastName: "George",
    number: "0506625294",
    address: ['One Microsoft way 6', 'Redmond 7', 'CA', '980566']
}

};

var list = function (friends){

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

var search = function(name) {
for (var firstName in friends) {
if (friends.firstName === name) {
console.log(friends.firstName);
return friends.firstName;
}
else {
return "Sorry!";
}
}
};
search("Steve");

Still I got the same message


#4

it should be because
friends.firstName== Bill & Steve not just steve
here firstName is just a name of a variable. it doesn't mean its the firstname of an object.

you have to do

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


#5

relaxed:

got it thanks :slight_smile:


#6

Hi all, join the conversation because I quite don't get it though the code seems ok..
Can you spot what is wrong please?

` var friends={};

friends.bill={
firstname: "Bill",
lastName: "Gates",
number:"869117",
address:['via Div Julia', '10']
};

friends.steve={
firstName: "Steve",
lastName:"Jobs",
number:"869210",
address:['P.zza Monumento', '32']
};

var list=function(friends){
for(var firstName in friends){
console.log(firstName)
}
};

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

list(bill);
search("Steve"); `


#7

@giuliocipone,

Have a close look at

firstname: "Bill",


#8

@giuliocipone

+++++ nyc object and the for-in loop explained ++++++++++++++++

T h e - B a s i c s

An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated VALUE

var nyc = {
       fullName: "New York City",
       mayor: "Bill de Blasio",
       population: 8000000,
       boroughs: 5
      };
nyc -object- S p e c i f i e d

The nyc object has 4 properties seperated by a comma-,
- a fullName property with property-key fullName and it's associated string value of "New York City"
- a mayor property with property-key mayor and it's associated string value of "Bill de Blasio"
- a population property with property-key population and it's associated number value 8000000
- a boroughs property with property-key boroughs and it's associated number value 5

for - in - loop - e x p l a i n e d

With the for-in-loop you have a Method
which will iterate over all properties of a given object.
At each iteration it will assign the property-key as a string
to a variable name of your choice.
Thus for (var x in nyc)
will lead to 4 iteration's
iteration-1 var x = "fullName";
iteration-2 var x = "mayor";
iteration-3 var x = "population";
iteration-4 var x = "boroughs";
As they want you to display the property-key and NOT it's associated VALUE
you would use
console.log( x );

If they wanted you to display the associated VALUE of the propery-key you would use
console.log( nyc[x] );


#9

@giuliocipone,

the FUNCTION talk

var myFunc = function( param1, param2) {
       //Begin of  anonymous FUNCTION-BODY
       //VARIABLE -myFunc- has an -anonymous function- assigned
       //this -anonymous function- has 2 PARAMETERS param1 and param2
       //param1 and param2 PARAMETERS are used 
       //as -local- VARIABLES throughout the FUNCTION-BODY

      console.log( param1 + " and " + param2 ) ;

      //End of anonymous FUNCTION-BODY
};

If you want to call/execute the anonymous function
you will have to add a pair of parentheses to the variable myFunc
like
myFunc();
As the anonymous function was defined
as having 2 parameters
you have to provide 2 arguments
in our case 2 string VALUES "Alena" and "Lauren"
like
myFunc("Alena","Lauren");

some quotes from the outer-world:

**argument is the value/variable/reference being passed in,
parameter is the receiving variable used within the function/block**

OR

**"parameters" are called "formal parameters",
while "arguments" are called "actual parameters".**

Your list function is defined a having 1 parameter
you will have to call the function
providing 1 argument in our case an object Value friends


#10

I am not familiar with the reference for selecting the property-value mentioned above: nyc[x] Was that introduced in an earlier lesson?


#11

@jnl2,

var james = {
    job: "programmer",
   married: false,
   sayJob: function() {
          // complete this method
          console.log("Hi, I work as a" + this.job);
    }
 };

Description of the james object.
The james object has 3 properties which are seperated by a comma-,

there is a job property with property-key job and it's associated string value 'programmer'
there is a married property with property-key married and it's associated boolean value false
there is a sayJob property with property-key sayJob and it's associated anonymous function VALUE
( they also would 'say', the james-object has the sayJob()-Method )

access via dot-notation

james.job ==> you will get the associated string VALUE of the job property-key, thus
you get the string VALUE 'programmer'

james.married ==> you will get the associated boolean VALUE false

james.sayJob ==> you will get the associated anonymous function VALUE
to call/execute this method you add a pair of parenthesis-( )
james.sayJob();

access via the square-bracket-notation

1 using the literal property-key

james["job"]
james["married"]
james["sayJob"]()

2 using the property-key by reference (=== via a variable )

var propertyKey1 = "job";
var propertyKey2 = "married";
var propertyKey3 = "sayJob";
james[propertyKey1] ==> you will get the associated string VALUE of the job property-key, thus

you get the string "programmer"

james[propertyKey2]
in this case you will get the boolean VALUE of the married property
being false

james[propertyKey3]();
would =display= "Hi, I work as a programmer"


#12

Thank you for providing that supplemental information. Dot and Bracket notation make a little more sense now.