谁能告诉我这些JavaScript程序涉及哪些问题?我将非常感谢您的意见和建议以及更正!
有一个数组。应该从这个数组创建三个函数。编写以下三个函数。所有这三个都应该使用火山阵列来确定要返回的内容。
GetVolcanoByName -此函数需要一个字符串作为参数,并返回火山数组中的对象,该对象的名称属性等于传递给它的字符串(如果有)。
getVolcanoesByCountry -此函数需要一个字符串作为参数,并返回一个数组,其中包含火山数组中的对象,这些对象的country属性等于传递给它的字符串。
getAverageHeight -此函数返回数组中所有火山的平均高度。
let volcanoes = [
createVolcano('Eyjafjallajökull', 'Iceland', 1651),
createVolcano('Krakatoa', 'Indonesia', 2850),
createVolcano('Manua Loa', 'USA', 4169),
createVolcano('Mount Etna', 'Italy', 3326),
createVolcano('Mount Fuji', 'Japan', 3778),
createVolcano('Mount Saint Helens', 'USA', 2549),
createVolcano('Mount Vesuvius', 'Italy', 1281)
];
function getVolcanoByName() {
for (var i = 0; i < volcanoes.length; i++) {
let volcano = volcanoes[i];
if (volcano.name === name) {
return name;
}
}
}
function getVolcanoesByCountry(country) {
return this.volcanoes.filter((volcano) => {
return volcano.country
});
}
function getAverageHeight(heightInMeters) {
var sum = 0;
for(var i = 0; i < volcanoes.heightInMeters.length; i++) {
sum += volcanoes.heightInMeters[i];
}
}
发布于 2021-04-24 16:14:21
下面是一些关于函数的参考资料。
让我们实现缺少的工厂createVolcano,一个组装/构建/创建和返回对象的函数……
function createVolcano(name, country, currentHeight) {
return {
name,
country,
heightInMeters: currentHeight,
};
}让我们继续阅读有关的内容,特别是关于如何unpack fields from objects passed as a function parameter的内容。
在下一步中,我们将实现getVolcanoByName,它将基于通过特定条件对Array实例中的项执行find操作……
function getVolcanoByName(list, value) {
return list.find(({ name }) => name === value);
}然后我们将转向getVolcanoesByCountry的实现,它将基于对给定数组中的一个或多个项进行filter(同样是通过一个条件),返回一个仅包含已过滤项的新数组……
function getVolcanoesByCountry(list, value) {
return list.filter(({ country }) => country === value);
}最后,我们需要计算getAverageVolcanoeHeight,我们将使用reduce来计算列表中所有火山项目的总高度。结果(总高度)然后立即除以计算中涉及的火山项目的数量…
function getAverageVolcanoeHeight(list) {
return (list.reduce((total, { heightInMeters }) =>
(total + heightInMeters), 0
) / list.length);
}结合OP的示例,可以运行和测试上面实现/提供的代码……
function getVolcanoByName(list, value) {
return list.find(({ name }) => name === value);
}
function getVolcanoesByCountry(list, value) {
return list.filter(({ country }) => country === value);
}
function getAverageVolcanoeHeight(list) {
return (list.reduce((total, { heightInMeters }) =>
(total + heightInMeters), 0
) / list.length);
}
function createVolcano(name, country, currentHeight) {
return {
name,
country,
heightInMeters: currentHeight,
};
}
const volcanoeList = [
createVolcano('Eyjafjallajökull', 'Iceland', 1651),
createVolcano('Krakatoa', 'Indonesia', 2850),
createVolcano('Manua Loa', 'USA', 4169),
createVolcano('Mount Etna', 'Italy', 3326),
createVolcano('Mount Fuji', 'Japan', 3778),
createVolcano('Mount Saint Helens', 'USA', 2549),
createVolcano('Mount Vesuvius', 'Italy', 1281),
];
console.log({ volcanoeList });
console.log(
'getVolcanoByName(volcanoeList, "Mount Saint Helens") ...',
getVolcanoByName(volcanoeList, "Mount Saint Helens")
);
console.log(
'getVolcanoByName(volcanoeList) ...',
getVolcanoByName(volcanoeList)
);
console.log(
'getVolcanoesByCountry(volcanoeList, "Italy") ...',
getVolcanoesByCountry(volcanoeList, "Italy")
);
console.log(
'getVolcanoesByCountry(volcanoeList) ...',
getVolcanoesByCountry(volcanoeList)
);
console.log(
'getAverageVolcanoeHeight(volcanoeList) ...',
getAverageVolcanoeHeight(volcanoeList)
);.as-console-wrapper { min-height: 100%!important; top: 0; }
https://stackoverflow.com/questions/67237614
复制相似问题