This forum is now read-only. Please use our new forums at discuss.codecademy.com.

249 points
8caf75b02552ae5a09b5e71f67ccedf9?s=140&d=retro
Submitted by
MegMartin
almost 4 years ago

Too much output

Guys,

My code is proving to pass on to the next level. however, in the console window, there seems to be more output than i expected. I was expecting the two names and an address for one (twice because of console.log and return). but there seems to be more... see below. Any way to clean this up? and I do want to stick to using the constructer form.

var friends = new Object();
friends.bill= new Object();
friends.bill.firstName="Bill";
friends.bill.lastName="Gates";
friends.bill.number ="444-444-4444";
friends.bill.address=['One Microsoft way', 'Redwood', 'WA'];

friends.steve = new Object();
friends.steve.firstName = "Steve";
friends.steve.lastName = "Jobs";
friends.steve.number = "333-333-3333";
friends.steve.address=['Two infinite loop', 'Bentown', 'DV'];

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

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

my output basically has two full contact info for steve (i was expecting one) and one for bill (I wasn't expecting bill) and then just steve's address (this part is completely confusing)


2 votes

permalink

@MegMartin,
A console.log(friends); gives:

{ bill: 
   { firstName: 'Bill',
     lastName: 'Gates',
     number: '444-444-4444',
     address: [ 'One Microsoft way', 'Redwood', 'WA' ] },
  steve: 
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: '333-333-3333',
     address: [ 'Two infinite loop', 'Bentown', 'DV' ] } }

keep repeating these two lines
An object has one or more properties seperated by a comma-,
Each property consists of a property-key and it's associated value

Description of the friends object.
The friends object has 2 properties and are seperated by a comma-,

there is a bill property with property-key bill and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)
there is a steve property with property-key steve and it's associated object value
(this associated object has 4 properties, the property-keys being firstName, lastName,number and address)

access via dot-notation
friends.bill ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends.bill.lastName ==> you will get the associated value of the lastName property-key

access via the square-bracket-notation
1 using the literal property-key
friends["bill"]
friends["bill"]["lastName"]

2 using the property-key by reference (=== via a variable )
var propertyKey1 = "bill";
var propertyKey2 ="lastName";
friends[propertyKey1] ==> you will get the associated value of the bill property-key, thus
you get the object with 4 properties

friends[propertyKey1][propertyKey2]

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago

1 Comment

654cdc46ba015075a89411d98536bb18?s=140&d=retro victorg60 almost 4 years ago

Great explanation Leon, thank you!


1 vote

permalink

@MegMartin,
How did you call the search function and the list function ???.

The secondary output is seen if you use the return-statement....
Try it... leave out the return-statement.

I added following code:

console.log(friends);
console.log("====================");
list(friends);
console.log("==============")
search("Bill");

and got following output:

{ bill: 
   { firstName: 'Bill',
     lastName: 'Gates',
     number: '444-444-4444',
     address: [ 'One Microsoft way', 'Redwood', 'WA' ] },
  steve: 
   { firstName: 'Steve',
     lastName: 'Jobs',
     number: '333-333-3333',
     address: [ 'Two infinite loop', 'Bentown', 'DV' ] } }
====================
bill
steve
==============
{ firstName: 'Bill',
  lastName: 'Gates',
  number: '444-444-4444',
  address: [ 'One Microsoft way', 'Redwood', 'WA' ] }
=> { firstName: 'Bill',
  lastName: 'Gates',
  number: '444-444-4444',
  address: [ 'One Microsoft way', 'Redwood', 'WA' ] }

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago

1 Comment

8caf75b02552ae5a09b5e71f67ccedf9?s=140&d=retro MegMartin almost 4 years ago

Thanks, for some reason... separating with the equal signs worked magic in cleaning up my output. however, I don't think i understand how the search and list funtions work... care to elaborate?


0 votes

permalink

@MegMartin,
What we have in this exercise:

  1. Building an object using the constructor notation (in contrast to the (literal notation)
  2. Accessing the friends object, using the "bill" and "steve" property-keys
  3. The buidling of a function construct.
  4. The usage of a for-in-loop method

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago


0 votes

permalink

@MegMartin,

Building an **object** using the **constructor notation** (in contrast to the _(**literal notation**)_

The Basics:
An object has one or more properties separated by a comma-,
Each property consists of a property-key and it's associated value

var friends ={ name: 'shantanu' , contact:'9557711991' };
The friends object has 2 properties:

  1. a name property with property-key name and a string value 'shantanu'
  2. a contact property with property-key contact and a string value '9557711991'

You can either create an object using the literal notation as was done
defining the object friends.

OR

You create an object using the constructor notation.
first you create an empty Object using the following syntax
either with
var friends = new Object();
OR
var friends = {};
THEN
you add the properties by using the following syntax
friends.name = 'shantanu';
friends.contact = '9557711991';
THUS

var friends = {};
friends.name = 'shantanu';
friends.contact = '9557711991' ;
console.log( friends );

will give output:

{ name: 'shantanu', contact: '9557711991' }

You should also familiarize yourself with following syntax
In the constructor notation you can
add a property
EITHER
the object-name dot literal property-key equal-sign associated value
friends.email = "[email protected]" ;
OR
the object-name squarebracket-open the property-key as string literal squarebracket-close equal-sign associated value
friends["email"] = "[email protected]" ;
OR
using variables

var thePropertyKey = "email";
var theAssociatedValue = "[email protected]";
friends[thePropertyKey] = theAssociatedValue;
console.log( friends );

will give the output:

{ name: 'shantanu', contact: '9557711991', email: '[email protected]' }

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago


0 votes

permalink

@MeggMartin,

The buidling of a function construct.

var search = function (parameter) {
             // the function-body
             //parameter can be seen as a VARIABLE,
             // which will get a VALUE as the function is called.
    console.log("hello the VALUE of parameter was: " + parameter);
             //end of function-body
    }

Now if you are going to call the function
you will have to give it an so-called argument.
search("abrakadabra");
will give:
hello the VALUE of parameter was: abrakadabra

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago


0 votes

permalink

@MeggMartin,

The usage of a for-in-loop method

The for-in-loop will iterate over all the 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:
Thus for (var aPropertyKey in friends)
will lead to 2 iteration's
iteration-1 var aPropertyKey = "bill";
iteration-2 var aPropertyKey = "steve";

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

will return:
bill
steve

  for (var aPropertyKey in friends) {
       console.log( friends[aPropertyKey].lastName );
    }

would return:
Gates
Jobs

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago


0 votes

permalink

@MeggMartin,
The LIST function

1 Create a function list that takes a single parameter. (which will contain the object)
2 In the body of the function, write a for/in loop.
3 In the loop, use console.log to print out the key. (For example, if you only have bill and steve as entries, list should just print out "bill" and "steve".) (thus aPropertyKey)

  var list = function(theObject) {
      for (var aPropertyKey in theObject ) {
           console.log( aPropertyKey );
     }
  };  //closing function-body

calling the function with the argument friends
list(friends);
will give
bill
steve

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago

1 Comment

52f35131631fe9dbbe0025d2_98570290 Martin over 3 years ago

Thank's for the help, Leon!!


0 votes

permalink

@MeggMartin,
The SEARCH function

Define a function search that takes a single argument, name.
function declaration with 1 parameter, - name -
If the argument passed to the function matches any of the first names in friends,
if ( friends[aPropertyKey].firstName === name)
it should log that friend's contact information to the console and return it.
a console.log() AND a return of the associated-object-value of aPropertyKey
friends[aPropertyKey]

 var search = function(name) {
          for (var aPropertyKey in friends) {
              if ( friends[aPropertyKey].firstName === name) {
                  console.log(friends[aPropertyKey] );
                  return friends[aPropertyKey] ;
             }
          }
    }; // end of function-body

So when we -call- the search function the argument can be "Bill" or "Steve"
search("Bill");
will give output

{ firstName: 'Bill',
  lastName: 'Gates',
  number: '444-444-4444',
  address: [ 'One Microsoft way', 'Redwood', 'WA' ] }
=> { firstName: 'Bill',
  lastName: 'Gates',
  number: '444-444-4444',
  address: [ 'One Microsoft way', 'Redwood', 'WA' ] }

As you are using the return-statement you could also capture the
return VALUE in a VARIABLE like:
var theResult = search("Bill");
You will get the console.log()-output on your console....
and you have the search result in a variable theResult,
which you could use to do specific processing.

3701 points
5334fc819c4e9db930001a0d_32139218
Submitted by
Leon
almost 4 years ago

2 Comments

Picture Sione Inoke almost 4 years ago

I'm so confused with how this works... How does it log and return the friend object with

console.log(friends[aPropertyKey]);
return friends[aPropertyKey];

When I read it back I read it like it's only going to give me one of the properties of the object, not the entire object itself. Also, is there a difference with attributes and properties?

Picture Sione Inoke almost 4 years ago

okay, I think I've answered my question and it has to do with the for/in loop. I'm still a little hazy on it, though.