6. List 'em all


#1



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


Oops, try again. There was a problem with your syntax.
SyntaxError: Unexpected token )

var friends = {
    bill: {
        firstName: "Bill",
        lastName: "Ahmed",
        number: 567890,
        address: ['18th Woodrush Glade', 'Livingston', 'West Lothian'], 
        },
    steve:{
        firstName: "Steve", 
        lastName: "Ahmed", 
        number: 678909,
        address: ['67th Aberdeen Place', 'Aberdeen', 'Aberdeenshire'],
        }
        
};

var list = function(friends) {
    for (var bill in friends) {
        console.log( friends[bill].firstName + " " + friends[bill].lastName + ": " + friends[bill].number);
        
    }
    
    for (var steve) {
        console.log(friends[steve].firstName + " " + friends[steve].lastName + ": " + friends[steve].number);
      
    }
};
friends(list);


#2

Your second for loop syntax is wrong. You didn't provided an array to iterate through.

for (var steve) { // here should be placed 'in ArrayName'
    console.log(friends[steve].firstName + " " + friends[steve].lastName + ": " + friends[steve].number);    
}

Rethink you function list(), function is going to log every friends element twice.

Also you are trying to call friends(list), which will throw you exception, that friends is not a function.
You probably meant list(friends).


#3

This worked for me...I hope it helps.

var friends = {
john:{
firstName: "John",
lastName: "Gates",
number: "(76838991)",
address:["onehundred greed street", "suite 16", "rio starsen", "JO","765"]
},
steve:{

    firstName:"Steve",
    lastName: "Coins",
    number: "(67345764)",
      address:["onehundred groin street", "suite 16", "south st", "JO","765"]
    },

bill:{
    firstName: "Bill",
    lastname: "Seind",
    number: "(543265734)",
      address:["one street", "suite 16", "rio grande", "JO","765"]
    }

};

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


#4

Thank you both, it works now


#5

amalakhe, that's the simple solution that was screaming to get out but it's a little deceptive. The use of 'list' as both a global and local variable makes it appear as though list pre-contains the key when actually local variable would work just as well:

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

Oh, and I don't understand the need of the semicolons in the examples either. I also like to end the brackets on the line that actually needed them last, not continue them on the next line
.
The other way is kind of like putting the period at the end of the sentence at the beginning of the next sentence, it just doesn't make sense
.
(see)


#6

if you guys could help out i'm am confused on how this worked, mainly the function with the for/in loop. i still don't really understand what its doing. also i had to come here to make it work.

code:


var friends = {};
friends.bill = {
firstName: 'bill',
lastName: 'lib',
number: '(123) 456-7890',
address: ['123 four street','las vegas','NV']
};
friends.steve = {
firstName: 'steve',
lastName: 'eve',
number: '(555) 555-5555',
address: ['555 five road','las vegas','NV']
};
var list = function(friends){
for(var key in friends){
console.log(key);
}
}
list



#7

@mathewvirgen

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".**

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

function with 1 parameter using return-statement

var myFunction = function( param1 ) {
       //Begin of FUNCTION-BODY
       //myFunction =function= has 1 PARAMETER param1
       //this param1 PARAMETER is used as a -local- VARIABLE
       //throughout the FUNCTION-BODY

      return param1;

      //End of FUNCTION-BODY
      };

you have defined a myFunction function
which takes 1 parameter param1
this param1 parameter is used
as a variable throughout the FUNCTION-BODY.

If you want to call/execute this myFunction function
and this myFunction function was defined
as having 1 parameter param1
you will have to provide 1 argument
in our case a "number VALUE" 4
myFunction( 4 );

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".**

============================================

As you are using the return-statement in your myFunction function
you will only get a return-value no-display.
You can however capture this return-value in a variable
and then use the console.log()-method to do a display.

var theResult = myFunction( 4 );
console.log( theResult );

OR directly

console.log( myFunction( 4 ) );

#8

@mathewvirgen
With

var list = function(friends){
   // friends is used as a PARAMETER
   // the =friends= PARAMETER is used as a =local= VARIABLE
   for(var key in friends){
       console.log(key);
 }
}

