有人可以帮助我修复我的MillenniumTrees()函数,该函数显示我想要的控制台日志,但是在控制台上也返回undefined。
我如何使功能仅在控制台日志中显示?
这是我正在学习的课程的一部分练习。
该函数位于代码的底部
谢谢
// 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
答案 0 :(得分:0)
在Chrome中,在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();
答案 1 :(得分:0)
您看到的undefined
仅仅是因为您正在控制台中调用的函数返回 undefined
,并且您在控制台中评估的所有JS都会显示该值的表达。它与控制台记录代码无关。
旁注,您在初始化事物时存在问题:
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)
]
在某些模式下,JavaScript可能会阻塞,因为park1
,park2
和park3
是未定义的。我相信您真正想要的只是:
let allParks = [
new Park("Brighton Park", 1900, 1200, 3),
new Park("Worthing Park", 1800, 650, 2),
new Park("Shoreham Park", 1850, 350, 2)
]
在前一种方法中,您要用赋值表达式的结果填充数组。在第二个中,您要用构造的对象填充它。
对allStreets
的反馈意见相同。
答案 2 :(得分:0)
定义在数组中使用的变量:
// 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)
];