Sum of properties of objects from array


#1

Hello
I have this script. It work with console.log but if I replace console.log with document.write the browser is white, don't work. Why?

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);
// add the last family member, "timmy", who is 6 years old
family[3]= new Person("timmy", 6);
for(i=0; i<=family.length;i++)
{console.log (family[i]);
    }
var sum=function(){
    var a=0;
    for (i=0;i<family.length;i++){
        a+=family[i].age}
        return a;
    };
console.log(sum());


#2

inside of the codecemy lessons? the preview window in the exercise is console, not a html document


#3

I start with the lesson from codec site but I try more for each script. I make some js file and html file to view the content of js file in a browser. Inside js file I use document.write. So, why is not working like console.log, this is my question.


#4

because console.log() logs to the console (a tool which browsers have, often used for debugging and used in the codecademy lessons) while document.write() writes to a html document


#5

The problem is: document.write is NOT WORKING in html document in this script!!!
Sorry for my english, I know is not too good


#6

i put yoru code in a bin, and both work fine? press the run js button to run the JS code, both the console and document show output


#7

Don't matters, is done. ; missing in my js file. THANK YOU


#8