未捕获引用错误:未定义总成本

Uncaught ReferenceError: total cost is not defined

本文关键字:未定义 总成本 错误 引用      更新时间:2023-09-26

我看了一遍又一遍,但似乎找不到问题。当我在Google Chrome的JavaScript控制台中加载脚本时,它会返回以下错误CCD_ 1。这是通过document.getElementById("answerswer").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("answerswer").innerHTML =(((totalcost) - fund) / othcalc).toFixed(decs);
</script>

这是因为我的var totalcost不正确,还是因为另一个错误?内置的GoogleSites HTML框不会给我任何错误,但JSconsole会。

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

您将不得不在calcul-函数中设置innerHTML,或者以某种方式(即通过返回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("answerswer").innerHTML =(((results.totalcost) - results.fund) / results.othcalc).toFixed(decs);

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

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

在中

function calcul() {
       // here replace `var totalcost` with `totalcost`
}