Reputation: 49
I am creating same if else function on my program. And I want to make it more short but same logic.
if(!checkStringInput("Test", 14)){
elementHasError($element);
} else {
elementIsOk($element);
}
I am thinking this boolean condition can be a shorter one to:
CONDITION ? FUNCTION1; : FUNCTION2;
Thanks for the help!
Upvotes: 1
Views: 85
Reputation: 1921
You seemed to have it correct. Were you looking for something else beyond the terniary operation?
!checkStringInput('Test', 14) ? elementHasError($element) : elementIsOk($element);
Upvotes: 0
Reputation: 38005
Probably the shortest possible:
(checkStringInput("Test", 14) ? elementIsOk : elementHasError)($element);
Upvotes: 3
Reputation: 21376
!checkStringInput("Test", 14)? elementHasError($element): elementIsOk($element);
Upvotes: 0
Reputation: 21852
Have you tried this
!checkStringInput("Test", 14)) ? elementHasError($element) : elementIsOk($element);
Upvotes: 3