第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

下面兩個代碼有什么區(qū)別,(我運行了它們,但它們給出了不同的輸出)

下面兩個代碼有什么區(qū)別,(我運行了它們,但它們給出了不同的輸出)

嗶嗶one 2021-12-12 17:47:41
我正在用 JavaScript 做一個挑戰(zhàn),指令是將志愿者的數(shù)量除以社區(qū)的數(shù)量。我用.length數(shù)組的方法獲取長度并執(zhí)行了操作,但是讓我煩惱的一件事是我嘗試了兩種方法,一種通過了,一種無法通過測試,代碼如下這個通過了測試const function(volunteers, neighborhood){  let neighborhoodLent = neighborhood;  let volunteersLength = volunteers.length  let evaluate =  neighborhoodLent / volunteersLength;  return evaluate;}下面這個沒有通過測試。const function(volunteers, neighborhood){  return volunteers.length / neighborhood.length}當(dāng)給出一個像下面這樣的數(shù)組時const volunteers = [  'Sally',  'Jake',  'Brian',  'Hamid'];const neighbourhoods = [  'Central Valley',  'Big Mountain',  'Little Bridge',  'Bricktown',  'Brownsville',  "Paul's Boutique",  'Clay Park',  'Fox Nest'];輸出應(yīng)該是2。我現(xiàn)在關(guān)心的是知道我使用的兩種方法之間的區(qū)別以及為什么另一個通過了另一個失敗了。請我將感謝您幫助澄清我
查看完整描述

3 回答

?
jeck貓

TA貢獻1909條經(jīng)驗 獲得超7個贊

你的第一個代碼是如何通過的。按數(shù)字劃分數(shù)組是 NaN。試試這個。


function calculate(volunteers, neighborhoods) {

    return volunteers.length / neighborhoods.length

}


查看完整回答
反對 回復(fù) 2021-12-12
?
幕布斯7119047

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));


查看完整回答
反對 回復(fù) 2021-12-12
?
呼喚遠方

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))


查看完整回答
反對 回復(fù) 2021-12-12
  • 3 回答
  • 0 關(guān)注
  • 242 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號