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

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,以便將值打印到控制臺。
添加一個斷點,以便代碼在該處停止,您可以看到變量的值并逐步執行程序。
這有點含糊,因為您的需求不清楚,希望對您有所幫助!

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; }
添加回答
舉報