Is it possible to have conditions in return in JavaScript?
Like in this case:
if
(A > 0 || B > 0)
return "A";
else return "C"
As long as A or B is > 0 return the one that is > 0 (so A or B). Is that possible? Like another condition as return, for example?
Maybe this is similar to what you are trying to do. My function eval() takes two parameters A and B, and checks different conditions to see which is the positive number. I am using the Logical AND && instead of || because I want my code to know the value of each parameter in each condition.
function eval(A, B) {
if (A > 0 && B <= 0) {
return 'A';
}
if (A <= 0 && B > 0) {
return 'B';
}
if (A > 0 && B > 0) return "Both A and B are positive numbers";
}
console.log(eval(2, -5));
console.log(eval(0, 8));
console.log(eval(1, 1));
Try this.
return (A > 0 || B > 0) ? "A" : "C"
It's a ternary operator. Here you have more information:
Conditional (ternary) operator
if(A > 0 || B > 0)
return A > 0 ? "A" : "B";
else return "C";
Related
I have some truth tables, mostly AND, based on 0, 1, -1 which I am using for Math operations:
Example 1:
var a,b,c;
if(a == 1 && b == 1) c = 0;
if(a == 1 && b == 0) c = 0;
if(a == -1 && b == 1) c = 0;
if(a == -1 && b == 0) c = 1;
Example 2:
var a,b,c;
if(a === 1 && b === 1) c = 1;
if(a === 1 && b === 0) c = 1;
if(a === -1 && b === 1) c = 1;
if(a === -1 && b === 0) c = -1;
For instance, the first one could be expressed as follows:
c = (a>>1)*(b-1);
I would like to convert also the second truth table using only Math or Bitwise Operators, how can it be done?
Is there any generic rule to easily create such kind of one-line expression?
You can use a generic superoptimizer such as this one I wrote (unfortunately not very user friendly): https://github.com/falk-hueffner/sematrope
This will give you (a + b) >>> 31 for your first example and ((a + b) | b) & a for the second.
For a more systematic approach, you can convert -1/1 to 0/1 by (x >> 1) + 1, do regular logic operations with |&^, and then (if needed) convert 0/1 to -1/1 by x ^ (x - 1).
If a is always 1 or -1 and b is always 1 or 0, and you're OK to use a boolean operator (||), then
c = b || a
should do it...
const c = (a, b) => b || a
console.log(c( 1, 1)) // 1
console.log(c( 1, 0)) // 1
console.log(c(-1, 1)) // 1
console.log(c(-1, 0)) //-1
I have an boolean expression in javascript and i don't know what it means.
a = (b === LEFT && -2 || b === RIGHT && 2 || 0)
Please what does it mean ?
The && is a hacky shortcut if:
if (B === LEFT) {
a = -2;
} else if (B === RIGHT) {
a = 2;
} else {
a = 0;
}
one more shortcut with ternary operator
a = b === LEFT? -2: (b === RIGHT? 2 : 0)
This question already has answers here:
Why `null >= 0 && null <= 0` but not `null == 0`?
(6 answers)
Closed 4 years ago.
Is the below if statement
if (a >= b)
Equal to this?
if (a > b || a === b)
Or is it equal to this?
if (a > b || a == b)
It is equivalent to if(a > b || a == b)
var a = "2";
var b = 2;
console.log(a >= b); // true
console.log(a > b || a == b); // true (== compares value)
console.log(a > b || a === b); // false (=== compares value and type)
You can test it in the console:
var a = 0;
var b = '0';
a == b; // true
a === b; // false
a >= b; // true
Ergo, >= is equivalent to > || ==.
The actual result depend on the use case if the typeof both a & b is same then (a >= b) is same as (a > b || a === b). This is because == is equality with type coercion
var a = "2";
var b = "2";
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // true
var a = "4";
var b = 4;
console.log(a >= b); // true
console.log(a > b || a == b); // true
console.log(a > b || a === b); // false
Actually the first one
if(a >= b)
is similiar to
if(a > b || a == b)
but not equals to
if(a > b || a === b)
because in this last one you are even comparing the type of both the operands.
Example:
x = "5"
console.log(x==parseInt(x)) will return true
console.log(x===parseInt(x)) will return false
So, == does not consider the types of operands.
It depends. If before the statement you had defined a or b with a different type,
if(a > b || a === b)
will return false if the first clause is not true. However if you didn't define a or b before both will have the same type and both expressions are equivalent.
You can understand "===" as
(a == b && sameType(a,b))
I am trying to alert "yes" if ether of the conditions in my if statement are true:
var a = 2;
var b = 1;
if (a = 1 or b = 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
https://jsfiddle.net/90z7urvd/1/
What do I use for the if, if this is possible?
a = 1 will set the value 1 to variable a. It is not doing a comparison. For comparison, you use === or ==
=== (Identity operator) is the correct way to compare if both the types are same.
if (a === 1 || b === 1 ) {
=== operator won't do the type conversion before the comparison while == does the type conversion before the comparison.
For your or case, You may use || operator
var bootresul = someExpression || anotherExpression
Corrected code
var a = 2;
var b = 1;
if (a === 1 || b === 1 ) {
alert('yes');
} else {
alert('no');
}
You are assiging value rather then comparing
Try like this
if (a == 1 || b == 1)
To compare strictly use ===
Like this
if (a === 1 || b === 1)
JSFIDDLE
you can do this
var a = 2;
var b = 1;
if ((a == 1) || (b == 1 )) {
alert('yes');// should alert in this case
} else {
alert('no');
}
the == is one of the relational operator for checking equality and || is a logical operator that is a notion of logical OR
use this to compare just values
if (a == 1 || b == 1){
}
OR use this to compare values and type of variable
if (a === 1 || b === 1){
}
note : == will just check of values and === this will check value with type of variable
var a = 2;
var b = 1;
if (a == 1 || b == 1 ) {
alert('yes');// should alert in this case
} else {
alert('no');
}
I think you were doing assignment instead of comparison
Try using this:
if(a === 1 || b === 1){
alert('YES!')
}else{
alert('NO!')
}
OR you can use ternary operator condition instead of if else
(a == 1 || b == 1) ? alert('YES!') : alert('NO!')
I have a scenario like this
var a,b;
if(a > 0 || b > 0){
var truthValue = // The value which was executed as true in the if condition either a or b
}
Now how do i identify which value is executed as true. I can write a separate if condition for both but in the process of minimizing the LOC i am trying this way.
Any help is appreciated.
Like this?
var truth, a=11, b=0;
if((truth = a) > 0 || (truth = b) > 0){
document.write(truth + "<br>")
}
var a=0, b=22;
if((truth = a) > 0 || (truth = b) > 0){
document.write(truth + "<br>")
}
var a,b;
a =1;
b=2;
if(((a > 0)? (truthValue =a) : false) || ((b > 0)? (truthValue =b) : false)){
alert(truthValue);
}