计算错误值的JavaScript等式

时间:2015-04-16 05:42:23

标签: javascript html

我在数字折旧方程之和方面遇到了困难。如果函数的最后一部分在底部else语句中,则数字之和等式。算法中的某些位置存在错误,但我无法找到它。

它打印的表应如下所示:

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;
        }
    }
}

如果有什么可以帮我找到它出错的地方我会非常感激!

谢谢!

1 个答案:

答案 0 :(得分: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;
    }