Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does this expression return true? [duplicate]

Possible Duplicate:
Can somebody explain this Javascript method ?

(x = [].reverse)() === window // true

Any idea why?

like image 573
Eugene Yarmash Avatar asked Feb 20 '10 11:02

Eugene Yarmash


People also ask

How do you find duplicate parentheses in an expression?

A set of parenthesis are duplicate if the same subexpression is surrounded by multiple parenthesis. Examples: Below expressions have duplicate parenthesis - ((a+b)+((c+d))) The subexpression "c+d" is surrounded by two pairs of brackets. (((a+(b)))+(c+d)) The subexpression "a+(b)" is surrounded by two pairs of brackets.

What is duplicate number?

Find the Duplicate Number. Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. There is only one repeated number in nums , return this repeated number. You must solve the problem without modifying the array nums and uses only constant extra space.

Which data structure is best to find if an expression has an duplicate parenthesis or not?

The idea is to use stack. Iterate through the given expression and for each character in the expression, if the character is a open parenthesis '(' or any of the operators or operands, push it to the top of the stack.


1 Answers

(x=[].reverse)() === window // true

Didn't understand this one at first, but I guess it is easy: first x becomes the reverse function of arrays, then it is called with this === window. So it amounts to window.reverse(). Just looked it up, and reverse() works in place, so window.reverse() === window - although it is potentially different from before.

I got this answer from this link

http://news.ycombinator.com/item?id=1122004

like image 163
ratty Avatar answered Nov 13 '22 07:11

ratty