0

我看了一遍又一遍,但我似乎找不到问题所在。当我在 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 会。

4

2 回答 2

0

定义totalcost为全局变量,以便您可以访问totalcost函数之外的值。

var totalcost; // define this var outside of function calcul()

在你的

function calcul() {

       // here replace `var totalcost` with `totalcost`

}
于 2013-08-19T10:12:29.303 回答
0

您在函数内包装的所有变量calcul都在不同的范围内,因此它们仅在此函数内可用。在 JavaScript 中,当你开始一个新函数时,你定义了一个新的作用域。

您要么必须设置-functioninnerHTML内部,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);
于 2013-08-19T10:00:31.520 回答