3 回答
TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個(gè)贊
只需將每次迭代推送到一個(gè)數(shù)組并返回該數(shù)組。
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貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
目前你的方法返回一個(gè)數(shù)字,而不是一個(gè)數(shù)組。你到底需要什么?您需要它返回一個(gè)數(shù)組,還是只想查看循環(huán)內(nèi)計(jì)算的中間值?
在第一種情況下,創(chuàng)建一個(gè)空數(shù)組并在循環(huán)的每一步中添加您想要的值:
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;
}
在第二種情況下,您有兩個(gè)選擇:
添加一個(gè)console.log,以便將值打印到控制臺(tái)。
添加一個(gè)斷點(diǎn),以便代碼在該處停止,您可以看到變量的值并逐步執(zhí)行程序。
這有點(diǎn)含糊,因?yàn)槟男枨蟛磺宄?,希望?duì)您有所幫助!
TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超5個(gè)贊
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; }
添加回答
舉報(bào)
