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

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

我如何從循環中獲取數組?

我如何從循環中獲取數組?

RISEBY 2023-03-10 16:07:19
大家好,你能幫我計算步數嗎?在此處輸入圖像描述function getPlan(currentProduction, months, percent) {  // write code here  let sum = 0;  for(let i = 0; i < months; i++){    let workCalculate = currentProduction * percent / 100;    sum *= workCalculate;  }  return Math.floor(sum);}示例:getPlan(1000, 6, 30) === [1300, 1690, 2197, 2856, 3712, 4825] getPlan(500, 3, 50) === [750, 1125, 1687]
查看完整描述

3 回答

?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

只需將每次迭代推送到一個數組并返回該數組。


function getPlan(currentProduction, months, percent) {

  // write code here

  // starting at currentProduction

  let sum = currentProduction;

 

  // output

  let output = [];

  

  for(let i = 0; i < months; i++){

    // progressive from sum and not from currentProduction

    let workCalculate = sum * percent / 100;

  

    sum += Math.floor(workCalculate);

    output.push(sum)

  };

  

  return output

};


console.log(getPlan(1000, 6, 30))

console.log(getPlan(500, 3, 50))


查看完整回答
反對 回復 2023-03-10
?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

目前你的方法返回一個數字,而不是一個數組。你到底需要什么?您需要它返回一個數組,還是只想查看循環內計算的中間值?


在第一種情況下,創建一個空數組并在循環的每一步中添加您想要的值:


function getPlan(currentProduction, months, percent) {

  // write code here

  let sum = 0;

  var result= [];


  for(let i = 0; i < months; i++){

    let workCalculate = currentProduction * percent / 100;

    sum *= workCalculate;

    result.push(sum);

  }


  return result;

}

在第二種情況下,您有兩個選擇:

  • 添加一個console.log,以便將值打印到控制臺。

  • 添加一個斷點,以便代碼在該處停止,您可以看到變量的值并逐步執行程序。

這有點含糊,因為您的需求不清楚,希望對您有所幫助!


查看完整回答
反對 回復 2023-03-10
?
森欄

TA貢獻1810條經驗 獲得超5個贊

function getPlan(currentProduction, months, percent) {

  var plan=[];

  var workCalculate=currentProduction;

  

  for(var i=0; i<months; i++) {

    workCalculate*=(1+percent/100);

    plan.push(Math.floor(workCalculate));

  }

  

  return plan;

}


console.log(getPlan(1000, 6, 30));

console.log(getPlan(500, 3, 50));

.as-console-wrapper { max-height: 100% !important; top: 0; }


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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