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:
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; }```
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