you have defined a list =function= which takes 1 parameter friends
this friends parameter is used as a =local= variable in the list =function=

Now if you want to call the list =function=
list(); ( you add a pair of parenthesis-( ) to express execute the =function= )
and as the list =function= was defined as having 1 parameter
you will have to provide 1 argument
in this case the friends variable with its associated object Value
like

list(friends);

I agree that this is abit confusing as you are using friends as a parameter of a function
and as a variable........

You could have defined the list function as

var list = function(param){
   // param is used as a PARAMETER
   // the =param= PARAMETER is used as a =local= VARIABLE
   for(var key in param){
       console.log(key);
 }
}
list(friends);

In some sections of this course, the course-checker Display's data
this you could then identify by placing two EXTRA code-line's:
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

Therefor i would suggesst you use

list(friends);
console.log( "== My End ==" );
console.log( "== All that is displayed now, is the course-checker ==" );

#9

thanks that helped out. but how dose it know to take the info from the friends object. thats really what I am not getting.

var list = function(object){
    for(var key in object){
        console.log(key);
    }

console.log(list(friends));
or is it here ^ that is where it happens... sweet I think I just got it.

thanks for the help @leonhard.wettengmx.n


#10

@mathewvirgen,

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] );


#11

oh boy. well thanks man, i'll have to keep looking over these. :+1:


#12

@mathewvirgen

james job married explained

- - - B a s i c s - - -

Each Object has one or more properties.
Each property consists of a property-key and it's associated value.

 var object1 = {
              name: "First"
              }

So object1 has 1 property with
a type property-key name
and it's associated value "FIRST" (which in this case is a "string-value")
OR

var myObj = {
         type: 'fancy',
         disposition: 'sunny'
        }

myObj has 2 properties seperated by a comma-,,
a property with property-key type and an associated-string-value 'fancy'
a disposition-property with property-key disposition and
..an associated-string-value 'sunny'.

= = = = = = = = = = = = = = = = = = = = = = = = = = = =

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"

+++++++ this explanation +++++++++++
I defined an extra property in the james object.
in this case a method
as there is a property-key sayJob2
with it's associated value being a function...

var james = {
    job: "programmer",
    married: false,
    sayJob: function() {
              // no PARAMETER used
              // job-Value is picked up from within this Object
              console.log("Hi, I work as a" + " " + this.job);
    },
    sayJob2: function(job) {
              // 1 PARAMETER is used as =local= VARIABLE
              // job will get the  Value as you call providing 1 ARGUMENT
              console.log("Hi, I work as a" + " " + job);
    }
 
 };

 james.sayJob();

 james.sayJob2("xx");

An object has one or more properties separated by a comma-,
A property consists of a property-key and it's associated value.
The associated value can be anything
from an Array [] , null, "string", number, true , _an Object {} or a function.

If the associated value of a property-key is a function
we are calling this property a METHOD.
(if in the course they ask you to create a method you will now know that
you have to create a function within an object using the function-name
as property-key)

Normally if you are using the this keyword in a Method,
you are referring to the property of the object of which it is part of.

So in our case:
james.sayJob();
would translate to
console.log("Hi, I work as a" + " " + "programmer");

A good article about this:
http://stackoverflow.com/questions/80084/in-javascript-why-is-the-this-operator-inconsistent


#13

My code is running well , but i don't how this portion list = function (object)
{
for(var key in object)
{
console.log(key);
}
}
works . Can anybody help me in this?

var friends= {
bill:{
firstName: "Bill",
lastName: "Gates",
number: "9312011430",
address:['c','59','new panchvati']
},
steve:{
firstName: "Steve",
lastName: "Johnson",
number: "931124430",
address:['China','56' ,'road']
},
};
var list = function (object)
{
for(var key in object)
{
console.log(key);
}
}


#14

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

I don't understand how the for loop works?


#15

@aparnavarshney
@codecrcker
Please read
https://discuss.codecademy.com/t/6-list-em-all/43873/10?u=leonhard.wettengmx.n


#16

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