1
は、次のコードを考えてみましょう。ここで出力に未定義があるのはなぜですか?
function displayParkReport() {
let title, part1, part2, part3;
title = '---PARKS REPORT---\n';
part1 = `Our ${data.all.parks.length} parks have an average of ${calcAverageParkAge()} years.`;
data.all.parks.forEach(function(cur) {
part2 += `${cur.name} has a tree density of ${cur.calcParkDensity()} trees per square km. \n`;
});
data.all.parks.forEach(function(cur) {
if(cur.numberOfTrees > 1000) {
part3 += `${cur.name} has more than 1000 trees. \n`;
}
});
console.log(title + part1 + part2 + part3);
}
function displayStreetReport() {
let title, part1, part2;
title = '---STREETS REPORT---\n';
part1 = `Our ${data.all.streets.length} streets have a total length of ${calcTotalStreetLength()} km, with an average of ${calcAverageStreetLength()} km. \n`;
data.all.streets.forEach(function(cur) {
part2 += `${cur.name}, built in ${cur.builtYear}, is a ${cur.size} street.\n`;
});
console.log(title + part1 + part2);
}
が出力されます。
---PARKS REPORT---
Our 5 parks have an average of 43.600 years.undefinedNorth Park has a
tree density of 62.500 trees per square km.
Central Park has a tree density of 102.174 trees per square km.
West Park has a tree density of 70.833 trees per square km.
East Park has a tree density of 100.000 trees per square km.
Hasan Efendi has a tree density of 235.714 trees per square km.
undefinedCentral Park has more than 1000 trees.
East Park has more than 1000 trees.
Hasan Efendi has more than 1000 trees.
---STREETS REPORT---
Our 4 streets have a total length of 122 km, with an average of 30.5 km.
undefinedBroadway, built in 2012, is a huge street.
Cavanough, built in 1992, is a normal street.
CrazyJoe, built in 2001, is a tiny street.
WestPark, built in 1956, is a small street.
なぜ出力でランダムundefined
sがありますか?
'let part2;パート2 + = "スタッフ"; console.log(part2); 'それについて何が奇妙ですか? –
私は初心者です。私は空の文字列にその変数を代入すると仮定していたのを知っていますが、悲しいことに私は時間を無駄にしてくれました。結局のところ、そのように今まで何も変わっていない。ありがとうございました! – mk3