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
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 ofsupplier_kw_per_rack
, you want to assign values tokw_integer
andongoing_kw
.Let’s say you can get the integer in the beginning of the
kw_value
, let’s call itinitial_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
. Divideinitial_integer - 1
bysupplier_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 aboutso, in conclusion, here will be your code
I think the supplier_kw_per_rack is dynamic so you can use this function and get the result.