Why doesn't AngularJS allow for xor operations? [duplicate] - javascript

This question already has an answer here:
How to use xor functionality in angular js expression? [closed]
(1 answer)
Closed 6 years ago.
<form name="businessFormAbout" ng-show="(businessFormAbout.$submitted)^(businessForm.$submitted)">
This line shows error in console:
[$parse:lexerr] Lexer Error: Unexpected next character at columns 30-30 [^] in expression [(businessFormAbout.$submitted)^(businessForm.$submitted)]

It's not AngularJS that doesn't support XOR: JavaScript itself doesn't support XOR as you might define it.
JS does have a bitwise XOR operator (^), but this only works for numbers.
You could create your own XOR function a bit like this:
function XOR(a,b) {
return ( a || b ) && !( a && b );
}

Related

Why does a number have to be encapsulated in an expression containing that number before methods can be applied to it? [duplicate]

This question already has answers here:
Why can't I access a property of an integer with a single dot?
(5 answers)
Closed 7 months ago.
1.toString() gives us Uncaught SyntaxError: Invalid or unexpected token.
One the other hand, (1).toString() returns '1'.
Considering that typeof 1 === typeof (1) (both are of type number), why is this the case?
In JS number literals might be both integers and floats, so probably after finding . after the number translator expects to parse a float, like 1.123.

How to turn strings into boolean expressions? [duplicate]

This question already has answers here:
JavaScript -- write a function that can solve a math expression (without eval)
(2 answers)
Closed 3 years ago.
I'm working on a problem generator that spits out phrases like "!(A && B) || (A || B)", but it does so in a string (I have a function that spits this out in string form). How would I convert this string expression into a same boolean expression in javascript? I've tried to use JSON.parse() but it keeps giving me an error.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval should be the function you're looking for, however read the note about the huge security risk!

How to use operator "^" in JavaScript? [duplicate]

This question already has an answer here:
How to have a number to be a power?
(1 answer)
Closed 3 years ago.
I'm trying to use ^ operator in javascript, like:
0.66666666666666666666666666666667^1.5
should equal
0,54433105395181735515495201660131
But when I'm trying to do it, it won't return me anything, it will return either 67,0 or 1, depending on my attempts.
Desired Output
console.log(100 * 0.66666666666666666666666666666667^1.5);
How do I solve this problem?
You need to take the exponenciation operator **instead of bitwise XOR ^.
Alternate use the classing Math.pow.
console.log(0.66666666666666666666666666666667 ** 1.5)
^ means XOR in JavaScript. If you want exponentiation, you have to use Math.pow or ** instead.

What does the caret symbol (^) do in JavaScript? [duplicate]

This question already has answers here:
What does the ^ (caret) symbol do in JavaScript?
(5 answers)
Closed 6 years ago.
I thought that Math.pow(2,2) was equal to 2^2 but it is not. So what does ^ (caret) mean in JavaScript?
I was executing some tests in the console but didn't recognize the results:
2 ^ 2 = 0
2 ^ 3 = 1
1 ^ 2 = 3
It means bitwise XOR.
EDIT: Fixed link
It's a bitwise integer XOR operation (MDC link).
The ^ operator is bitwise XOR, you have more information in MDN:
https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators
That operator performs the logical XOR operation. (out bit is 1 when both input bits are different).
This is the bitwise XOR operator, which returns a one for each position where one (not both) of the corresponding bits of its operands is a one. The next example returns 4 (0100):
Code:
result = a ^ b;

What does the | operator do? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what's the difference between ( | ) and ( || ) in javascript?
I've seen this in a couple examples here but I never fully understood what it's supposed to do. Can anyone give me a simple example please?
In Javascript, the | operator is a bitwise operators (in contrast to the || operator which is a logical operator).
It convert each operand to a 32-bit number, and performs a bitwise or between them.
Example of expressions and their results:
1 | 1 === 1
1 | 2 === 3
1.99 | 2.99 === 3
Reference: http://developer.mozilla.org/en/JavaScript/Reference/operators/bitwise_operators
Depends where you use it:
In regular expression it represents "or": /[a-z]|[0-9]/i
It is also bitwise or operator, as described in: What does the "|" (single pipe) do in JavaScript?

Categories