Add condition to angular expression? [duplicate] - javascript

This question already has answers here:
Angularjs if-then-else construction in expression
(5 answers)
Closed 7 years ago.
If I have an angular expression which resolves to a String, can I add a condition to that expression which will append extra text to that String?
I have:
<span>{{groupType}}</span>
But if groupType resolves to car or animal, I want to add an s to the end of the String.
Can I add a condition to the expression to accomplish this?

You can use Ternary operator in Angular expression
<span>{{groupType === 'Car' || groupType === 'Animal' ? groupType + 's' : groupType}}</span>

Yes you can:
<span>{{["Car", "Animal"].indexOf(groupType) >= 0 ? groupType + 's' : groupType}}</span>
I use an array of names with indexOf, because it's a little shorter than "x or y or z"
Ideally, you don't want this logic in your HTML, though. I'd suggest putting it in a function in your controller:
$scope.getGroupType = function(type){
if(["Car", "Anumal"].indexOf(type) >= 0)
return type + 's';
return type;
};
Then, in your HTML:
<span>{{getGroupType(groupType)}}</span>

Related

Javascript OR (||) with ternary operator [duplicate]

This question already has answers here:
Precedence: Logical or vs. Ternary operator
(2 answers)
Closed 3 years ago.
payload = {type: 3}
const type = payload.type || state.active === "period" ? 1 : 2;
// type returns 1
I'm surprised by the return of type which is 1.. I was expecting it to be 3.. What happened here? What I want to really achieve is if the type index is not available then state.active === "period" ? 1 : 2 will be the basis of the value of type..
How to achieve this in a clean one line?
You need parentheses, because of the operator precedence.
const type = payload.type || (state.active === "period" ? 1 : 2);

Regex only contain binary number and not more than 8 digit [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 3 years ago.
I expect true return value that match condition below :
Only binary number (0 or 1)
Length not more than 8
I'm new to regex, i have googling and read JS RegExp from https://www.w3schools.com/Js/js_regexp.asp but still i don't get it.
I've tried
/[0-1]$/
but still didn't match the condition above
I expect boolean return from regex test if there is contain no other number except 0 or 1 and length not more than 8.
data: {
binRegExp: /[0-1]$/,
isBinary: false,
binNum: '', // get value from user input
},
computed: {
inputCheck(){
return this.isBinary = this.binRegExp.test(this.binNum)
}
}
code above is vue js
If there is solution, please answer below. Thank you
this will takes 1 to 8 [1-0]
const regex = /^[0-1]{1,8}$/
const text = "1010010"
console.log(regex.test(text));
Try
/^[01]{1,8}$/
let strings = ['11110001','1111000111','11112001'];
strings.forEach(x=> /^[01]{1,8}$/.test(x) ? console.log(x,'pass') : console.log(x,'not pass'))

Get the first number in a string in script [duplicate]

This question already has answers here:
Get the first integers in a string with JavaScript
(5 answers)
Closed 3 years ago.
This script returns a value like 33 | 4 and I need only the the first whole number, in this case 33.
Can I use .replace( /[^\d].*/, '' ) and where/how to put this? Or is there better solutions?
You are all very helpful, but i'm in a stage where I need to see my full script or a line from it with you solutions implemented ;-)
Thanks
jQuery(document).ready(function($) {
$("#input_1_1").blur(function() {
// sets the value of #input_1_8 to
// that which was entered in #input_1_1
$("#input_1_8").val($("#input_1_1").val());
});
});
You can use parseInt() or split your string with | and get the first result
console.log(parseInt("33 | 4"))
// or
console.log("33 | 4".split(" | ")[0])
In your case you can do
jQuery(document).ready(function($) {
$("#input_1_1").blur(function() {
$("#input_1_8").val(parseInt($("#input_1_1").val()));
});
});
Try this,
var str = "33 | 4";
var res = str.split("|");//split with '|' it will return array
console.log(res[0].trim());//33
You can do trim on it or parseInt, whatever you want to get your task done.
With split you can split a string with any character, my example:
<script>
var stringexample="45.89 | 65";
alert(takefirst(sting));
}
function takefirst(){
var a=string.split("|");
return a[0]
}
</script>
Also you can use split for separete character for character like this
var a=stringexample.split("");
for(var i=0;i<a.lenght;i++){
alert(a[i]);
}
Alerts:
4
5
.
8
Etc..

what does the "?" and ":" sign mean in a line of java code? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is a Question Mark “?” and Colon “:” Operator Used for?
Question mark in java code
I am writing codes for a RBG to HSV converter.
I have this line:
var d = (r==minRGB) ? g-b : ((b==minRGB) ? r-g : b-r);
i dont really understand what the "?" and the ":" means here.
This is the short way to make a condition :
Condition ? Statment1 : Statement2;
Means
If (Condition) {Statement1} else {Statement2}
Its Ternary Operator:
C = condition? A : B
is equivalent to
if (condition){
C= A;
} else{
C= B;
}
It also support nesting i.e. C = condition1? A : condition2?D:E, which is equivalent to
if (condition1){
C=  A;
} else if (condition2){
C=  D;
} else{
C= E;
}
This is called ternary operator in java.
Based on java tutorial
Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement (discussed in the Control Flow Statements section of this lesson). This operator is also known as the ternary operator because it uses three operands.
If first expression results in true, then assign second operand as value, otherwise third operand as value.
It means
if (r==minRGB)
d = g-b
else
if(b==minRGB)
d=r-g
else
d=b-r
In c-based languages, it means:
? :
Its short-hand for an if-else, basically.
it work similar to if than else
if (r==minRGB)
d = g-b;
}else{
if (b==minRGB)
{
d = r-g;
}else{
d = b-r;
}
}
Its called ternary operator (?:): -
System.out.println(condition? value1 : value2);
The above expression is evaluated like: -
if (condition) {
System.out.println(value1);
} else {
System.out.println(value2);
}

Came across weird javascript operation [duplicate]

This question already has answers here:
How do you use the ? : (conditional) operator in JavaScript?
(20 answers)
Closed 8 years ago.
I am working on a project, designed by someone else. I came across the following operation. I have no idea what it is doing. It seems to be returning 1.
Anyone care to elaborate?
Thank you!
( 7 > 8?2:1)
You're looking at the Ternary Operator.
It consists of (condition) ? (expression1) : (expression2). The entire expression will evaluate to (expression1) if (condition) is true, and (expression2) if (condition) is false.
var i = (7 > 8 ? 2 : 1);
translates into
if (7 > 8)
{
i = 2;
}
else
{
i = 1;
}
See: http://en.wikipedia.org/wiki/%3F:
Your example would return 2 if 7 > 8, or 1 otherwise.
? : is a ternary operator. This is equivalent to
var x = 0;
if (7 > 8){
x = 2;
} else {
x = 1;
}
It is a terse way of expressing simple conditional statements. It is a great way to conditionally assign values to a variable without the verbose semantics used above.

Categories