2015-04-16 76 views
0

我在计算折旧方程的总和时遇到了困难。如果底部else语句中函数的最后部分,数字等式的总和。算法中某处出现错误,我无法找到它。计算不正确值的JavaScript方程

它打印应该是这样的表格:

correct results

但它打印这样的:

incorrect results

function calDepreciation() { 

    var rowNum = document.getElementById('myTable').rows.length; 

    if(rowNum > 1){ 
     for(i = 0; i < (rowNum-1); i++){ 
      document.getElementById("myTable").deleteRow(-1); 
     } 
    } 
    var table = document.getElementById("myTable"); 
    var maxRow = document.getElementById("myTable").getElementsByTagName(
      "tr").length; 
    var assetVal = document.getElementById("assetVal").value; 
    var assetLife = document.getElementById("assetLife").value; 
    var salVal = document.getElementById("salvageVal").value; 
    var eq = ""; 
    var year = 1; 

    var radios = document.getElementsByName('choice'); 
    for (var i = 0, length = radios.length; i < length; i++) { 
     if (radios[i].checked) { 
      eq = radios[i].value; 

      break; 
     } 
    } 

    if (eq == "straightLine") { 
     var depreciation = (assetVal - salVal)/assetLife; 
     var aDepreciation = depreciation; 
     assetVal = assetVal - salVal; 

     for (i = 0; i < assetLife; i++) { 
      assetVal = assetVal - depreciation; 
      var row = table.insertRow(-1); 
      var cell1 = row.insertCell(0); 
      var cell2 = row.insertCell(1); 
      var cell3 = row.insertCell(2); 
      var cell4 = row.insertCell(3); 
      var num = 2; 
      cell1.innerHTML = year; 
      cell2.innerHTML = parseFloat(Math.round(assetVal * 100)/100) 
        .toFixed(2); 
      cell3.innerHTML = parseFloat(
        Math.round(depreciation * 100)/100).toFixed(2); 
      cell4.innerHTML = parseFloat(
        Math.round(aDepreciation * 100)/100).toFixed(2); 

      aDepreciation = aDepreciation + depreciation; 
      year = year + 1; 
     } 
    } else if (eq == "doubleDeclining") { 
     var depreciation = (1/assetLife) * assetVal; 
     var aDepreciation = depreciation; 
     assetVal = assetVal - depreciation; 

     for (i = 0; i < assetLife; i++) { 

      var row = table.insertRow(-1); 
      var cell1 = row.insertCell(0); 
      var cell2 = row.insertCell(1); 
      var cell3 = row.insertCell(2); 
      var cell4 = row.insertCell(3); 
      var num = 2; 
      cell1.innerHTML = year; 
      cell2.innerHTML = parseFloat(Math.round(assetVal * 100)/100) 
        .toFixed(2); 
      cell3.innerHTML = parseFloat(
        Math.round(depreciation * 100)/100).toFixed(2); 
      cell4.innerHTML = parseFloat(
        Math.round(aDepreciation * 100)/100).toFixed(2); 


      year = year + 1; 
      depreciation = (1/assetLife) * assetVal; 
      assetVal = assetVal - depreciation; 
      aDepreciation = aDepreciation + depreciation; 
     } 
    } else { 
     assetVal = assetVal - salVal; 
     var s = 0; 
     var n = parseInt(document.getElementById("assetLife").value, 10); 
     for(i = 1; i <= (assetLife); i++){ 
      s = s + i; 
     } 

     var p = (((n + 1) - year))/ s * 1.0; 

     var depreciation = assetVal * p; 
     var aDepreciation = depreciation; 
     assetVal = assetVal - depreciation; 

     for (i = 0; i < assetLife; i++) { 

      var row = table.insertRow(-1); 
      var cell1 = row.insertCell(0); 
      var cell2 = row.insertCell(1); 
      var cell3 = row.insertCell(2); 
      var cell4 = row.insertCell(3); 
      var num = 2; 
      cell1.innerHTML = year; 
      cell2.innerHTML = parseFloat(Math.round(assetVal * 100)/100) 
        .toFixed(2); 
      cell3.innerHTML = parseFloat(
        Math.round(depreciation * 100)/100).toFixed(2); 
      cell4.innerHTML = parseFloat(
        Math.round(aDepreciation * 100)/100).toFixed(2); 


      year = year + 1; 
      p = (n + 1 - year)/s * 1.0; 
      depreciation = p * assetVal * 1.0; 
      assetVal = assetVal - depreciation; 
      aDepreciation = aDepreciation + depreciation; 
     } 
    } 
} 

如果有什么能帮助我找到在哪里它去错误我会非常感谢它!

谢谢!

+3

如何刚刚发布的代码,而DOM操作这部分? –

+0

它更好,如果你能提供小提琴 – Kushal

回答

0

它这里

assetVal = assetVal - salVal; 
    var s = 0; 
    var n = parseInt(document.getElementById("assetLife").value, 10); 
    for(i = 1; i <= (assetLife); i++){ 
     s = s + i; 
    } 

    var p = (((n + 1) - year))/ s * 1.0; 

    var depreciation = assetVal * p; 
    var aDepreciation = depreciation; 
    assetVal = assetVal - depreciation; 

    for (i = 0; i < assetLife; i++) { 

     var row = table.insertRow(-1); 
     var cell1 = row.insertCell(0); 
     var cell2 = row.insertCell(1); 
     var cell3 = row.insertCell(2); 
     var cell4 = row.insertCell(3); 
     var num = 2; 
     cell1.innerHTML = year; 
     cell2.innerHTML = parseFloat(Math.round(assetVal * 100)/100) 
       .toFixed(2); 
     cell3.innerHTML = parseFloat(
       Math.round(depreciation * 100)/100).toFixed(2); 
     cell4.innerHTML = parseFloat(
       Math.round(aDepreciation * 100)/100).toFixed(2); 


     year = year + 1; 
     p = (n + 1 - year)/s * 1.0; 
     depreciation = p * assetVal * 1.0; 
     assetVal = assetVal - depreciation; 
     aDepreciation = aDepreciation + depreciation; 
    }