Reputation: 5051
First, I do not do JS so apologies in advance for the noob question.
Like the title says, I have this line:
stringValue = numericValue >= 1000 ? numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB" : numericValue + "GB";
Basically, what it does is if the value is over 1000, it appends "TB", and under 1000 it appends "GB". I need a third condition where if it's under 50 it gets rid of the text altogether.
I can see that this is like an if/else statement, but I can't quite decode it and figure out how to add in the third condition. Thanks in advance!
Upvotes: 1
Views: 122
Reputation: 77
pretty easy
stringValue = numericValue >= 1000 ? numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB" : numericValue >= 50? numericValue + "GB":"do here your 'below 50 stuff'";
basically it works that way:
(condition)?(return case true):(return case false);
so you can also nest it
(condition1)?(return case true1):((condition2)?(return case true2):(return case false both));
in your case, you have 3 cases. There are several other solutions:
a classic if(){} else if(){} else{}
statement
switch: W3Schools JavaScript Switch Statement like this:
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
In your case I would consider using the switch version (easier to read) or a if, else if, else. Nesting the ternary operator ' ? : ' is not easy to read.
For the switch you can do something like
var numericValue = //a numeric value;
switch (true) {
case (numericValue <= 50):
//do case below 50
break;
case (numericValue > 50 && numericValue <= 1000):
//do case above 50 and below 1000
break;
default:
//do case above 1000
break;
}
Here we switch over the value 'true'. That means we look the different cases and check for the one that is 'true' (or default)
Upvotes: 1
Reputation: 18848
value = function(numericValue) {
numString = numericValue.toString();
length = numString.length -3;
subString = numString.substr(0, length);
if(numericValue<50) { return ""; }
else if(numericValue< 1000) { return numString + "GB"; }
else { return substring + "TB"; }
}
Don't make your code overly complicated for no reason. Keep it simple and clearly show what you are intending to do. This will prevents headaches for you and anyone else maintaining your code in the future.
EDIT - Fixed line "return numString + "GB"".
Upvotes: 1
Reputation: 16020
var x = 5;
var y = x > 1000 ? "big" : x > 50 ? "medium" : "small"
You can chain the ternary operator.
Upvotes: 1
Reputation: 16039
This should work:
stringValue = numericValue >= 1000 ? numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB" : (numericValue > 50 ? numericValue + "GB" : numericValue);
Upvotes: 1
Reputation: 7896
You can benefit from a little indentation. Breaking it like this makes it more readable:
stringValue =
numericValue >= 1000
? numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB"
: numericValue + "GB";
Now, to add another condition in, say the else part, you could do it like this:
stringValue =
numericValue >= 1000
? numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB"
: numericValue < 1
? "Negligible"
: numericValue + "GB";
Upvotes: 1
Reputation: 114367
This translates to:
stringValue = function(numericValue) {
if(thirdCondition) {
return "something";
}
if(numericValue >= 1000) {
return numericValue.toString().substr(0, numericValue.toString().length - 3) + "TB";
}
else {
return numericValue + "GB";
}
}
I'm not sure what your third condition is, but you get the picture.
Upvotes: 2