我有一个脚本,但我不明白为什么它不能正确执行。 我已经声明了所有变量,并且根据提示它工作正常,但我不明白为什么它不会记录我的函数的结果。
var leasePriceString = prompt("Input lease price per month");
var ecoScoreString = prompt("Input eco score");
var catalogValueString = prompt("Input catalog value");
var c02String = prompt("Input C02");
var leasePrice = parseInt(leasePriceString);
var ecoScore = parseInt(ecoScoreString);
var catalogValue = parseInt(catalogValueString);
var c02 = parseInt(c02String);
var brutoMonth = true;
var VAA = true;
function calculator(){
function brutoMonthCalc(){
if (ecoScore >= 74){
brutoMonth = ((leasePrice*12)/13.92)-75;
console.log(brutoMonth);
} else {
brutoMonth = ((leasePrice*12)/13.92)-150;
console.log(brutoMonth);
}
}
function VAACalc(){
VAA = 6/7*catalogValue*(0.055+((c02-105)*0.001));
console.log(VAA);
}
brutoMonthCalc();
VAACalc();
console.log("price per month is =" + brutoMonth + VAA);
};
calculator();
最佳答案
我修改了您的代码的某些方面,并在下面提供了解释:
var leasePriceString = prompt("Input lease price per month");
var ecoScoreString = prompt("Input eco score");
var catalogValueString = prompt("Input catalog value");
var c02String = prompt("Input C02");
var leasePrice = parseInt(leasePriceString);
var ecoScore = parseInt(ecoScoreString);
var catalogValue = parseInt(catalogValueString);
var c02 = parseInt(c02String);
// Declare global variables here
var brutoMonth;
var VAA;
function calculator(){
function brutoMonthCalc() {
if (ecoScore >= 74) {
brutoMonth = ((leasePrice * 12) / 13.92) - 75;
console.log(brutoMonth);
}else{
brutoMonth = ((leasePrice * 12) / 13.92) - 150;
console.log(brutoMonth);
}
}
function VAACalc() {
VAA = 6 / 7 * catalogValue * (0.055 + ((c02 - 105) * 0.001));
console.log(VAA);
}
// Call functions here
brutoMonthCalc();
VAACalc();
}
calculator();
console.log("price per month is =" + brutoMonth + VAA);
在上面的代码中我主要改变了两件事:-
声明全局变量:您只想声明一次变量,然后将它们分配到您打算运行以更改其值的函数中。
调用函数:编写函数后,稍后需要调用它。仅当您调用它时,函数中的表达式才会运行。
关于Javascript 脚本不会登录到浏览器控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52265371/