亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

給定一個露營地數組,返回數組中露營地的總數

給定一個露營地數組,返回數組中露營地的總數

慕絲7291255 2023-07-14 16:20:06
大家好,有人可以幫我看看我是否遺漏了什么??磥砦覠o法將它們轉換為數字。//This is the data I have:let campgrounds = [  { number: 1, view: 'ocean', partySize: 8, isReserved: false },  { number: 5, view: 'ocean', partySize: 4, isReserved: false },  { number: 12, view: 'ocean', partySize: 4, isReserved: true },  { number: 18, view: 'forest', partySize: 4, isReserved: false },  { number: 23, view: 'forest', partySize: 4, isReserved: true }];//This is my code so far.function campsiteCount(campgrounds) {  let counter = 0;  for(let i = 0; i < campgrounds.length; i++) {    counter = counter + campgrounds[i];  }  return counter;}
查看完整描述

3 回答

?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

如果您實際上只想要露營地數組中的露營地數量,則可以使用:


campgrounds.length

就像你已經在 for 循環中一樣


如果出于某種原因您需要增加 for 循環中的計數(也許這是出于學習目的而給您的某種作業,當您在 for 循環初始化程序中使用campgrounds.length 時仍然看起來有點奇怪),您可以每次迭代時增加計數:


function campsiteCount(campgrounds) {

  let counter = 0;

  for(let i = 0; i < campgrounds.length; i++) {

    counter++;

  }

  return counter;

}

或者,如果您想總結所有聚會規模,我想這可能就是您想要的?你會這樣做:


function campsiteCount(campgrounds) {

  let counter = 0;

  for(let i = 0; i < campgrounds.length; i++) {

    counter += campgrounds[i].partySize;

  }

  return counter;

}


查看完整回答
反對 回復 2023-07-14
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

console.log(campgrounds.length);

數組有一個.length屬性可以告訴您數組中的項目數(您已經在 for 循環中使用了)。


查看完整回答
反對 回復 2023-07-14
?
慕田峪7331174

TA貢獻1828條經驗 獲得超13個贊

//For future reference, this worked:


function campsiteCount(campgrounds) {

  let counter = 0;

  for(let i = 0; i < campgrounds.length; i++) {

    counter = campgrounds.length;

  }

  return counter;

}


查看完整回答
反對 回復 2023-07-14
  • 3 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號