Reputation: 29
I am trying to add a conversation logic for converting 1 GB to value 1000; 2.5 GB to value 2500; 50 MB to value 50; 1 MB to value 1 and so on using javascript.
I tried adding logic but it is comprising multiple if else.
var bw;
var value;
if (bw.toUpperCase() == '1 GB' || bw.toUpperCase() == '1 GBPS')
{
value = 1000;
}
else if (bw.toUpperCase() == '2.5 GB' || bw.toUpperCase() == '2.5 GBPS')
{
value = 2500;
}
and so on for MB. The only values that will come are GB or MB. No kb or bytes.
Upvotes: 0
Views: 741
Reputation: 144689
One option is:
let units = { GB: 1000, GBPS: 1000, ... };
let [a, b] = yourString.split(' ');
let value = +a * units[b];
Upvotes: 1