ternary-operator

JS Ternary functions with multiple conditions?

戏子无情 提交于 2019-12-03 07:03:24
I have been using a ternary operator in JavaScript to modify the value of an object based on user input. I have the following code, which runs as it should: var inputOneAns = inputOne == "Yes" ? "517" : "518"; As you can see, I am assigning a numeric string value to inputOneAns whether a user has inputed "Yes" or "No". However, there may be a case that a user has not selected a value (as it is not required). If this input was left blank, I would like to assign an empty string "" to inputOneAns . Is there a wayf or me to embed an ternary operator inside of another ternary operator? To help

Using && in EL results in error: The entity name must immediately follow the '&' in the entity reference

佐手、 提交于 2019-12-03 06:05:25
I'm trying to use a conditional expression in an el expression used in jsf, but it does not work. <h:outputText value="#{(sel.description !=null) && (sel.description !='') ? sel.description : 'Empty description'} - "/> but it does not work, the compiler says: Error Traced[line: 118] The entity name must immediately follow the '&' in the entity reference. Do you have any suggestions? Thank you! You seem to be using Facelets (which is perfectly fine). It's however a XML based view technology. Everything which you write in Facelets has to be syntactically valid XML. The & is in XML a special

Html literal in Razor ternary expression

百般思念 提交于 2019-12-03 04:27:19
I'm trying to do something like the following <div id="test"> @( string.IsNullOrEmpty(myString) ? @:  : myString ) </div> The above syntax is invalid, I've tried a bunch of different things but can't get it working. Try the following: @Html.Raw(string.IsNullOrEmpty(myString) ? " " : Html.Encode(myString)) But I would recommend you writing a helper that does this job so that you don't have to turn your views into spaghetti: public static class HtmlExtensions { public static IHtmlString ValueOrSpace(this HtmlHelper html, string value) { if (string.IsNullOrEmpty(value)) { return new HtmlString(" 

How do you use the Optional variable in a ternary conditional operator?

北城以北 提交于 2019-12-03 03:15:58
问题 I want to use an Optional variable with the ternary conditional operator but it is throwing error this error: optional cannot be used as boolean. What am I doing wrong? var str1: String? var myBool:Bool myBool = str1 ? true : false 回答1: You can not assign string value to bool but You can check it str1 is nil or not like this way : myBool = str1 != nil ? true : false print(myBool) It will print false because str1 is empty. 回答2: Nil Coalescing Operator can be used as well. The code below uses

Expressions in JavaScript Ternary Operator and JSLint

不问归期 提交于 2019-12-03 01:22:06
I recently received a comment on one of my blog posts about JSLint asking why JSLint threw an error with the following: s === "test" ? MyFunc() : MyFunc2(); The error generated was: "Expected an assignment or function call and instead saw an expression." Clearly JSLint is expecting an assignment here, somthing more like: var y = (s === "test") ? MyFunc() : MyFunc2(); But, I don't really see the problem with the first example. Is it really the case that ternary operators should only be used for assignments? I couldn't see anything on JSLint.com , nor was there anything apparent in the book

javascript ternary operator opposed to if/else

爱⌒轻易说出口 提交于 2019-12-02 18:48:20
问题 I am trying to write this if/else statement using javascript's ternary operator syntax. Is it possible to write this as a ternary operator? function changePlayer() { if (currentPlayer === playerOne) { currentPlayer = playerTwo } else { currentPlayer = playerOne } }; My current attempt is: function changePlayer(){ currentPlayer === playerOne ? playerTwo : playerOne; } 回答1: You just miss the assignment statement. So the final example will go like this: function changePlayer(){ currentPlayer =

Assigning using ternary operator?

十年热恋 提交于 2019-12-02 18:06:04
I am on Perl 5.8 and am needing to assign a default value. I ended up doing this: if ($model->test) { $review = "1" } else { $review = '' } The value of $model->test is going to be either "1" or undefined. If there's something in $model->test , set $review to "1" otherwise set it equal to '' . Because it's not Perl 5.10 I can't use the new swanky defined-or operator. My first reaction was to use the ternary operator like this... defined($model->test) ? $review = "1" : $review = ''; but that didn't work either. Does anyone have an idea how to assign this more efficiently? Janie I'd usually

Ternary in Laravel Blade

…衆ロ難τιáo~ 提交于 2019-12-02 15:24:01
Looking for a ternary operator for blade templates @if(Auth::check()) ? yes : no @endif Can't seem to get it to work this works @if(Auth::check()) yes @else no @endif suppose there is not much in it for this example, just curious. You are free to use it with {{ }} . {{ Auth::check() ? 'yes' : 'no' }} This works: {{ Auth::check() ? 'yes' : 'no' }} Bryce I know this question was asked a while ago, but this may help somebody. You can now do this in Laravel 5. {{ $variable or "default" }} Laravel 5 Blade Templates Laravel 5.2 Blade Template in addition, here is a nice shortcut ?: , if you you need

How do you use the Optional variable in a ternary conditional operator?

谁说我不能喝 提交于 2019-12-02 15:22:54
I want to use an Optional variable with the ternary conditional operator but it is throwing error this error: optional cannot be used as boolean. What am I doing wrong? var str1: String? var myBool:Bool myBool = str1 ? true : false Dharmesh You can not assign string value to bool but You can check it str1 is nil or not like this way : myBool = str1 != nil ? true : false print(myBool) It will print false because str1 is empty. Nil Coalescing Operator can be used as well. The code below uses the ternary conditional operator and forced unwrapping (a!) to access the value wrapped inside a when a

Ternary with boolean condition in c#

…衆ロ難τιáo~ 提交于 2019-12-02 14:59:41
问题 If I am to write this piece of code, it works fine with the normal 'if-else' layout. if(isOn) { i = 10; } else { i = 20; } Although I am unsure how to convert this using the ternary operator isOn = true ? i = 1 : i = 0; Error: Type of conditional expression cannot be determined because there is no implicitly conversion between 'void' and 'void'. EDIT: Answer = i = isOn ? 10 : 20; Is it possible to do this with methods? if(isOn) { foo(); } else { bar(); } 回答1: Please try the following. BTW, it