我的代码如下,不知道为什么最后的FinalexpectedPaybackTime结果始终无法自动计算出来,特此求助,感谢帮助。
// index.js
// 获取小程序实例
const app = getApp();
Page({
data: {
showInputModal: false,
inputModalTitle: '',
inputValue: '',
inputType: 'text',
inputCallback: null,
maintenanceFee: 0,
operatorCost: 0,
immunohistochemistryGrowthRate: 0,
detectionSystemPrice: 0,
detectionSystemCost: 0,
dailySlideCount: 0,
exchangeRate: 0,
instrumentPriceUSD: 0,
menuItems: ['无','BOND 试剂政策', '2023 BOND 政策', 'BOND 试剂政策+2023 BOND 政策'], // 下拉菜单选项
selectedMenuItem: '', // 当前选择的选项
isMenuOpen: false, // 下拉菜单是否打开
finalexpectedPaybackTime: 0,
machineDiscount: 0,
},
toggleMenu : function () {
this.setData({
isMenuOpen: !this.data.isMenuOpen
});
},
selectMenuItem: function (e) {
const { value } = e.currentTarget.dataset;
this.setData({ selectedMenuItem: value, isMenuOpen: false
});
},
onInputModalCancel() {
this.setData({ showInputModal: false });
},
onInputModalConfirm() {
this.data.inputCallback(this.data.inputValue);
this.setData({ showInputModal: false });
},
onInputChange(e) {
this.setData({ inputValue: e.detail.value });
},
instrumentPriceUSDInput: function (e) {
const instrumentPriceUSD = parseFloat(e.detail.value);
this.setData({ instrumentPriceUSD: instrumentPriceUSD });
this.calculateResults();
},
machineDiscountInput: function (e) {
const machineDiscount = parseFloat(e.detail.value);
this.setData({ machineDiscount: machineDiscount });
this.calculateResults();
},
dailySlideCountInput: function (e) {
const dailySlideCount = parseFloat(e.detail.value);
this.setData({ dailySlideCount: dailySlideCount });
this.calculateResults();
},
detectionSystemCostInput: function (e) {
const detectionSystemCost = parseFloat(e.detail.value);
this.setData({ detectionSystemCost: detectionSystemCost });
this.calculateResults();
},
detectionSystemPriceInput: function (e) {
const detectionSystemPrice = parseFloat(e.detail.value);
this.setData({ detectionSystemPrice: detectionSystemPrice });
this.calculateResults();
},
immunohistochemistryGrowthRateInput: function (e) {
const immunohistochemistryGrowthRate = parseFloat(e.detail.value);
this.setData({ immunohistochemistryGrowthRate: immunohistochemistryGrowthRate });
this.calculateResults();
},
operatorCostInput: function (e) {
const operatorCost = parseFloat(e.detail.value);
this.setData({ operatorCost: operatorCost });
this.calculateResults();
},
maintenanceFeeInput: function (e) {
const maintenanceFee = parseFloat(e.detail.value);
this.setData({ maintenanceFee: maintenanceFee });
this.calculateResults();
},
exchangeRateInput: function (e) {
const { value } = e.detail;
// 使用正则表达式替换输入值,只保留数字和小数点,并限制小数点后两位
const formattedValue = value.replace(/[^\d.]/g, '').replace(/\.(?=.*\.)/g, '').slice(0, 5);
this.setData({ exchangeRate: formattedValue });
},
calculateResults: function () {
const {
instrumentPriceUSD,
machineDiscount,
exchangeRate,
dailySlideCount,
immunohistochemistryGrowthRate,
maintenanceFee,
operatorCost,
detectionSystemPrice,
detectionSystemCost,
selectedMenuItem,
} = this.data;
const instrumentPriceRMB = instrumentPriceUSD * exchangeRate;
const instrumentPriceWithDiscount = instrumentPriceRMB * (1 - machineDiscount / 100);
const detectionSystemRev = detectionSystemPrice-detectionSystemCost;
const annualSlideCount = dailySlideCount * 21.75 * 12;
const FirstreturnUnits = Math.floor((annualSlideCount / 600) * 200);
const FIRSTexpectedPaybackTime = (instrumentPriceWithDiscount+maintenanceFee+operatorCost*2)/(((annualSlideCount*detectionSystemRev)+ ((annualSlideCount * (1+immunohistochemistryGrowthRate)-FirstreturnUnits) *detectionSystemRev))+FirstreturnUnits*detectionSystemRev);
if (selectedMenuItem === 'BOND 试剂政策') {
let SECONDexpectedPaybackTime = FIRSTexpectedPaybackTime;
const SecondreturnUnits = Math.floor((annualSlideCount * (1+immunohistochemistryGrowthRate) / 600) * 200);
while (SECONDexpectedPaybackTime > 1) {
SECONDexpectedPaybackTime = instrumentPriceWithDiscount + maintenanceFee + operatorCost * 2 -((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev + SecondreturnUnits * detectionSystemPrice / ((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev + SecondreturnUnits * detectionSystemPrice;
const ThirdreturnUnits = Math.floor((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)/ 600 * 200));
let ThirdexpectedPaybackTime = SECONDexpectedPaybackTime;
while (ThirdexpectedPaybackTime > 1) {
ThirdexpectedPaybackTime = ((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev + SecondreturnUnits * detectionSystemPrice-((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev - SecondreturnUnits * detectionSystemPrice + maintenanceFee + operatorCost / (annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) - ThirdreturnUnits) * detectionSystemRev + ThirdreturnUnits * detectionSystemPrice;
const FourthreturnUnits = Math.floor((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)* (1 + immunohistochemistryGrowthRate) / 600 * 200));
let FourthexpectedPaybackTime = ThirdexpectedPaybackTime;
while (FourthexpectedPaybackTime > 1) {
FourthexpectedPaybackTime = ((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev + SecondreturnUnits * detectionSystemPrice-((annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate)) - SecondreturnUnits) * detectionSystemRev - SecondreturnUnits * detectionSystemPrice + maintenanceFee + operatorCost-(annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) - ThirdreturnUnits) * detectionSystemRev - ThirdreturnUnits * detectionSystemPrice + maintenanceFee + operatorCost / (annualSlideCount * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) * (1 + immunohistochemistryGrowthRate) - FourthreturnUnits) * detectionSystemRev + FourthreturnUnits * detectionSystemPrice;
if (SECONDexpectedPaybackTime < 1) {
FinalexpectedPaybackTime = 2 + SECONDexpectedPaybackTime * 12;
} else if (ThirdexpectedPaybackTime < 1) {
FinalexpectedPaybackTime = 3 + ThirdexpectedPaybackTime * 12;
} else if (FourthexpectedPaybackTime < 1) {
FinalexpectedPaybackTime = 4 + FourthexpectedPaybackTime * 12;
}
this.setData({
FinalexpectedPaybackTime: FinalexpectedPaybackTime
});
}
}
}
}
}
});
相似问题