3 回答
TA贡献1909条经验 获得超7个赞
你的第一个代码是如何通过的。按数字划分数组是 NaN。试试这个。
function calculate(volunteers, neighborhoods) {
return volunteers.length / neighborhoods.length
}
TA贡献1794条经验 获得超8个赞
看起来您使用了不正确的语法来声明函数。注意这function是 JavaScript 中的保留关键字。所以const function {...}不是有效的语法。您应该在function关键字后命名您的函数。试着这样写:
const volunteers = [
'Sally',
'Jake',
'Brian',
'Hamid'
];
const neighbourhoods = [
'Central Valley',
'Big Mountain',
'Little Bridge',
'Bricktown',
'Brownsville',
"Paul's Boutique",
'Clay Park',
'Fox Nest'
];
function calc (volunteers, neighborhood) {
return neighborhood.length / volunteers.length;
}
const constCalc = (volunteers, neighborhood) =>{
return neighborhood.length / volunteers.length;
}
console.log(`usual declaration`, calc(volunteers, neighbourhoods));
console.log(`arrow function:`, constCalc(volunteers, neighbourhoods));
TA贡献1856条经验 获得超11个赞
结果应该是一样的
在方法中two而不是volunteers.length / neighbourhoods.length它应该是neighbourhoods.length / volunteers.length因为在诸如除法之类的数学运算中,因子的顺序会改变结果
固定代码:
const volunteers = ['Sally','Jake','Brian','Hamid']
const neighbourhoods = ['Central Valley','Big Mountain','Little Bridge','Bricktown','Brownsville',"Paul's Boutique",'Clay Park','Fox Nest']
const one = (volunteers, neighbourhoods) => {
const neighbourhoodsLength = neighbourhoods.length
const volunteersLength = volunteers.length
const evaluate = neighbourhoodsLength / volunteersLength
return evaluate
}
const two = (volunteers, neighbourhoods) => {
return neighbourhoods.length / volunteers.length
}
console.log('one:', one(volunteers, neighbourhoods))
console.log('two:', two(volunteers, neighbourhoods))
添加回答
举报