skip to Main Content

I have a series of IF statements that I think should be automated into a shorter piece of code but I’m not sure how to go about doing it. Here’s an example of the code:

var supplier_kw_per_rack = 2;

if (kw_value === '1+kW'){ var kw_integer = 1; var ongoing_kw = 350; }
if (kw_value === '2+kW'){ var kw_integer = 2; var ongoing_kw = 350; }
if (kw_value === '3+kW'){ var kw_integer = 3; var ongoing_kw = 700; }
if (kw_value === '4+kW'){ var kw_integer = 4; var ongoing_kw = 700; }
if (kw_value === '5+kW'){ var kw_integer = 5; var ongoing_kw = 1050; }
if (kw_value === '6+kW'){ var kw_integer = 6; var ongoing_kw = 1050; }
if (kw_value === '7+kW'){ var kw_integer = 7; var ongoing_kw = 1400; }
if (kw_value === '8+kW'){ var kw_integer = 8; var ongoing_kw = 1400; }
if (kw_value === '9+kW'){ var kw_integer = 9; var ongoing_kw = 1750; }
if (kw_value === '10+kW'){ var kw_integer = 10; var ongoing_kw = 1750; }
if (kw_value === '11+kW'){ var kw_integer = 11; var ongoing_kw = 2100; }
if (kw_value === '12+kW'){ var kw_integer = 12; var ongoing_kw = 2100; }
if (kw_value === '13+kW'){ var kw_integer = 13; var ongoing_kw = 2450; }
if (kw_value === '14+kW'){ var kw_integer = 14; var ongoing_kw = 2450; }
if (kw_value === '15+kW'){ var kw_integer = 15; var ongoing_kw = 2800; }
if (kw_value === '16+kW'){ var kw_integer = 16; var ongoing_kw = 2800; }
if (kw_value === '17+kW'){ var kw_integer = 17; var ongoing_kw = 3150; }
if (kw_value === '18+kW'){ var kw_integer = 18; var ongoing_kw = 3150; }
if (kw_value === '19+kW'){ var kw_integer = 19; var ongoing_kw = 3500; }
if (kw_value === '20+kW'){ var kw_integer = 20; var ongoing_kw = 3500; }
if (kw_value === '21+kW'){ var kw_integer = 21; var ongoing_kw = 3850; }
if (kw_value === '22+kW'){ var kw_integer = 22; var ongoing_kw = 3850; }

Some context:

  • kw_value is the value of a selectbox value that’s been selected. The CMS doesn’t have a value attribute with just the number, so I can only match it to the IF statement by the value of what’s outputted once selected
  • kw_integer is just that – a clean integer of the selected selectbox option.
  • ongoing_kw goes up by a fixed 350 for every iteration of the value defined in supplier_kw_per_rack. In this instance, it’s every 2 racks, but if the value of supplier_kw_per_rack is changed to 4, the value of ongoing_kw needs only go up by the fixed 350 figure every multiple of 4 for kw_value/integer

I’ve got it working to this point where I don’t need to explicitly run the IF statement for each combination of selectbox value and I’ve found a way to automate the kw integer variable that is born from this statement. However I’m struggling now with finding a way to make the value of ongoing_kw go up by 350, depending on whether the kw value or kw integer is a multiple of supplier_kw_per_rack. Here’s an example of where I’m at:

if (kw_value === kw_value){ var kw_integer = Number(kw_value.substring(0, kw_value.length - 3)); var ongoing_kw = 350; }```

2

Answers


  1. In order to solve this issue, let’s break it down first:

    you have an input kw_value, and based on the initial integer in this input and the value of supplier_kw_per_rack, you want to assign values to kw_integer and ongoing_kw.

    Let’s say you can get the integer in the beginning of the kw_value, let’s call it initial_integer. the initial_integer value can be assigned to the kw_integer variable. ongoing_kw seems to be (((initial_integer - 1) // supplier_kw_per_rack) + 1) * 350. Divide initial_integer - 1 by supplier_kw_per_rack and only get the integer part add 1 to it. Then multiply that by 350.

    when you reduce 1 from the initial_integer, it gets a bit easier to think about

       reduce 1    divide by supplier_kw_per_rack       add 1      multiply by 350
    1     ->    0               ->                  0    ->    1          ->        350
    2     ->    1               ->                  0    ->    1          ->        350
    3     ->    2               ->                  1    ->    2          ->        700
    4     ->    3               ->                  1    ->    2          ->        700
    5     ->    4               ->                  2    ->    3          ->        1050
    6     ->    5               ->                  2    ->    3          ->        1050
    

    so, in conclusion, here will be your code

    let kw_value = <input>
    let supplier_kw_per_rack = 2
    var kw_integer = Number(kw_value.substring(0, kw_value.length - 3)) // like you've wrote
    var ongoing_kw = (((kw_integer - 1) // supplier_kw_per_rack) + 1) * 350
    
    Login or Signup to reply.
  2. I think the supplier_kw_per_rack is dynamic so you can use this function and get the result.

    const getOngoing_kw = function(kw_value, supplier_kw_per_rack, fixed = 350) {
      // I assume your input value is a string like '12+kW'. 
      // convert any input to string type
      let inputString = '' + kw_value;
      // Get the number out of the string.
      let num = inputString.match(/d+/)[0];
    
      // Create the iteration number
      const iteration = Math.ceil(num / supplier_kw_per_rack);
      
      // Return the Object 
      return { kw_integer: num, ongoing_kw: iteration * fixed }
    }
    
    
    // test the function
    for(let i = 1; i <= 24; i++) {
      console.log(`${i}+kW  = `, getOngoing_kw(`${i}+kW`,2) )
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search