27 Loop the loop



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++ ) {
<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>


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


Oh ! sorry
Here is the link
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


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
var family =[{name:“alice”},{name: “bob”}, {name:“michelle”} ,{name:“timmy”}];


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
each of those associated object’s having at least the name property defined.


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 :

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

thanks Leonhard


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


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 !


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


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

console.log( family );


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”)

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' 

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

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

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


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 ? )


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


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.


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


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