Reputation: 4085
var PgrtiJr = {
"TWfbR": +((!+[] + !![] + !![] + !![] + []) + (!+[] + !![] + !![]))
};
PgrtiJr.TWfbR -= +((+!![] + []) + (+!![]));
console.log(parseInt(PgrtiJr.TWfbR, 10));
I have above mentioned js code. I executed this code on http://math.chapman.edu/~jipsen/js/. Can anybody explain me how it is equal to 32
?
and can you recommend any python library that can evaluate this expression in python
tried execjs but no luck
Upvotes: 0
Views: 51
Reputation: 304
[] is an object that is equal to null.
!![] is a "boolean" that equals to true. (twice ! of null ( = false) )
!+[] is a "boolean" that equals to true.
and if we add a [] after this expressions, they will be converted to string.
so
(!+[]+!![]+!![]+!![]+[])
will be a string that equals 4
(!+[]+!![]+!![])
will be a string that equals 3
hence (!+[]+!![]+!![]+!![]+[]) + (!+[]+!![]+!![])
will be a string that equals 43
and +(!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![])
will be a number that again equals 43
in the same way +((+!![]+[])+(+!![]))
will equal to 11
so total of expression will equal to 43 - 11 = 32
Upvotes: 1
Reputation: 239473
You need to understand few important things about JavaScript's loose typing. Lets start with simpler things, specific to your question.
An empty array literal is considered truthy, but when you apply unary + operator, the array will be converted to a String and which will then be converted to a number. So, internally +[]
evaluates to 0.
console.log(+[]);
// 0
Since []
is truthy, double negating it with logical not operator will give you true
, which when used in an arithmetic expression, evaluate to 1, since 1 is loosely equal to true
.
console.log(!![], 3 + !![]);
// true 4
Apart from all these, when we use an array literal with +
operator, the array will be converted to a string and since one part of the expression is string, the other part of the expression will also be converted to string and string concatenation takes place. So the resulting object will be of type string.
console.log(typeof 1, typeof [], typeof (1 + []), 1 + []);
// number object string 1
With this basic understanding, lets analyze the first expression.
+((!+[]+!![]+!![]+!![]+[])+(!+[]+!![]+!![]))
Lets first take,
(!+[]+!![]+!![]+!![]+[])
Here, +[]
evaluates to 0
and logical not makes it as true
. Since we use it in an expression with a numeric operand, true
is treated as 1. And as per our point 2 seen above, !![]
evaluates to 1. So, the expression becomes 1 + 1 + 1 + 1 + []
, which is actually 4 + []
. And as per point 3, the number 4 will become string 4.
The same way, other part of the expression, (!+[]+!![]+!![])
becomes, 1 + 1 + 1
and which is actually 3. So when you do '4' + 3
, you will get '43'
, which is a string. Now, we have a unary + operator which converts this string to a number. So, the result of evaluation of this expression becomes 43, which is a number.
The other part of the expression,
PgrtiJr.TWfbR -= +((+!![]+[])+(+!![]))
will be evaluated like this
+((1 + []) + 1)
and then
+('1' + 1)
and then
+'11'
which is then evaluated to 11. Since PgrtiJr.TWfbR
is actually 43, 43 - 11 becomes 32. That is why you are getting 32 as the answer.
Upvotes: 1