Could someone please tell me how I could simplify this code?

In practice I have this code and it seems very tiring to do all those if there is a way to make it easier?
But that it works correctly? Because this code not only has * (multiplications) it also has 3 / (divisions).
If it only had multiplications it was easy to do it but it also has those divisons so it becomes more difficult to do so.
If anyone could help me thanks.

        function calculateLength() {
        
        
        
            var lengthinput = parseFloat(document.getElementById('lengthinput').value);


            var oper = document.getElementById('lengthselector1').value;
            var oper2 = document.getElementById('lengthselector2').value;


            if(oper === 'k' && oper2 === 'm')
            {
                document.getElementById('lengthresult').value = lengthinput*1000 || 0;
            }

            
            if(oper === 'k' && oper2 === 'd')
            {
                document.getElementById('lengthresult').value = lengthinput*10000 || 0;
            }

            if(oper === 'k' && oper2 === 'c')
            {
                document.getElementById('lengthresult').value = lengthinput*100000 || 0;
            }
            
            if(oper === 'k' && oper2 === 'mi')
            {
                document.getElementById('lengthresult').value = lengthinput*1000000 || 0;
            }

            if(oper === 'k' && oper2 === 'mic')
            {
                document.getElementById('lengthresult').value = lengthinput*1.0000E+9 || 0;
            }               
            
            if(oper === 'k' && oper2 === 'de')
            {
                document.getElementById('lengthresult').value = lengthinput*100 || 0;
            }

            if(oper === 'k' && oper2 === 'h')
            {
                document.getElementById('lengthresult').value = lengthinput*10 || 0;
            }   

            if(oper === 'k' && oper2 === 'me')
            {
                document.getElementById('lengthresult').value = lengthinput/1000 || 0;
            }
            
            if(oper === 'k' && oper2 === 'g')
            {
                document.getElementById('lengthresult').value = lengthinput/1000000 || 0;
            }

            if(oper === 'k' && oper2 === 'z')
            {
                document.getElementById('lengthresult').value = lengthinput/1.0000E+18 || 0;
            }               
            
            if(oper === 'k' && oper2 === 'i')
            {
                document.getElementById('lengthresult').value = lengthinput*39370.0787 || 0;
            }

            if(oper === 'k' && oper2 === 'a')
            {
                document.getElementById('lengthresult').value = lengthinput*1.0000E+13 || 0;
            }           
                            

        }

3 thoughts on “Could someone please tell me how I could simplify this code?”

  1. All your if conditions test for oper==='k', so that can become just one if statement around all the rest. For oper2 make a lookup table:

    function calculateLength() {
        var lengthinput = parseFloat(document.getElementById('lengthinput').value);
        var oper = document.getElementById('lengthselector1').value;
        if (oper === 'k') {
            var oper2 = document.getElementById('lengthselector2').value;
            var coefficient = {
                'i': 39370.0787,
                'z': 1e-18,
                'g': 1e-6,
                'me': 1e-3,
                'h': 10,
                'de': 100,
                'm': 1e3,
                'd': 1e4,
                'c': 1e5,
                'mi': 1e6,
                'mic': 1e9,
                'a': 1e13
            }[oper2];
            document.getElementById('lengthresult').value = lengthinput*coefficient || 0;
        }
    }
    
    Reply
  2. You can try this:

    function calculateLength() {
      const data = [
        {
          oper1: "k",
          oper2: "m",
          time: 1000,
        },
        {
          oper1: "k",
          oper2: "d",
          time: 10000,
        },
      ];
      //---
      var lengthinput = parseFloat(document.getElementById("lengthinput").value);
      //---
      var oper = document.getElementById("lengthselector1").value;
      var oper2 = document.getElementById("lengthselector2").value;
      //---
      for (var count = 0; count < data.length; count++) {
        if (data[count].oper1 === oper && data[count].oper2 === oper2) {
          document.getElementById("lengthresult").value =
            lengthinput * data[count].time || 0;
        }
      }
    }
    
    Reply
  3. You could use a lookup table for the coefficient, e.g.

    const coefficient = { k: { mic: 1.0000E+9} }
    

    and use it with

    document.getElementById('lengthresult').value = lengthinput*coefficient[oper][oper2] || 0;
    

    Example:

    function calculateLength() {
        const coefficient = { k: { mic: 1.0000E+9} }
        const lengthinput = parseFloat(document.getElementById('lengthinput').value);
    
    
        const oper = document.getElementById('lengthselector1').value;
        const oper2 = document.getElementById('lengthselector2').value;
    
        document.getElementById('lengthresult').value = lengthinput*coefficient[oper][oper2] || 0;
        
    }
    
    Reply

Leave a Comment