Reputation: 5101
How to assign multiple values to ternary
operator? is that not possible? I tried like this, but getting error:
size === 3 ? ( var val1=999, var val2=100; ) : 0;
and
size === 3 ? ( var val1=999; var val2=100; ) : 0;
above both approach throws error. how to set both var val1
and var val2
;
I can directly declare it. But I would like to know the ternary operator approach here.
Upvotes: 7
Views: 7798
Reputation: 8542
The syntax of ternary operator is
condition ? expr1 : expr2
var val1=999; var val2=100;
is a valid declaration (var val1=999, var val2=100;
is not), but NOT an expression. So you can't use them the way you've done it in your code. However, you can make it into an expression by using the eval function like so:
size === 3 ? eval('var val1=999; var val2=100;') : 0;
Of course, as the others have pointed out. Using eval
is the wrong approach to take. I am showing you how it could be done for the sake of answering your question.
Upvotes: 0
Reputation: 621
var size=3;
var val1=null;
var val2=null;
size === 3 ? ( val1=999,val2=100 ) : 0;
console.log(val1,val2)
Upvotes: 8
Reputation: 28483
Use an if
statement.
var val1;
var val2;
if (size === 3) {
val1 = 999;
val2 = 100;
}
Whilst D-reaper's answer answers your question, really your question is not the right thing to ask. (Additionally eval
is useful very very occasionally but is otherwise considered "evil" as it prevents various JavaScript engine optimisations and opens security holes like XSS if used on stored user input data).
Ternary operators are useful when you're using them to assign to another variable like:
var val1 = size === 3 ? 999 : 0;
In your example the fact you do no assignment, the first expression does not intend to return a value and the second value of 0
is ignored and therefore redundant is a very strong code smell they should alert you to there being a better easier way of doing what you want.
Upvotes: 0
Reputation: 2691
I see what you are trying to do, you can use Math.pow()
to generate numbers instead of checking on size
manually. I have put down two methods below : createDigit
is mine which can generate numbers for any size given using Math.pow()
and createDigitBuggy
is yours which will just generate numbers for size 2 and 3 and rest will be NaN.
// Improved version
const createDigit = (size) => {
if (size > 0) {
const val1 = Math.pow(10, size) - 1
const val2 = Math.pow(10, size - 1)
return Math.floor(Math.random() * (val1 - val2 + 1)) + val2
}
return 0
}
// Old buggy version
const createDigitBuggy = (size) => {
var val1, val2
size === 3 ? (val1 = 999, val2 = 100) : size === 2 ? (val1 = 99, val2 = 10) : 0
return Math.floor(Math.random() * (val1 - val2 + 1)) + val2
}
console.log(createDigitBuggy(1)) // prints NaN
console.log(createDigitBuggy(2)) // prints number
console.log(createDigitBuggy(3)) // prints number
console.log(createDigitBuggy(4)) // prints NaN
console.log(createDigit(1)) // prints number
console.log(createDigit(2)) // prints number
console.log(createDigit(3)) // prints number
console.log(createDigit(4)) // prints number
Upvotes: -1
Reputation: 22500
Its a syntax error .You could use like this separate call
var size=3;
var val1 = size === 3 ? 999 : 0;
var val2 = size === 3 ? 100 : 0;
console.log(val1,val2)
Upvotes: 1
Reputation: 50291
You can do like this
var val1 = 0,
val2 = 0,
size = 3;
3 === size ? function() {
val1 = 999;
val2 = 100
}() : 0;
console.log(val1, val2);
Upvotes: 0
Reputation: 5101
Here is my try: it works fine for me:
createDigit : function( size ) {
var val1, val2;
size === 3 ? ( val1=999, val2=100 ) : size === 2 ? ( val1=99, val2=10 ) : 0;
//generates only 3 digit values
return Math.floor( Math.random()*(val1-val2+1 )) + val2;
},
Upvotes: -1