2013-08-19 32 views
0

我已经反复查看,但似乎无法找到问题。当我在Google Chrome的JavaScript控制台中加载脚本时,它会将以下错误返回给我 Uncaught referenceerror: total cost is not defined。这发生在document.getElementById("answer").innerHTML代码中。未捕获ReferenceError:总成本未定义

我试图分割公式,给它另一个名字,重新组合公式,但它仍然给我同样的错误。

//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 Sites HTML-box不会给我任何错误,但是JSconsole可以。

+0

我不明白。在我所有的其他模板和文件中,它工作。下面是一个它工作的例子:http://jsfiddle.net/VapTM/ – user2686401

回答

0

您在函数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("answer").innerHTML =(((results.totalcost) - results.fund)/results.othcalc).toFixed(decs); 
+0

但为什么它在这里工作:http://jsfiddle.net/VapTM/? – user2686401

+0

因为innerHTML是在调用后直接设置的,即在同一个函数中,所以在同一个范围内,所以变量仍然保存使用的值。在你的情况下,innerHTML没有在完成计算的函数中设置。 – user1983983

+0

让你的函数起作用的另一种方法是将最后3个命令放入'calcul'函数中。这就是你在这个小提琴中所做的。这可以解决你的问题,但我认为这不是一个好的方法,因为当你想为另一个任务重用函数时,你会努力工作,而你不想设置'answer'的innerHTML。 – user1983983

0

定义totalcost为全局变量,这样你就可以在函数范围外访问totalcost值。

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

在你

function calcul() { 

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

} 
+0

试过了,但我仍然收到错误消息。这里是jsfiddle:http://jsfiddle.net/K75Uy/ 或者我输入了错误,那也是可能的 – user2686401

+0

@ user2686401检查我的编辑。从函数内的totalcost中删除var。并在函数之外定义var totalcost。 –

+0

找到解决方案。我刚刚关闭了'function()'。在'document.get ......'之后添加'}'现在,它至少起作用。该公式中仍然存在一个错误,但这是我必须自行整理的。大家都谢谢! – user2686401

相关问题