0

enter image description here

Can someone help me fix my thousandTrees() function, it displaying the console log that i want, but it is also returning undefined on the console.

How do i make the function just do the console log?

this is part of an exercise from a course i am following.

The function is just at the bottom of the code

thank you

// park constructor
class Park {
    constructor (name, buildYear, trees, size) {
        this.name = name;
        this.buildYear = buildYear;
        this.trees = trees;
        this.size = size;
    }

    // number of tree / park area
    treeDensity() {
        return this.trees / this.size;
    }

    // return the park age
    parkAge() {
        return new Date().getFullYear() - this.buildYear;
    }
}

// street constructor
class Street {
    constructor (name, buildYear, size) {
        this.name = name;
        this.buildYear = buildYear;
        this.size = size;
    }
};


// all parks

let allParks = [
    park1 = new Park("Brighton Park", 1900, 1200, 3),
    park2 = new Park("Worthing Park", 1800, 650, 2),
    park3 = new Park("Shoreham Park", 1850, 350, 2)
]
// all streets

let allStreets = [
    street1 = new Street("Brighton Street", 1858, 2000),
    street2 = new Street("Worthing Street", 1950, 1200),
    street3 = new Street("Shoreham Street", 1850, 800),
    street4 = new Street("Lancing Street", 1980, 760)
];

// calculate the average age of all parks
let parkAgeAvg = function() {
    return (park1.parkAge() + park2.parkAge() + park3.parkAge()) / 3
};
// Display the name of the park with more than 1000 trees
let thousandTrees = function() {
    for (let cur of allParks) {
        if (cur.trees >= 1000) {
          console.log(`${cur.name} has more than 1000 trees, we have verified there is actually ${cur.trees} trees in total.`);
        }
    }
};

// total length of streets and avg length

//size classification of all streets
Jacob
  • 77,566
  • 24
  • 149
  • 228
Nuno Carro
  • 75
  • 1
  • 8
  • What do you want it to return? Also, your indentation is very deceiving. – Aplet123 Mar 25 '20 at 21:05
  • Are you sure it's the code you posted that's logging the `undefined`? Anything else present on the page? – Jacob Mar 25 '20 at 21:05
  • I think the problem is in instance creation. You store it in array but your code does not initialize it. – Salman Ahmed Mar 25 '20 at 21:07
  • I dont really want to return anything, just want a function that when called, looks within the array for any property of trees that has 1000 or more and console log it. I am getting really confused at the moment as i am trying to rush through the course and i know there is some very basic error i am doing here, just dont know what! – Nuno Carro Mar 25 '20 at 21:09
  • salman ahmed, i am just calling it in the console for the moment for testing only – Nuno Carro Mar 25 '20 at 21:10
  • for (let cur of allParks) { after this line console.log(cur) – Mr Khan Mar 25 '20 at 21:13
  • I don't see where you defined variables park1, park2 and park3 , but you have these names in array ? – Arturas Mar 25 '20 at 21:17
  • That is not an error. Without going into too much detail about the inner workings, that is how Chrome console works. The extra 'undefined' line is expected. – tlong314 Mar 25 '20 at 21:23
  • Thank you tlong314 i will ignore it for now then :p – Nuno Carro Mar 25 '20 at 21:25
  • No problem. Here is another reference with some more info: https://stackoverflow.com/questions/14633968/chrome-firefox-console-log-always-appends-a-line-saying-undefined – tlong314 Mar 25 '20 at 21:25

3 Answers3

0

In chrome I'm not seeing any errors in the console.log

// park constructor
class Park {
    constructor (name, buildYear, trees, size) {
        this.name = name;
        this.buildYear = buildYear;
        this.trees = trees;
        this.size = size;
    }

    // number of tree / park area
    treeDensity() {
        return this.trees / this.size;
    }

    // return the park age
    parkAge() {
        return new Date().getFullYear() - this.buildYear;
    }
}

// street constructor
class Street {
    constructor (name, buildYear, size) {
        this.name = name;
        this.buildYear = buildYear;
        this.size = size;
    }
};


// all parks

let allParks = [
    park1 = new Park("Brighton Park", 1900, 1200, 3),
    park2 = new Park("Worthing Park", 1800, 650, 2),
    park3 = new Park("Shoreham Park", 1850, 350, 2)
]
// all streets

let allStreets = [
    street1 = new Street("Brighton Street", 1858, 2000),
    street2 = new Street("Worthing Street", 1950, 1200),
    street3 = new Street("Shoreham Street", 1850, 800),
    street4 = new Street("Lancing Street", 1980, 760)
];

// calculate the average age of all parks
let parkAgeAvg = function() {
    return (park1.parkAge() + park2.parkAge() + park3.parkAge()) / 3
};
// Display the name of the park with more than 1000 trees
let thousandTrees = function() {
    for (let cur of allParks) {
        if (cur.trees >= 300) {
          console.log(`${cur.name} has more than 1000 trees, we have verified there is actually ${cur.trees} trees in total.`);
        }
    }
};

thousandTrees();
DCR
  • 14,737
  • 12
  • 52
  • 115
0

The undefined you're seeing is just because the function you're calling in the console returns undefined, and any JS you evaluate in the console shows the value of the expression. It has nothing to do with the console logging code.

Side note, there's a problem in how you're initializing things:

let allParks = [
    park1 = new Park("Brighton Park", 1900, 1200, 3),
    park2 = new Park("Worthing Park", 1800, 650, 2),
    park3 = new Park("Shoreham Park", 1850, 350, 2)
]

In some modes, JavaScript would choke on this, because park1, park2, and park3 are undefined. I believe what you actually want is just:

let allParks = [
    new Park("Brighton Park", 1900, 1200, 3),
    new Park("Worthing Park", 1800, 650, 2),
    new Park("Shoreham Park", 1850, 350, 2)
]

In the former, you're populating the array with the results of assignment expressions. In the second, you're populating it with constructed objects.

Same feedback for allStreets.

Jacob
  • 77,566
  • 24
  • 149
  • 228
  • 1
    Try putting "use strict"; on top of script and you will see errors of undefined variables. It is bad practice to use values in array like that. – Arturas Mar 25 '20 at 21:27
  • Thank you guys, you are correct about this. In the first place i didnt have them inside an array , but thoough i would then put them inside an array so i could iterate over them Will fix the issue, but haven't seen any bugs yet? how come? Thank you – Nuno Carro Mar 25 '20 at 21:29
0

Define variables which ones you use in arrays:

// all parks
let park1,park2,park3;

let allParks = [
     park1 = new Park("Brighton Park", 1900, 1200, 3),
     park2 = new Park("Worthing Park", 1800, 650, 2),
     park3 = new Park("Shoreham Park", 1850, 350, 2)
]
// all streets
let street1, street2, street3, street4;

let allStreets = [
    street1 = new Street("Brighton Street", 1858, 2000),
    street2 = new Street("Worthing Street", 1950, 1200),
    street3 = new Street("Shoreham Street", 1850, 800),
    street4 = new Street("Lancing Street", 1980, 760)
];
Arturas
  • 192
  • 1
  • 5