我一遍又一遍地看着它,但我似乎无法找到问题。当我在Google Chrome的JavaScript控制台中加载脚本时,它会将以下错误返回给我
Uncaught referenceerror: total cost is not defined
。这是由document.getElementById("answer").innerHTML
代码发生的。
我试图拆分公式,给它另一个名字,重新组合公式,但它仍然给我同样的错误。
//waarde vasthouden
function calcul(){
var fund = parseFloat(document.getElementById("fund").value);
var age1 = parseFloat(document.getElementById("age1").value);
var age2 = parseFloat(document.getElementById("age2").value);
var age3 = parseFloat(document.getElementById("age3").value);
var age4 = parseFloat(document.getElementById("age4").value);
var age5 = parseFloat(document.getElementById("age5").value);
var age6 = parseFloat(document.getElementById("age6").value);
var primcost = parseFloat(document.getElementById("primcost").value);
var seccost = parseFloat(document.getElementById("seccost").value);
var unicost = parseFloat(document.getElementById("unicost").value);
var start = parseFloat(document.getElementById("start").value);
var annrate = parseFloat(document.getElementById("annrate").value);
//additional calculations
//Duration primary school
var primdur = (age2 - age1) + 1;
//Present Value Primary School before starting
var pvprim = primcost * ((1 - (Math.pow((1 + (annrate / 100)), primdur))) / (annrate / 100));
//Duration secondary school
var secdur = (age4 - age3) + 1;
//Present Value secondary School before starting
var pvsec = seccost * ((1 - (Math.pow((1 + (annrate / 100)), secdur))) / (annrate / 100));
//Duration university
var unidur = (age6 - age5) + 1;
//Present Value university before starting
var pvuni = unicost * ((1 - (Math.pow((1 + (annrate / 100)), unidur))) / (annrate / 100));
//Present value when starting primary school
//var X1 = ((pvprim) / (Math.pow((1 + (annrate / 100)), (age1 - 1))));
//Present value when starting secondary school
//var X2 = ((pvsec) / (Math.pow((1 + (annrate / 100)), (age3 - 1))));
//Present value when starting university
//var X3 = ((pvuni) / (Math.pow((1 + (annrate / 100)), (age5 - 1))));
//Annual deposits (PV formula)
var totalcost = ((pvprim) / (Math.pow((1 + (annrate / 100)), (age1 - 1)))) +
((pvsec) / (Math.pow((1 + (annrate / 100)), (age3 - 1)))) +
((pvuni) / (Math.pow((1 + (annrate / 100)), (age5 - 1))));
var othcalc = (1 / (annrate / 100)) - ((1 / ((annrate / 100)*(Math.pow((1 + (annrate / 100)), age5)))));
}
//binnen in script resterende decimal code, nadat alle variabelen zijn gedefinieerd
var decs = +(document.getElementById('dec').value);
calculated = true; //end of decimal code
//Eindantwoord weergeven in decimalen
document.getElementById("answer").innerHTML =(((totalcost) - fund) / othcalc).toFixed(decs);
</script>
这是因为我的var totalcost
不正确,还是因为另一个错误?内置的Google协作平台HTML框不会给我任何错误,但JSconsole会这样做。
答案 0 :(得分:0)
您在函数calcul
中包装的所有变量都在不同的范围内,因此它们仅在此函数内可用。在JavaScript中,您在开始新函数时定义了一个新范围。
您必须在innerHTML
- 函数内设置calcul
或以某种方式返回您需要执行此操作的值(即返回一个JavaScript对象)。
您遇到的第二个问题是,在尝试使用您在里面计算的值之前,您没有调用该函数。
尝试以下
function calcul() {
// Your calculation logic goes here
return { "fund": fund, "totalcost": totalcost, "othcalc": othcalc };
}
var decs = +(document.getElementById('dec').value);
var results = calcul();
document.getElementById("answer").innerHTML =(((results.totalcost) - results.fund) / results.othcalc).toFixed(decs);
答案 1 :(得分:0)
将totalcost
定义为全局变量,以便您可以在函数外部访问totalcost
值。
var totalcost; // define this var outside of function calcul()
在你的
中function calcul() {
// here replace `var totalcost` with `totalcost`
}