27 Loop the loop


#1

<PLEASE USE THIS TEMPLATE TO HELP YOU CREATE A GREAT POST!>

What(s wrong here please ? function Person (name, age) { this.name = name; this.age = age;}

var family = new Array();

var family =[“alice”, “bob”, “michelle” ,“timmy”]
for (var i=0;i<family.length;i++ ) {
console.log(family[i].name)}
<Below this line, add a link to the EXACT exercise that you are stuck at.>

Lesson 27 Loop the Loop in Introduction to Objects I

<Below this line, in what way does your code behave incorrectly? Include ALL error messages.>
Message Syntax Error

```

Replace this line with your code.

<do not remove the three backticks above>

#2

please include the exercise url so the exercise you are at can be found


#3

Oh ! sorry
Here is the link
https://www.codecademy.com/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661
I saw different answers on CA forum , and they seem not to take into account the exercice demand to make a loop array with object constructor . That’s I post this .
thanks for your help


#4

If you have

you have defined an Array, with each item or element having a string Value.

thus if you loop over this Array,
a family[i] will return this string Value

If you want family[i].name to be valid
you should have defined the Array as having elements with an object Value
like
var family =[{name:“alice”},{name: “bob”}, {name:“michelle”} ,{name:“timmy”}];

AND

then there is the third possibility
if you defined the Array as
var family =[alice, bob, michelle ,timmy];
you would need 4 variables alice, bob, michelle and timmy
each of those variables having an associated object Value
and
each of those associated object’s having at least the name property defined.


#5

Hi Leonhard !
I vez chosen the second possibility . But I don’t understand you don’t have to to associate each var to an object value (do you?) as in this section it is learned that the object constructor gives the possibility to attribute with 1 function , here Person all value to each var ??? or am I wrong ?
as this ?

var alice=new Person( "Alice",40);
   var bob =new Person("bob",42);
var michelle =new Person("michelle",8);
   var timmy=new Person("timmy", 6);

here is the link :
https://www.codecademy.com/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661

name property defined ? not sure to understand . Means adding .name ?

thanks Leonhard


#6

There will be four objects in the array. Using your Person constructor, create the four objects and put them in the array

so now you created your 4 objects: alice, bob, michelle and tyler using the person constructor. Now we need to put our 4 objects in the array


#7

Great it worked !
family[0] = new Person(“alice”, 40);
family[1] = new Person(“bob”, 42)
family[2] = new Person(“michelle”, 8)
family[3] = new Person(“timmy”, 6);

My mistake was I didn’t redefine oject value . I thought the constructor would avoid to do so .

thanks Stetim & Leonhard a lot !
https://www.codecademy.com/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


#8

if you then store the new object in a variable first:

var alice=new Person( "Alice",40);

and then in array:

family = [alice]

or directly:

family[0] = new Person(“alice”, 40);

doesn’t really matter, both options are possible


#9

In such a case
just use a console.log() to get a better idear…


console.log( family );

#10

What you learnde upontill now…

- - - 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 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 separated 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’.

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


#11

Oh Thanks so much Leonhard for your complete explanation !

(My CA Labs doesn’t work any longer ??? I can’t reach the Labs’ page . Work in progress at the moment ? )


#12

yea, unknown what happens to labs. You can use jsbin, just ensure you use js and console tab, there is a run button in the top right


#13

The labs are going to be taken down after August 31. If you have any thing you want to save, then copy your code and save it locally. Notify me if you have any posts with links in them and send me a list. I’ll swap out the link and replace with your code.


#14

I ve stored nothing there . Thanks a lot ! heel bedankt !


#15

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