3 回答
TA貢獻1909條經(jīng)驗 獲得超7個贊
你的第一個代碼是如何通過的。按數(shù)字劃分數(shù)組是 NaN。試試這個。
function calculate(volunteers, neighborhoods) {
return volunteers.length / neighborhoods.length
}
TA貢獻1794條經(jīng)驗 獲得超8個贊
看起來您使用了不正確的語法來聲明函數(shù)。注意這function是 JavaScript 中的保留關(guān)鍵字。所以const function {...}不是有效的語法。您應(yīng)該在function關(guān)鍵字后命名您的函數(shù)。試著這樣寫:
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條經(jīng)驗 獲得超11個贊
結(jié)果應(yīng)該是一樣的
在方法中two而不是volunteers.length / neighbourhoods.length它應(yīng)該是neighbourhoods.length / volunteers.length因為在諸如除法之類的數(shù)學(xué)運算中,因子的順序會改變結(jié)果
固定代碼:
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))
添加回答
舉報
