2013-08-19 29 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

相關問題