Win a copy of Bad Programming Practices 101 (e-book) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Javascript interview riddle  RSS feed

 
Ranch Hand
Posts: 974
9
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Recently I came across this question on twitter:

"Javascript expression (a==1 && a==2 && a==3) could ever evaluate to true ?"

The answer was clever and elegant - and yes, definitely above my knowledge of JS 

I thought it would be nice to propose the same riddle here...
 
whippersnapper
Ranch Hand
Posts: 1843
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Precede your code with the following:



This is a reasonably well-known example of why you shouldn't redefine built-in functions in JavaScript. Don't do stuff like this.
 
Marshal
Posts: 59401
187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I searched for it and found two other solutions on SO. One is to define the three variables with different characters all looking like a but only one is '\u0061'.
The other is to append characters which look like spaces but aren't to the a.
There was a solution similar to CM's on SO, too.

Several people gave a better answer still, “Don't take a job there.”
 
Claude Moore
Ranch Hand
Posts: 974
9
IBM DB2 Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
Several people gave a better answer still, “Don't take a job there.”



Well, a riddle is a riddle after all...I suppose that  no one in real life would write such a tricky code !
 
Author and ninkuma
Marshal
Posts: 66727
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Several people gave a better answer still, “Don't take a job there.”



Yup, for me that would be "just walk away" level nonsense.

Just as bad as "so there's this pirate that wants the gold rope..."
 
Master Rancher
Posts: 2708
92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm... with a wife and a couple of kids to feed, plus a mortgage to pay, "crying with the wolves" comes to mind.
 
I've got no option but to sell you all for scientific experiments. Or a tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!