我无法弄清楚下面的javascript代码有什么问题。我尝试将函数存储在变量中

时间:2017-05-28 17:11:13

标签: javascript function var

我根据收到的建议重新编写了整个代码,但似乎有些东西我想念,因为我仍然不能在数学运算中使用var deducere。我一直都在拿NaN。我的代码上的任何建议都会非常有用,因为此时我的想法已经用完了。这似乎是最好的学习方式。

function calculeazaVenit() {
        var venit_brut = document.getElementById("venit_brut").value;
        var selection = document.getElementById("select1").value;
        var cas = (10.5 / 100) * venit_brut;
        var cass = (5.5 / 100) * venit_brut;
        var somaj = (0.5 / 100) * venit_brut;
        var nr_tichete = document.getElementById("nr_tichete").value;
        var val_tichete = document.getElementById("val_tichete").value;
        var tichete = nr_tichete * val_tichete;
        var impozit = (16 / 100) * (venit + tichete);
        if (venit_brut != null) {
            document.getElementById("venit_brutx").innerHTML = Math.round(venit_brut);
            document.getElementById("cas").innerHTML = Math.round(cas);
            document.getElementById("cass").innerHTML = Math.round(cass);
            document.getElementById("somaj").innerHTML = Math.round(somaj);
            document.getElementById("venitx").innerHTML = Math.round(venit);
            document.getElementById("tichete").innerHTML = Math.round(tichete);
            document.getElementById("impozit").innerHTML = Math.round(impozit);
            document.getElementById("venit_net").innerHTML = Math.round(venit_net);
        }
        };


        function calculateDeducere(selection, venit_brut) {
        var deducere0 = (1 - (venit_brut - 1500) / 1500) * 300;
        var deducere1 = (1 - (venit_brut - 1500) / 1500) * 400;
        var deducere2 = (1 - (venit_brut - 1500) / 1500) * 500;
        var deducere3 = (1 - (venit_brut - 1500) / 1500) * 600;
        var deducere4 = (1 - (venit_brut - 1500) / 1500) * 800;
        if (selection == 0 && venitBrut <= 1500) {
            return 300;
        }
        if (selection == 0 && venitBrut > 1500 && venitBrut <= 3000) {
            return Math.ceil10(deducere0, 1);
        }
        if (selection == 1 && venitBrut <= 1500) {
            return 400;
        }
        if (selection == 1 && venitBrut > 1500 && venitBrut <= 3000) {
            return Math.ceil10(deducere1, 1);
        }
        if (selection == 2 && venitBrut <= 1500) {
            return 500;
        }
        if (selection == 2 && venitBrut > 1500 && venitBrut <= 3000) {
            return Math.ceil10(deducere2, 1);
        }
        if (selection == 3 && venitBrut <= 1500) {
            return 600;
        }
        if (selection == 3 && venitBrut > 1500 && venitBrut <= 3000) {
            return Math.ceil10(deducere3, 1);
        }
        if (selection == 4 && venitBrut <= 1500) {
            return 800;
        }
        if (selection == 4 && venitBrut > 1500 && venitBrut <= 3000) {
            return Math.ceil10(deducere4, 1);
        }
        if (document.getElementById("btn_deducere").checked == false) {
            document.getElementById("deducerex").innerHTML = 0;
        }
        if (venit_brut > 3000)  {
        return 0;
        }
        };

    var deducere = calculateDeducere(selection, venit_brut);
    var venit = venit_brut - (cas + cass + somaj + deducere);
    var venit_net = venit - impozit + deducere;

        document.getElementById("deducerex").innerHTML = Math.round(deducere);

1 个答案:

答案 0 :(得分:1)

var deducere0 = ...,
    deducere1 = ...,
    deducere2 = ...,    
    deducere3 = ...,
    deducere4 = ...;


function calculateFoo(selection, venitBrut) {
    if (selection == 0 && venitBrut <= 1500) {
        return 300;
    }
    if (selection == 0 && venitBrut > 1500 && venitBrut <= 3000) {
        return Math.ceil10(deducere0, 1);
    }
    if (selection == 1 && venitBrut <= 1500) {
        return 400;
    }
    if (selection == 1 && venitBrut > 1500 && venitBrut <= 3000) {
        return Math.ceil10(deducere1, 1);
    }
    if (selection == 2 && venitBrut <= 1500) {
        return 500;
    }
    if (selection == 2 && venitBrut > 1500 && venitBrut <= 3000) {
        return Math.ceil10(deducere2, 1);
    }
    if (selection == 3 && venitBrut <= 1500) {
        return 600;
    }
    if (selection == 3 && venitBrut > 1500 && venitBrut <= 3000) {
        return Math.ceil10(deducere3, 1);
    }
    if (selection == 4 && venitBrut <= 1500) {
        return 800;
    }
    if (selection == 4 && venitBrut > 1500 && venitBrut <= 3000) {
        return Math.ceil10(deducere4, 1);
    }
    if (document.getElementById("btn_deducere").checked == false) {
        return 0;
    }

    return 0;
};

var foo = calculateFoo(selection, venitBrut);
var venit = venitBrut - (cas + cass + somaj + foo);

console.log(venit);
  • 确保声明deducere01234
  • 添加一些console.log以检查函数
  • 返回的值
  • 您应该将foo更改为更合适的名称,我真的不明白在计算什么

希望这会有所帮助:)