27/33 - Loop the loop


#1

For some reason this looks correct to me but the error I'm receiving is:

TypeError: Cannot read property 'name' of undefined in the output box.

As well as:

Oops, try again. It looks like the object at index 0 was not created with the Person constructor

// Our Person constructor
function Person(name, age) {
    this.name = name;
    this.age = age;
}
// Now we can make an array of people
var family = new Array();
family[0] = Person("alice", 40);
family[1] = Person("bob", 42);
family[2] = Person("michelle",8);
family[3] = Person("timmy", 6);
// loop through our new array
for (var i = 0; i < 4; i++) {
    console.log(family[i].name);
}


#2

You only use name in one place which tells you that family[i] is undefined and so you would need to consider if that really evaluates to what you want (check that index is valid, and check what the array contains, you can do both of those by printing, and whichever is wrong you repeat the process of considering how it should have been obtained)


#3

Doesn't family[i].name iterate through each name in the object, per the array?

ex:

family[0].name = alice
family[1].name = bob

And so on...


#4

Not quite sure I am following you, but I changed the for loop to the below, and still gives me the same error which is odd. It's exactly the same code as every other solution listed on the discussion board.

for (var i = 0; i < family.length; i++) {
console.log(family[i].name);
}


#5

Don't guess, find out. Print out the indexes you use, the length of the array, and the array itself

If those are all correct then you wouldn't have this problem. Therefore, one of them isn't correct and you should look at them

"this is what should be happening, let's have a look if that is happening"

To debug something you start by establishing what should happen, then follow up by verifying whether it is. And if you don't know what should be happening then you couldn't have written the code in the first place and would have to re-consider what you're doing at all and would need to read about what's being done / break it up into trivial parts and debug them individually


#6

I appreciate the help but you're not really providing where I am going wrong here, as I am literally doing everything the lesson has taught. The lesson hint even says: arrayName[2].propertyName; which is what family[].name is, within the for loop. Everything else is correct.

So what's the code supposed to actually look like? Every other time this question has been posed the above code is the proper code.


#7

You're getting the value undefined from somewhere, yes?
You know that you are printing it in a particular location.
So you'd think about where it should be getting its information, check that it's from the right place, and if it is, check where that came from, and so on until the mix-up is found.
I keep suggesting you print out a few things to see what they are, have you?


#8

At this point I just need to see the correct code so I can learn from it.

Thanks for your help though.


#9

There's nothing to learn from the correct code when the problem is debugging.
You'll always make mistakes and you can't just always get it right so that you don't have to investigate something that's broken.

Assuming that you're using the correct indexes, that must then mean that the values in the array are wrong, because that is where you get your undefined's from.
Look at that array, print it. What should it contain, and what does it contain?

If you have a bowl of balls where all the balls are supposed to be red.
And you grab a ball from it, but it's blue.
What can you then conclude is wrong about the content of the bowl?
That's exactly what's going on in your code, and the very same logic is applied.
So you might look in that bowl and say, yeah, those are indeed blue, and then you might think about how they got there.


#10

I see that it should be

family[0] = new Person (name, age)

Somehow I forgot about that when creating custom constructors. Now it's working!


#12

hope this helps...
// Our Person constructor
function Person (name,age){
this.name = name;
this.age = age;
}

// Now we can make an array of people
var family = new Array();
family[0] = new Person("alice", 40);
family[1] = new Person("bob", 42);
family[2] = new Person("michelle", 8);
family[3] = new Person("timmy", 6);

// loop through our new array
for(var i = 0; i<family.length;i++){
console.log(family[i].name);
}


#13

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