I have this variable that toggles if some things are enabled to be edited or not looking like this
enabled: !(parseInt("#Model.Status") === #((int)Status.Active) ||
!(parseInt("#Model.Status") === #((int)Status.Expired)) && '#Model.EditMode' === 'True'),
For some reason if this works for the ones with active status and not expired and if i turn it around and put expired first it works for expired but not active ones.... So i am wrong here somewere the thought is that if the status is either Expired or Active the enabled should be false so that it cannot be edtied all other statuses should be fine.
I have also tried to write it like this with brackets around the second statement
!((parseInt("#Model.Status") === #((int)Status.Expired))
and like this inverting the if but none of it solved it
(parseInt("#Model.Status") !== #((int)Status.Expired)
Great comment by Pointy! but unfortunately it isn't the problem here either because even if I skip the whole && operator I still have the same problem with just
!(parseInt("#Model.Status") === #((int)Status.Active) || !(parseInt("#Model.Status") === #((int)Status.Expired)))
this still gives the same problem
Your condition seems to be wrong. !(parseInt("#Model.Status") === #((int)Status.Active) || !(parseInt("#Model.Status") === #((int)Status.Expired)) && '#Model.EditMode' === 'True')
can be viewed as !(a || !(b) && c) where
a = parseInt("#Model.Status") === #((int)Status.Active)
b = parseInt("#Model.Status") === #((int)Status.Expired)
c = '#Model.EditMode' === 'True'
The AND operator (&&) has higher precedence than ||, so the actual evaluation order can be shown using grouping as !(a || (!(b) && c)) the same way how 2 + 3 * 4 is evaluated as 2 + (3 * 4).
However, the logic presented does not match the requirement. The status should anything but Active or Expired and Edit Mode should be "True". However, it seems that the brackets are mismatched likely due to a typo, since the entire logical expression is negated, and further the second condition is also negated, whereas it shouldn't be.
The correct logical expression is !(a || b) && c. Substituting the logical tests:
!(parseInt("#Model.Status") === #((int)Status.Active) || parseInt("#Model.Status") === #((int)Status.Expired) &&
'#Model.EditMode' === 'True'
I am not familiar with Kendo, but if it's possible to use !== then the expression can be simplified a bit by removing some of the brackets. According to De Morgan's laws !(a || b) is equivalent to !a && !b, so if we perform that substitution, we get !a && !b && c. If we also change from !(parseInt("#Model.Status") === #((int)Status.Active)) to parseInt("#Model.Status") !== #((int)Status.Active) then we end up with:
parseInt("#Model.Status") !== #((int)Status.Active) &&
parseInt("#Model.Status") !== #((int)Status.Expired) &&
'#Model.EditMode' === 'True'
Related
Whats the difference between (a==1 || b==1) and ((a || b)==1)
this code block works right
if(userName==="" || userAge==="" ){
addErrorBool(true);
return;}
but this one not
if((userName || userAge)==="" ){
addErrorBool(true);
return;}
whats does the second one do?
a || b will evaluate to a, if a is truthy, otherwise it'll evaluate to b. So ((a || b)==1) will take whichever value that was and compare it against 1.
For example
(0 || 5) == 1
// equivalent to
(5) == 1
(1 || 2) == 1
// equivalent to
(1) == 1
For what you want, use .some instead, if you want to keep things DRY.
if ([userName, userAge].some(val => val === '')) {
addErrorBool(true);
return;
}
And then you can add as many items to the array .some is called on that you want.
(userName || userAge)==="" means:
(userName || userAge): if userName is truthy, use this value. Otherwise use userAge
==="": compare whichever object was chosen above, and compare that this is a string with no contents.
userName==="" || userAge==="" means:
userName==="": compare userName to see if it is a string with no contents
if it is, the result is true, otherwise:
userAge==="": compare userAge to see if it is a string with no contents
if it is, the result is true, otherwise the result is false
I am trying to get a comparison operator to work, without success. The operator compares two arrays to ensure they are identical.
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)) {
alert("all matches dropped");
}
The code works of course with 'true' in place of the comparison.
if (true) {
alert("all matches dropped");
}
The strange part is that the comparison returns 'true' when entered into the console:
console.log($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0)
----> true
Any ideas what may be wrong? Thanks.
It should be:
if($(array_1).not(array_2).length === 0 && $(array_2).not(array_1).length === 0)
Instead of:
if (($(array_1).not($(array_2)).length === 0 && $(array_2).not($(array_1)).length === 0))
Here $(array_1).not(array_2).length and ($(array_1).not($(array_2)).length both are not the same thing.
I'm wondering how to combine and (&&) with or (||) in JavaScript.
I want to check if either both a and b equal 1 or if both c and d equal 1.
I've tried this:
if (a == 1 && b == 1 || c == 1 && d == 1)
{
//Do something
}
But it doesn't work.
How can I write this condition correctly?
&& precedes ||. == precedes both of them.
From your minimal example I don't see, why it doesn't achieve your desired effect. What kind of value types do a–d have? JavaScript might have some non-obvious type coercion going on. Maybe try comparing with === or convert to numbers explicitly.
Side note: many lint tools for C-like languages recommend to throw in parentheses for readability when mixing logical operators.
Operator Precedence can be overridden by placing the expression between parenthesis.
if ((+a == 1 && +b == 1) || (+c == 1 && +d == 1)) // Use brackets to group them
{
// your code
}
This will prevent you from such cases like if(0&&0 || 1&&1) .
Well now that I've finished telling everybody else (except David) why their answers are wrong, let me give them the same chance to hassle me.
Your existing code as shown should already do what you seem to be describing. But is it possible that when you say:
"I want to check if either both a and b equals 1 or if both c and d equals 1."
...your use of the word "either" mean that you want to check if one and only one of the following conditions is true:
both a and b are 1, but c and d are not both 1
both c and d are 1, but a and b are not both 1
That is, you want one pair of variables to be 1, but you don't want all four variables to be 1 at the same time?
If so, that is an exclusive OR operation, which in JS is the ^ operator:
if ((a == 1 && b == 1) ^ (c == 1 && d == 1)) {
Note that unlike with a logical OR || operator, you have to add parentheses around the AND && parts of the expression because ^ has higher precendence. (^ is actually a bitwise operator, but it will work for you here since the operands you'd be using with it are all booleans.)
place some extra brackets to differentiate the and n or conditions
if ((a == 1 && b == 1) || (c == 1 && d == 1))
It is slightly hard to explain but I want to do something that looks like this:
if(a === 4 && b === true && c === "words" || "numbersandwords")DoSomething();
but it ends running without it matching the first operators. I want to know how to have the last operator except 2 different inputs while still making sure the other criteria are met before running.
You just need to use parentheses, e.g.:
if(a == 4 && b == true && (c == "words" || c == "numbersandwords")) { DoSomething(); }
Just use a few brackets to separate your or parts and the and parts, and add the c === before the last string. Without that equality part at the end, the 'numbersandwords' string always equates to true.
if(a === 4 && b === true && (c === "words" || c === "numbersandwords")){
DoSomething();
}
In JavaScript, like other languages, every operator (like && and ||) has a precendence that determines the order in which it's evaluated. && has higher precedence than ||, so it's evaluated first. Therefore, every term on the left is anded together. Even if they are all false, however, the overall result is true because it's ored with "numbersandwords", which evaluates to true (as does everything except 0, -0, null, false, NaN, undefined, or the empty string). The first thing you need to do is actually compare something (presumably c) to it. Then you can change the order of evaluation using parentheses, which has higher precedence than anything else:
if(a === 4 && b === true && (c === "words" || c === "numbersandwords")) DoSomething();
Alternatively, you can break the test up into several if statements if you may want to eventually do something slightly different based on the value of c (or it just better expresses your intent):
if(a === 4 && b === true)
{
if(c === "words" || c === "numbersandwords")
{
DoSomething();
}
}
More specifically, is there a set of values ( a, b and c) for which the operator precedence matters in the statement:
var value = (a && b == c);
(with the exception of NaN).
Yes
js> false && true == false
false
js> (false && true) == false
true
Since == has higher precedence than &&, the first is parsed as false && (true == false), which is equivalent to false && false, and thus evaluates to false. The second is equivalent to false == false, which is true
The language is parsed such that your statement is the equivalent of (a && (b == c)). The equality operator will always run before &&, || and other logical operators. You can find the nitty-gritty details here.
Yup. == binds more tightly than &&, so what you have binds as
var val = a && ( b == c)
See here. So a==0, b==1 and c==0 is false, while (a&&b)==c is true.
(Fixed typo. Dammit.)