-3
\$\begingroup\$

I was told I should post this code here because I have a good amount of redundancy in the code. I am new to JS and would appreciate any help in condensing this code and having it still function the same. Does anyone see anything I can do better to help make this code run more smoothly?

https://jsfiddle.net/bobrierton/1m5thp43/

/* eslint-env node, browser */
/* eslint spaced-comment: 0, quotes: 0, brace-style: 0, indent: 0, max-len: 0, no-alert: 0 */
import precise from './precise';

// refactored from CreditCalc()
function calculateCredit(options) {
  let cmnv = Number(options.tCreditMonths); // get credit months
  const ctwv = Number(options.tCreditWeight); // get credit weight or length
  const cccv = Number(options.creditClass); // get credit _class
  let cctot = 0; //base tax breakdown  // GR (General Revenue)

  if (!cmnv || !ctwv || !cccv) {
    return precise(0);
  }

  if (cccv !== 0) {
    if ((cccv === 1) && (ctwv < 2500)) //base tax _class 01(01)
    {
        cctot = 14.50 * (cmnv / 12);
    } //14.50 Flat
    if ((cccv === 1) && (ctwv > 2499) && (ctwv < 3500)) //base tax _class 01(02)
    {
        cctot = 22.50 * (cmnv / 12);
    } //22.50 Flat
    if ((cccv === 1) && (ctwv > 3499) && (ctwv <= 99999)) //base tax _class 01(03)
    {
        cctot = 32.50 * (cmnv / 12);
    } //32.50 Flat
    if ((cccv === 9) && (ctwv > 0) && (ctwv <= 99999)) //_class 9 lease
    {
        if (cmnv > 6) {
            cmnv = 6;
        } //_class 9 credit max credit 1/2 of annual tax amount
        cctot = ((17.00 + (1.50 * (Math.round(ctwv / 100))))) * (cmnv / 12);
    } //17.00 Flat of which 4.50 is GR and 1.50 per cwt of which .50 is GR
    if ((cccv === 14) && (ctwv > 0) && (ctwv <= 99999)) // non-resident military
    {
        cctot = 4.00 * (cmnv / 12);
    } // 4.00 Flat - 1 of which is GR

    if ((cccv === 31) && (ctwv > 0) && (ctwv <= 1999)) // truck _class 31(31)
    {
        cctot = 14.50 * (cmnv / 12);
    } //14.50 Flat
    if ((cccv === 31) && (ctwv > 1999) && (ctwv <= 3000)) //truck _class 31(32)
    {
        cctot = 22.50 * (cmnv / 12);
    } //22.50 Flat
    if ((cccv === 31) && (ctwv > 3000) && (ctwv <= 5000)) //truck _class 31(33)
    {
        cctot = 32.50 * (cmnv / 12);
    } //32.50 Flat
    if ((cccv === 36) && (ctwv > 1951) && (ctwv <= 50049)) //_class 36 bus
    {
        cctot = ((17.00 + (2.00 * (Math.round(ctwv / 100))))) * (cmnv / 12);
    } //////////////////////////////??????
    if ((cccv === 37) && (ctwv > 0) && (ctwv <= 80000)) //base tax fees 0.00 - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    } //no credit
    if ((cccv === 39) && (ctwv > 0) && (ctwv <= 99999)) //Forestry use trucks
    {
        cctot = 324.00 * (cmnv / 12);
    } //324.00 Flat-84.00 of which is GR (General Revenue)
    if ((cccv === 42) && (ctwv > 0) && (ctwv <= 4499)) //_class 42 motorcoach, chassis mount truck camper, motor home
    {
        cctot = 27.00 * (cmnv / 12);
    } //27.00 Flat - 7 which is GR
    if ((cccv === 42) && (ctwv > 4499) && (ctwv <= 99999)) //_class 42, Motor coach, motor home, chassis mount truck camper,
    {
        cctot = 47.25 * (cmnv / 12);
    } //47.25 Flat - 12.25 which is GR //_class 42 ends
    if ((cccv === 50) && (ctwv > 0) && (ctwv <= 99999)) //non-res military MH (HS)
    {
        cctot = 4.00 * (cmnv / 12);
    } //4.00 Flat - 1.00 of which is GR
    if ((cccv === 51) && (ctwv > 0) && (ctwv <= 35)) //base tax - begin 51 HS
    {
        cctot = 20.00 * (cmnv / 12);
    } //20.00 flat
    if ((cccv === 51) && (ctwv > 35) && (ctwv <= 40)) {
        cctot = 25.00 * (cmnv / 12);
    } //25.00 flat
    if ((cccv === 51) && (ctwv > 40) && (ctwv <= 45)) {
        cctot = 30.00 * (cmnv / 12);
    } //30.00 flat
    if ((cccv === 51) && (ctwv > 45) && (ctwv <= 50)) {
        cctot = 35.00 * (cmnv / 12);
    } //35.00 flat
    if ((cccv === 51) && (ctwv > 50) && (ctwv <= 55)) {
        cctot = 40.00 * (cmnv / 12);
    } //40.00 flat
    if ((cccv === 51) && (ctwv > 55) && (ctwv <= 60)) {
        cctot = 45.00 * (cmnv / 12);
    } //45.00 flat
    if ((cccv === 51) && (ctwv > 60) && (ctwv <= 65)) {
        cctot = 50.00 * (cmnv / 12);
    } //50.00 flat
    if ((cccv === 51) && (ctwv > 65) && (ctwv <= 99)) {
        cctot = 80.00 * (cmnv / 12);
    } //80.00 flat  // end 51 HS
    if ((cccv === 52) && (ctwv > 0) && (ctwv <= 500)) //_class 52 trailer < 500 lbs
    {
        cctot = 6.75 * (cmnv / 12);
    } // 6.75 Flat - 1.75 which is GR
    if ((cccv === 53) && (ctwv > 500) && (ctwv <= 99999)) //base tax _class 53
    {
        cctot = ((3.50 + (1.00 * (Math.round(ctwv / 100))))) * (cmnv / 12);
    } //53credit per100lbs //3.50 Flat - which 1 is GR and 1.00 per cwt of which .25 is GR
    if ((cccv === 54) && (ctwv > 0) && (ctwv <= 1999)) // _class 54
    {
        if (cmnv > 6) {
            cmnv = 6;
        }
        cctot = ((3.50 + (1.50 * (Math.round(ctwv / 100))))) * (cmnv / 12);
    } //3.50 Flat - which 1 is GR and 1.50 per cwt of which .50 is GR
    if ((cccv === 54) && (ctwv > 1999) && (ctwv <= 99999)) //_class 54
    {
        if (cmnv > 6) {
            cmnv = 6;
        }
        cctot = ((13.50 + (1.50 * (Math.round(ctwv / 100))))) * (cmnv / 12);
    } //13.50 Flat - which 3.50 is GR and 1.50 per cwt of which .50 is GR  //_class 54 end
    if ((cccv === 56) && (ctwv >= 0) && (ctwv <= 99999)) //_class 56 semi-trailer - flat fee
    {
        cctot = 13.50 * (cmnv / 12);
    } //13.50 Flat-3.50 of which is GR
    if ((cccv === 62) && (ctwv >= 0) && (ctwv <= 99999)) //62 camp trailer
    {
        cctot = 13.50 * (cmnv / 12);
    } //13.50 Flat - 3.50 of which is GR
    if ((cccv === 65) && (ctwv > 0) && (ctwv <= 99999)) //65 MC
    {
        cctot = 10.00 * (cmnv / 12);
    } //10.00 Flat
    if ((cccv === 69) && (ctwv >= 0) && (ctwv <= 99999)) //69 moped /motorized bike
    {
        cctot = 5.00 * (cmnv / 12);
    } //5.00 Flat
    if ((cccv === 70) && (ctwv >= 0) && (ctwv <= 99999)) //70 transporter
    {
        cctot = 101.25 * (cmnv / 12);
    } // 101.25 flat - 26.25 of which is GR
    if ((cccv === 71) && (ctwv >= 0) && (ctwv <= 99999)) //dealer plates
    {
        cctot = 17.00 * (cmnv / 12);
    } // 17.00 flat - 4.50 of which is GR
    if ((cccv === 74) && (ctwv >= 0) && (ctwv <= 99999)) //boat trailer dealer plates
    {
        cctot = 17.00 * (cmnv / 12);
    } // 17.00 flat - 4.50 of which is GR
    if ((cccv === 76) && (ctwv > 0) && (ctwv <= 99999)) //76 Park Trailer
    {
        cctot = 25.00 * (cmnv / 12);
    } //25.00 Flat
    if ((cccv === 77) && (ctwv > 0) && (ctwv <= 35)) //77 travel trailer thru 35
    {
        cctot = 27.00 * (cmnv / 12);
    } //27.00 Flat- 7 of which is GR
    if ((cccv === 78) && (ctwv > 35) && (ctwv <= 99999)) //78 travel trailer over 35
    {
        cctot = 25.00 * (cmnv / 12);
    } //25.00 Flat
    if ((cccv === 80) && (ctwv > 0) && (ctwv <= 99999)) //antique motorcycle
    {
        cctot = 7.50 * (cmnv / 12);
    } //7.50 Flat-2.50 of which is GF
    if ((cccv === 82) && (ctwv > 0) && (ctwv <= 99999)) //_class 82 Horseless Carriage
    {
        cctot = 7.50 * (cmnv / 12);
    } //7.50 Flat
    if ((cccv === 91) && (ctwv >= 0) && (ctwv <= 99999)) //_class 91/95 Antique truck/military trailer
    {
        cctot = 7.50 * (cmnv / 12);
    } //7.50 Flat
    if ((cccv === 92) && (ctwv >= 0) && (ctwv <= 99999)) //ambulance,hearse,non-41 wrecker
    {
        cctot = 40.50 * (cmnv / 12);
    } //40.50 flat- of which 10.50 is GR
    if ((cccv === 93) && (ctwv >= 0) && (ctwv <= 99999)) //goat 93
    {
        cctot = 7.50 * (cmnv / 12);
    } //7.50 Flat
    if ((cccv === 94) && (ctwv >= 0) && (ctwv <= 99999)) //tractor crane (tools)
    {
        cctot = 44.00 * (cmnv / 12);
    } //44.00 Flat-11.50 of which is GR
    if ((cccv === 95) && (ctwv >= 0) && (ctwv <= 99999)) //antique car
    {
        cctot = 7.50 * (cmnv / 12);
    } //7.50 Flat
    if ((cccv === 96) && (ctwv >= 0) && (ctwv <= 99999)) //x-series exempt
    {
        cctot = 4.00 * (cmnv / 12);
    } // 4.00 Flat - 1.00 of which is GR
    if ((cccv === 97) && (ctwv >= 0) && (ctwv <= 99999)) //permanent,all govt - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    }
    if ((cccv === 100) && (ctwv > 0) && (ctwv <= 99999)) //vessels - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    }
    if ((cccv === 101) && (ctwv > 0) && (ctwv <= 99999)) //vessel dealer - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    }
    if ((cccv === 102) && (ctwv > 5000) && (ctwv <= 43999)) //Agricultural use trucks
    {
        cctot = 87.75 * (cmnv / 12);
    } //87.75 Flat-22.75 of which is GR
    if ((cccv === 102) && (ctwv > 43999) && (ctwv <= 99999)) //Agricultural use trucks
    {
        cctot = 324.00 * (cmnv / 12);
    } //324.00 Flat-84.00 of which is GR
    if ((cccv === 103) && (ctwv > 0) && (ctwv <= 99999)) //_class 103 permanent (semi)TRL - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    } //no credit
    if ((cccv === 104) && (ctwv > 0) && (ctwv <= 99999)) //vessel exempt - no credit
    {
        cctot = 0.00 * (cmnv / 12);
    }
    // add in a yes/no app alert - if _class 41 is also a wrecker:

    // _class 41 wrecker credit starts by weight category)
    if ((cccv === 41) && (ctwv > 5000)) {
        const cResponseA41 = window.confirm('\nIs this class code 41 also a wrecker?\n\nOK = Yes\nCancel = No');

      if (cResponseA41) {
          if ((cccv === 41) && (ctwv > 5000) && (ctwv <= 5999)) {
              cctot = 60.75 * (cmnv / 12);
          } //60.75 Flat-15.75 of which is GR
          if ((cccv === 41) && (ctwv > 5999) && (ctwv <= 7999)) {
              cctot = 87.75 * (cmnv / 12);
          } //87.75 Flat-22.75 of which is GR
          if ((cccv === 41) && (ctwv > 7999) && (ctwv <= 9999)) {
              cctot = 103.00 * (cmnv / 12);
          } //103.00 Flat-27.00 of which is GR
          if ((cccv === 41) && (ctwv > 9999) && (ctwv <= 14999)) {
              cctot = 118.00 * (cmnv / 12);
          } //118.00 Flat-31.00 of which is GR
          if ((cccv === 41) && (ctwv > 14999) && (ctwv <= 19999)) {
              cctot = 177.00 * (cmnv / 12);
          } //177.00 Flat-46.00 of which is GR
          if ((cccv === 41) && (ctwv > 19999) && (ctwv <= 26000)) {
              cctot = 251.00 * (cmnv / 12);
          } //251.00 Flat-65.00 of which is GR
          if ((cccv === 41) && (ctwv > 26000) && (ctwv <= 34999)) {
              cctot = 324.00 * (cmnv / 12);
          } //324.00 Flat-84.00 of which is GR
          if ((cccv === 41) && (ctwv > 34999) && (ctwv <= 43999)) {
              cctot = 405.00 * (cmnv / 12);
          } //405.00 Flat-105.00 of which is GR
          if ((cccv === 41) && (ctwv > 43999) && (ctwv <= 54999)) {
              cctot = 772.00 * (cmnv / 12);
          } //772.00 Flat-200.00 of which is GR
          if ((cccv === 41) && (ctwv > 54999) && (ctwv <= 61999)) {
              cctot = 915.00 * (cmnv / 12);
          } //915.00 Flat-237.00 of which is GR
          if ((cccv === 41) && (ctwv > 61999) && (ctwv <= 71999)) {
              cctot = 1080.00 * (cmnv / 12);
          } //1080.00 Flat-280.00 of which is GR
          if ((cccv === 41) && (ctwv > 71999) && (ctwv <= 80000)) {
              cctot = 1322.00 * (cmnv / 12);
          } //1322.00 Flat-343.00 of which is GR
      } else if (!cResponseA41) {
        // else if class 41 is not a wrecker then continue with the following:
        if ((cccv === 41) && (ctwv > 5000) && (ctwv <= 5999)) {
            cctot = 60.75 * (cmnv / 12);
        } //60.75 Flat-15.75 of which is GR
        if ((cccv === 41) && (ctwv > 5999) && (ctwv <= 7999)) {
            cctot = 87.75 * (cmnv / 12);
        } //87.75 Flat-22.75 of which is GR
        if ((cccv === 41) && (ctwv > 7999) && (ctwv <= 9999)) {
            cctot = 103.00 * (cmnv / 12);
        } //103.00 Flat-27.00 of which is GR
        if ((cccv === 41) && (ctwv > 9999) && (ctwv <= 14999)) {
            cctot = 118.00 * (cmnv / 12);
        } //118.00 Flat-31.00 of which is GR
        if ((cccv === 41) && (ctwv > 14999) && (ctwv <= 19999)) {
            cctot = 177.00 * (cmnv / 12);
        } //177.00 Flat-46.00 of which is GR
        if ((cccv === 41) && (ctwv > 19999) && (ctwv <= 26000)) {
            cctot = 251.00 * (cmnv / 12);
        } //251.00 Flat-65.00 of which is GR
        if ((cccv === 41) && (ctwv > 26000) && (ctwv <= 34999)) {
            cctot = 324.00 * (cmnv / 12);
        } //324.00 Flat-84.00 of which is GR
        if ((cccv === 41) && (ctwv > 34999) && (ctwv <= 43999)) {
            cctot = 405.00 * (cmnv / 12);
        } //405.00 Flat-105.00 of which is GR
        if ((cccv === 41) && (ctwv > 43999) && (ctwv <= 54999)) {
            cctot = 773.00 * (cmnv / 12);
        } //773.00 Flat-201.00 of which is GR
        if ((cccv === 41) && (ctwv > 54999) && (ctwv <= 61999)) {
            cctot = 916.00 * (cmnv / 12);
        } //916.00 Flat-238.00 of which is GR
        if ((cccv === 41) && (ctwv > 61999) && (ctwv <= 71999)) {
            cctot = 1080.00 * (cmnv / 12);
        } //1080.00 Flat-280.00 of which is GR
        if ((cccv === 41) && (ctwv > 71999) && (ctwv <= 80000)) {
            cctot = 1322.00 * (cmnv / 12);
        } //1322.00 Flat-343.00 of which is GR
      }
    } // class 41 credit ends

    if ((cccv === 0)) {
        cctot = 0 * (cmnv / 12);
    }

    if (cctot === 0) {
      alert('No credit amount allowed for this class code.');
      return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
    }

    if (cctot <= 3 && cctot > 0) {
      alert('Credit under $3.00 not allowed');
      return Number.MIN_SAFE_INTEGER; // important to return 0 so we can check for these conditions for validation
    } else {
      cctot *= -1;
    }
  }
  return precise(cctot);
}
module.exports = calculateCredit;
\$\endgroup\$
3
  • 4
    \$\begingroup\$ Please edit your question to state what the code does, not what you would like to improve about it (we will review all aspects for improvements, including repetition). \$\endgroup\$
    – Phrancis
    Oct 22, 2017 at 15:27
  • 2
    \$\begingroup\$ Two words "table driven". \$\endgroup\$
    – jfriend00
    Oct 22, 2017 at 15:38
  • \$\begingroup\$ To check if a variable is between two numbers i usually put both checks in the same parenthesis, with the variable in the middle. So i would type (1999 < ctwv && ctwv <= 3000) instead of (ctwv > 1999) && (ctwv <= 3000). No big deal, but kind of a readability thing to me :o) \$\endgroup\$
    – agrm
    Oct 22, 2017 at 16:19

1 Answer 1

2
\$\begingroup\$

I was going to rewrite the code, but there's some odd stuff, so I'll just give you some notes to consider:

  • There are redundant checks of cccv to 0 after it has already been checked with !cccv.

  • There are many redundant comparisons of cccv to the same number that can be eliminated by having all sub-comparisons grouped under a single comparison.

    In general, the repeating pattern is a comparison of cccv followed by a check of cctw being in a range. Imagine a data structure that can perform a lookup of cccv, which gives a list of ranges, each of which have either a corresponding function or a number by which you can multiply (cmnv/12).

  • The cmnv/12 appears to happen on every branch. Put it once toward the end!

  • Your code that runs when cccv is 41 performs many more comparisons of cccv === 41 even though we already know it's 41.

  • The if (cResponseA41) is followed by an else if (!cResponseA41), which is redundnant, since that's the only other possibility. A simple else should be used.

  • The many branches under if (cResponseA41) and its corresponding (now corrected) else are identical. If that's the case, what's the point of the confirm dialog? If not, then something needs to change, and it will likely involve using a separate function to get the single value that differs in order to reduce the redundancy in all the sub branches.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.