7 Search for a friend for / in loops and search function


#1

Hi, I've got this far by searching the forum but I'm finally going to ask a question.
I have passed this and the exercise before it but I am getting pretty strange output in my logs..
I really don't understand what is going on, could someone please explain, thanks

var friends = {
bill: {
firstName:'Bill',
lastName: 'Gates',
number: 07855518701,
address: ['One High Street','Horfield','Bristol','86754']
},
steve: {
firstName:'Steve',
lastName: 'Jobs',
number: 07855518702,
address: ['One High Street','Horfield','Bristol','86754']
},
tobe: {
firstName:'Tobe',
lastName: 'Davids',
number: 07855518703,
address: ['One High Street','Horfield','Bristol','86754']
},
math: {
firstName:'Mathew',
lastName: 'Williams',
number: 07855518704,
address: ['One High Street','Horfield','Bristol','86754']
},
};

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

    THIS IS WHAT PRINTS OUT (below)

bill
steve
tobe
math
{ bill:
{ firstName: 'Bill',
lastName: 'Gates',
number: 7855518701,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
steve:
{ firstName: 'Steve',
lastName: 'Jobs',
number: 7855518702,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
tobe:
{ firstName: 'Tobe',
lastName: 'Davids',
number: 7855518703,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
math:
{ firstName: 'Mathew',
lastName: 'Williams',
number: 7855518704,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] } }
{ bill:
{ firstName: 'Bill',
lastName: 'Gates',
number: 7855518701,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
steve:
{ firstName: 'Steve',
lastName: 'Jobs',
number: 7855518702,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
tobe:
{ firstName: 'Tobe',
lastName: 'Davids',
number: 7855518703,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
math:
{ firstName: 'Mathew',
lastName: 'Williams',
number: 7855518704,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] } }
{ bill:
{ firstName: 'Bill',
lastName: 'Gates',
number: 7855518701,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
steve:
{ firstName: 'Steve',
lastName: 'Jobs',
number: 7855518702,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
tobe:
{ firstName: 'Tobe',
lastName: 'Davids',
number: 7855518703,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] },
math:
{ firstName: 'Mathew',
lastName: 'Williams',
number: 7855518704,
address: [ 'One High Street', 'Horfield', 'Bristol', '86754' ] } }

Any light on this would be much appreciated.
I only started the course three days ago so I'm quite new to it all..
Jim


#2

@electricbacfac,

1

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

2

As you defined the search function as having 1 parameter name
at the call of the search function
you will have to provide 1 argument
in this case a string Value being one of the firstName's
like

search("Mathew");

3

In your search function you use

console.log( friends );

which will display the whole of the Object friends

4

The comma-, is used as a property-separator
so remove the last comma-, at the end of your math property

5

+++ friends list and search and for-in explained.
Look here for an explanation
object
function
for-in-loop
list-function
search-function
http://www.codecademy.com/forum_questions/545275fd8c1ccc8e3e000a23#comment-5457b12c9c4e9d87cb00003f


#3

Thanks for the response Leonhard.
I've been back over the earlier exercises and now I am getting a better grip on the way things work.
1 (above) - thanks, I am using this now and it helps.

I'm starting to get the cryptic instructions a little more now. I was taking the hint instructions too literally and at times they were sending me way off track. (Don't think I'm the only one either..!)

here is my new code, I think it is correct..

var friends = {
bill: {
firstName:'Bill',
lastName: 'Gates',
number: 07855518701,
address: ['One High Street','Horfield','Bristol','86754']
},
steve: {
firstName:'Steve',
lastName: 'Jobs',
number: 07855518702,
address: ['One High Street','Horfield','Bristol','86754']
},
tobe: {
firstName:'Tobe',
lastName: 'Davids',
number: 07855518703,
address: ['One High Street','Horfield','Bristol','86754']
},
math: {
firstName:'Mathew',
lastName: 'Williams',
number: 07855518704,
address: ['One High Street','Horfield','Bristol','86754']
}
};

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

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

ALSO
I tried to set this up with OBJECT CONSTRUCTOR code but got stuck...

How far off is this?
//(There's no example to look at that I can find.)

var friends = new Object();
friends.bill = {
friends.firstName: 'bill',
friends.lastName: 'gates'
};

Thank you
Jimmy


#4

@electricbacfac aka Jimmy

##1
The associated Value of your number property-key
are still in number Format.
Even better, you start the number Values with a `zero,
which will cause, the Javascript interpreter to Interpret the numbers as so-called octal numbers
http://stackoverflow.com/questions/12888075/javascript-alert-number-starting-with-0
You really have to use the string Format !!!

##2
Creating an Object with the constructor notation

  • create an empty new-Object by using
  • var friends = new Object;
    or
    var friends = {};
  • adding the properties by using the so-called dot.notation
  • friends.bill = {};
    friends.bill.firstName = “Bill”;
    friends.bill.lastName = “Gates”;
    friends.bob = new Object;
    friends.bob.firstName=“Bob”;
    friends.bob.lastName=“Jones”;

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

nice to know

You could test in labs repl.it
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

www.crockford.com --> for most respected guide-line in Javascript
like coding convention
www.crockford.com
http://javascript.crockford.com/code.html <== Javascript code-convention
http://javascript.crockford.com/survey.html


#5

@electricbacfac,

- - - 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
a name property with property-key name and it’s associated string VALUE “FIRST”

OR

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

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

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

To create an Object,
#you can use the literal notation,
you directly create an Instance of the object, with the
properties being separated by a comma-,

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

OR
#You create an Object by the construct notation.
First you create an empty Object by way of either
myObj = new Object(); or myObj = {};
and then you attach its properties using the syntax
object-name.property-key = it’s-associated-value ;
( this.name = x ; )

thus:

  var myObj = {};
      myObj.type = 'fancy';
      myObj.disposition = 'sunny'; 

OR
#There is also the facility Class construct notation.
The name should then start with a Capital-letter.

  var Person = function( theName, theAge ) {
         this.name = theName;
         this.age = theAge;
         this.displayInstance = function() {
           console.log("The displayInstance -output-"+
                       "\n============================" +
                     "\n\t name: " + this.name +
                     "\n\t age: " + this.age);
                       };
       };
    //now create an Instance of this Class-object
  var myObj = new Person("Classy_Rocker",20);
    //call the Method displayInstance which takes NO parameters
  myObj.displayInstance();
  console.log( myObj );

you can run this in labs repl.it try it !!

As you can see i created a function within this constructor,
they now call this function a Method.
So if in near future the course is asking you to create a method you now know
that you have to create
a property-key with an associated value being a function within an Object.


#6

@electricbacfac,

++++ the object as an associative array
++++++++++++++++++++++++++++++++
They were taking a line out of the
developer.mozilla.org manual, look here subject::
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

[[ Begin of quote ]]
Properties of JavaScript objects can
also be accessed or set
using a bracket-notation. ( this in contrast to the = literal-notation = )

Objects are sometimes called associative arrays,
since each property is associated with a string value ( call it the property-key )
that can be used to access it.

Having a myCar object like:

 var myCar = {
          make: "Ford",
         model: "Mustang",
           year: 1969
        };

for example, you could access the properties of the myCar object as follows:
myCar["make"] = "Ford";
myCar["model"] = "Mustang";
myCar["year"] = 1969;
[[ End of quote ]]

The 3 possibilties to get access to the property VALUE

1 Via the so-called DOT-notation, here you use the

literal-property-key-name (NO variable-name allowed)
like:
myCar.year ==wil give you the associated number-VALUE => 1969

2 Via the so-called bracket-notation...

2a ---

We use the literal-property-key-name-as -a-string in brackets
myCar["year"] ==wil give you the associated number-VALUE => 1969

2b ---

We declare a variable which has the property-key-name-as -a-string
assigned to it... like:
**var theKey = "year";
myCar[theKey] ==wil give you the associated number-VALUE => 1969
( They also call this, getting at the -associated- VALUE by reference )


#7

Are that's were the lab is, I've ben looking for it since I got a sniff of it from reading this forum. Thanks again.
I shall be using it to test my code.

There are a lot of ways of doing this, it seems to me that the literal form is most straightforward...?

Incidentally I started reading the C programming language guide and it's god to know that JavaScript is so similar.... I really did know nothing about it 8 days ago and haven't programmed since having a ZX81 aged 11 in 81. So I am coming at this with an open mind but with two professional coder friends who encouraged me to try it..
cheers
again


#8

@electricbacfac,
You could do
https://www.codecademy.com/en/tracks/teachyourself
to get a quick overview. ( and get a feel where the weak points are )

To get an idear of a proposed coding convention ofCrockford'
read
http://javascript.crockford.com/code.html <---JavaScript coding-convention

To get a feel where the Javascript-Interpreter stands in comparison to JAVA or C
read
http://javascript.crockford.com/survey.html


a string is immutable ??
http://stackoverflow.com/questions/3200211/what-does-immutable-mean


javascript object vs hash ???
http://stackoverflow.com/questions/1143498/difference-between-an-object-and-a-hash


enumerate an object

  • mention (a number of things) one by one.
    „there is not space to enumerate all his works”
    synonyms: list, itemize, set out, give, cite, name, specify, identify, spell out, detail, particularize

google search
== the Book ==
jquery [your question] site:developer.mozilla.org
CSS [your question] site:developer.mozilla.org
javascript [your question] site:developer.mozilla.org
[your question] site:jquery.com
[your question] site:getbootstrap.com

== discussions / opinions ==
jquery [your question] site:stackoverflow.com
CSS [your question] site:stackoverflow.com
javascript [your question] site:stackoverflow.com

== guidance ==
www.crockford.com
[your question] site:crockford.com
- - http://javascript.crockford.com/code.html
- - http://javascript.crockford.com/survey.html

http://stackoverflow.com/questions/1795438/load-and-execution-sequence-of-a-web-page