Is there a contracted syntax that allows the execution of a certain function only if the left value is true?
Example :
ready() ? drawGraph();
I do not want this:
ready() ? drawGraph() : ELSEFUNCTION;
Thanks
Related
There are two types of if function codes:
condition1 ? function1 : condition2 ? function2 : function3;
if (condition1) {
function1
} else {
if (condition2) {
function2
} else {
function3
}
}
I don't know what is the correct way to use the if else function code, I hope everyone can tell me.
Feel free to leave a comment if you need more information.
Which if else code should I use, single line or multiple lines? I would appreciate any help. Thank you in advance!
Those two are not "different styles of if".
The first one is the ternary operator. As the term operator suggests, it is used to produce a value.
The second one is the flow control statement if. It does not produce any value, it changes your program's flow.
So which one should you use? The one that fits your goal best. Do you need a value? The operator. Do you need to change your program flow? The flow control statement.
Try to reproduce this if statement with a ternary operator:
if (trafficlight.current == red) {
stopVehicle();
}
You cannot. Not without adding pointless waste. Because this is flow control.
On the other hand, this:
var newSpeed = (trafficlight.current == red) ? 0 : this.MaxSpeed;
Would be very convoluted to write as an if statement. Because it is generating a value.
So pick what is best for your program. It's not a "style" to follow blindly. It is a decision you should make for every one of those instances.
I am trying to dynamically render class based off actionTypeCreate. This is a method that simply returns a boolean value based off the prop actionType that is passed. I am triggering this method on the mounted hook and confirmed it is returning properly.
Now I am trying to return the class value of 'col-md-4' if actionTypeCreate. If not actionTypeCreate I want to return the class 'col-md-6'.
This is what I have but it is not working:
:class="{toggleActionType : 'col-md-4' ? 'col-md-6'}"
I tried to reference this existing question, but I did not get it.
You can do it as follows:
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType }"
According to the Vue documentation itself you can do it in two ways. First, you can use Array Syntax and this is broadly used to apply a list of classes.
Array Syntax
:class="[toggleActionType ? 'col-md-4' : 'col-md-6']"
Or you can do it as normal by Object Syntax but it does not accept ternary operations, so you have to do it this way:
Object Syntax
:class="{'col-md-4' : toggleActionType , 'col-md-6' : !toggleActionType}"
Try this:
:class="[toggleActionType : 'col-md-4' ? 'col-md-6']"
I am trying to set the src property of sap.ui.core.Icon based upon data being fetched from the data model. Something like this:
<Icon src="{= ${propertyname} === 'somevalue' ? 'sap-icon://arrow-top' : 'sap-icon://arrow-bottom'}"/>
I have one additional condition in my case which implies that:
Set icon1 (say 'sap-icon://arrow-top') when property value is 'UP'
Set icon2 (say 'sap-icon://arrow-bottom') when property value is 'DOWN'
Set icon3 (say 'sap-icon://arrow-left') for all other cases
Is it possible to achieve this without the use of formatter function?
Simply nest another ternary operator inside your expression.
<Icon src="{= ${propertyname} === 'UP' ? 'sap-icon://arrow-top' : ${propertyname} === 'DOWN' ? 'sap-icon://arrow-bottom' : 'sap-icon://arrow-left'}"/>
I'm trying to decide which function to call, based on a boolean value.
myBooleanVariable ? function1() : function2();
Unity gives the error :
Expressions in statements must only be executed for their
side-effects.
So why does this not work, and how can I make it work ?
Thanks for any help !
So why does this not work, and how can I make it work ?
If it's true that it doesn't work (I don't have Unity to hand), it means UnityScript (Unity's implementation of JavaScript) doesn't support the expression statement. Which puts it at variance with the specification, and means a fair number of JavaScript idioms won't work in it. Your line is perfectly valid JavaScript/ECMAScript. You might check to see if there are "lint"-style options you can enable/disable.
The solution would be to use the result of the expression, or rewrite that using if.
Use the result:
var f = myBooleanVariable ? function1() : function2();
Using if:
if (myBooleanVariable) {
function1();
}
else {
function2();
}
Or if you really want the if to be on one line:
if (myBooleanVariable) function1(); else function2();
I have a String and I would like to check that this string is > 0
using assert
trying :
using assertFalse I can't set 2 strings : my value and "0" .
Is there another way to say : assertThat "myString"> "0" ?
my purpose :
myTable.getValue =>this return a string : "20"
I would like to check that this string is > or different from "0"
thanks
You really want to do the comparison as a number, for safety. So you want something like
assertTrue(Integer.parseInt(myTable.getValue()) > 0)
Additionally, with assertThat style you may want to use:
assertThat(myTable.length()).isGreaterThan(0);
you will need to import org.assertj.core.api.Assertions.assertThat static methods. I'd prefer this method as enhance test readability.