将计算函数添加到计算器js

时间:2018-03-21 23:43:15

标签: javascript jquery

如何将计算功能添加到贷款计算器。当我添加 另一个块(else)函数不起作用,其他函数也不起作用。如何正确添加功能?以下数据可以正常工作。

//  global variables
    var min, max, creditBody, oneTimeCommission, monthlyCommission, rate, grace, smsCost, monthlyFee;

    //  calculating function 
    function calc(type, summ, term) {
        min = 3, max = 36;
        if (type == "standart") {
            $("#month-slider").slider('option', {min: 3});
            $("#min").text('3');
            oneTimeCommission = 0.0199;
            monthlyCommission = 0.0199;
            rate = 0.0001;
            smsCost = 20;
            creditBody = summ / (1 - monthlyCommission);
            monthlyFee = ((creditBody + (creditBody * term * monthlyCommission)) / term) + smsCost;
        }
        else {
            min = 4, max = 36;
            $("#month-slider").slider('option',{min: min});
            $("#min").text('4');
            if (term < min) {
                term = min;
            }
            oneTimeCommission = 0;
            monthlyCommission = 0.035;
            grace = 4;
            smsCost = 20;
            creditBody = summ / (1 - 0);
            monthlyFee = ((creditBody + (creditBody * (term - grace) * monthlyCommission)) / term) + smsCost;
        }
        // my add block - not work :(
            else {
            min = 8, max = 36;
            $("#month-slider").slider('option',{min: min});
            $("#min").text('8');
            if (term < min) {
                term = min;
            }
            oneTimeCommission = 0;
            monthlyCommission = 0.035;
            grace = 8;
            smsCost = 20;
            creditBody = summ / (1 - 0);
            monthlyFee = ((creditBody + (creditBody * (term - grace) * monthlyCommission)) / term) + smsCost;
        }
        // 
        var result = {type:type,summ:summ,term:term};
        $("#monthlyFee").text(parseInt(monthlyFee) + " грн");
        $("#total-sum").text(parseInt($("#product-sum").val()) + " грн");
        $("#total-month").text(parseInt($("#credit-month").val()) + " мic");
        return result
    }

1 个答案:

答案 0 :(得分:0)

也许这可以更好地处理你的情况,避免if / else条件并使其更纯粹。

calc = ( type, params ) => {
      
  const typesFn = {

    'standart': () => {
      // do something here
    },

    'test': ( params ) => {
      console.log( params );
    }

  }

  typesFn[type](params); // call && exec

}
    
calc( 'test', {summ: null, term:null, test: true});

我希望能帮到你:)。