Loop for Loop


#1

https://www.codecademy.com/en/courses/spencer-sandbox/4/2?curriculum_id=506324b3a7dffd00020bf661#


Hey CC Squad, I was hoping to get feedback from you lot about the code i produced for the loop the loop exercise.

Although I got the green go ahead button I've noticed my code was different from others and just wanted a little feedback on how I could improve?

Cheers


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

// Now we can make an array of people
var family = [];

var family1 = new Person ("alice", 40);
var family2 = new Person ("bob", 42);
var family3 = new Person ("michelle", 8);
var family4 = new Person ("timmy", 6);

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


#2

This code is pretty much perfect, I personally wouldn't change it.


#3

there..

You should use indexing ..

like If I had to make an Array from scratch(of numbers)

var numbers = [];

numbers[0] = 1; 
numbers[1] = 2;

and so on..

You must have noticed that I used Bracket notation to make this numbers Array.

another thing..

for (var i = 0; i < family.length ; family++ ) family++ ?
Your iteration variable is i ?


#4

Thanks very much guys, I know you two ( @oscartbeamish & @greentreemee ) are always on it. Ok cool. I'll brush up on indexing and read up more. That threw me slightly because I've seen others have used indexing for their arrays but when I tried i was getting error signs :


#5

You can read this..


#6

@greentreemee is right indexing would be better practice, but not necessary for this exercise.

But I definitely agree with this.


#7

got it open right now, cheers for sharing the knowledge!


#8

Ahh silly mistake, I understand you, thanks again @oscartbeamish


#9

All good I think I can speak for @greentreemee and myself when saying we love to help


#10

If it did not sorted out...
Then..

hint**
There you don't need var before family[0], family[1] etc!


#11

Awesome, feel like I'm learning a good deal from you guys. Thanks again! Code is running perfectly now @greentreemee @oscartbeamish :raised_hands:


#12

Keep up the good work! :thumbsup:


#13

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