简体   繁体   中英

how can I combine these two jquery calculations?

I have some inputs which has the foot/inch of two widths and two heights. I have to convert them into all foot and get the square footage. example, width is 1'2" and height is 1'2". Another width is 2'1" and height is 2'1". I have to convert the width into foot then times the height for both of them and then multiply the first width and height then addition to the 2nd width and height. Also, each side will need an addition 1.6 multiplication on top. I have done it with the first width and height but I figured doing it for the second one is duplicating but don't have much idea what is the best way to combine them together since.

I have something like this for my js

$('.total-footage').on('click', function(){
    /*Side A Calculation*/
    var $sideAFootValueW = parseInt($('.dimensions-side-a .dimension-width .dimension-input-foot').val());
    var $sideAInchValueW = parseInt($('.dimensions-side-a .dimension-width .dimension-input-inch').val());

    if(($sideAFootValueW == "") || (!$.isNumeric($sideAFootValueW))){
        $sideAFootValueW = 0;
    }

    if(($sideAInchValueW == "") || (!$.isNumeric($sideAInchValueW))){
        $sideAInchValueW = 0;
    }else{
        $sideAInchValueW = $sideAInchValueW/12;
    }

    var totalFootAW = $sideAFootValueW + $sideAInchValueW;

    var $sideAFootValueH = parseInt($('.dimensions-side-a .dimension-height .dimension-input-foot').val());
    var $sideAInchValueH = parseInt($('.dimensions-side-a .dimension-height .dimension-input-inch').val());

    if(($sideAFootValueH == "") || (!$.isNumeric($sideAFootValueH))){
        $sideAFootValueH = 0;
    }

    if(($sideAInchValueH == "") || (!$.isNumeric($sideAInchValueH))){
        $sideAInchValueH = 0;
    }else{
        $sideAInchValueH = $sideAInchValueH/12;
    }

    var totalFootAH = $sideAFootValueH + $sideAInchValueH;

    var sideATotal = totalFootAH * totalFootAW * 1.6;

    /*Side B Calculation*/
    var $sideBFootValueW = parseInt($('.dimensions-side-b .dimension-width .dimension-input-foot').val());
    var $sideBInchValueW = parseInt($('.dimensions-side-b .dimension-width .dimension-input-inch').val());

    if(($sideBFootValueW == "") || (!$.isNumeric($sideBFootValueW))){
        $sideBFootValueW = 0;
    }

    if(($sideBInchValueW == "") || (!$.isNumeric($sideBInchValueW))){
        $sideBInchValueW = 0;
    }else{
        $sideBInchValueW = $sideBInchValueW/12;
    }

    var totalFootBW = $sideBFootValueW + $sideBInchValueW;

    var $sideBFootValueH = parseInt($('.dimensions-side-b .dimension-height .dimension-input-foot').val());
    var $sideBInchValueH = parseInt($('.dimensions-side-b .dimension-height .dimension-input-inch').val());

    if(($sideBFootValueH == "") || (!$.isNumeric($sideBFootValueH))){
        $sideBFootValueH = 0;
    }

    if(($sideBInchValueH == "") || (!$.isNumeric($sideBInchValueH))){
        $sideBInchValueH = 0;
    }else{
        $sideBInchValueH = $sideBInchValueH/12;
    }

    var totalFootBH = $sideBFootValueH + $sideBInchValueH;

    var sideBTotal = totalFootBH * totalFootBW * 1.6;

    $('.total').empty();
    $('.total').append(sideATotal+sideBTotal);

});

I figured I should be have a function so this calculation can be done easily even if there are more sides I need to calculate

Add the two helped functions to your code

function getSideDimensions($side){
    var width = getDimension($side.find('.dimension-width'));
    var height = getDimension($side.find('.dimension-height'));
    return width * height * 1.6;
}

function getDimension($dimension){
    var feet = $dimension.find('.dimension-input-foot');
    var inches = $dimension.find('.dimension-input-inch');

    if((feet == "") || (!$.isNumeric(feet))){
        feet = 0;
    }

    if((inches == "") || (!$.isNumeric(inches))){
        inches = 0;
    }else{
        inches = inches/12;
    }
    return feet + inches;
}

And alter your code to

$('.total-footage').on('click', function(){
    var sideATotal = getSideDimensions($('.dimensions-side-a'));
    var sideBTotal = getSideDimensions($('.dimensions-side-b'));
    $('.total').empty();
    $('.total').append(sideATotal+sideBTotal);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM