Yesterday, there is a very very popular discussion on a simple Javascript piece of code, the question is, should the following expression can ever be evaluated true
(a== 1 && a == 1 && a ==1)
It looks strange indeed, and in Javascript somehow, it’s very possible. Here some answers from StackOverflow.
Answer by Kevin B
const a = {
i: 1,
toString: function () {
return a.i++;
}
}
if(a == 1 && a == 2 && a == 3) {
console.log('Hello World!');
}
Answer by Jeff
var aᅠ = 1;
var a = 2;
var ᅠa = 3;
if(aᅠ==1 && a== 2 &&ᅠa==3) {
console.log("Why hello there!")
}
Quote: “Note the weird spacing in the if statement (that I copied from your question). It is the half-width Hangul (that’s Korean for those not familiar) which is an Unicode space character that is not interpreted by ECMA script as a space character – this means that it is a valid character for an identifier. Therefore there are three completely different variables, one with the Hangul after the a, one with it before and the last one with just a.”
Answer by Jonas W.
var i = 0;
with({
get a() {
return ++i;
}
}) {
if (a == 1 && a == 2 && a == 3)
console.log("wohoo");
}
There are even more answers on this problem, you can visit StackOverflow for more in-depth discussion.
Finally, is it possible in any other language?
Well, in C/C++, it is possible via macro/pre-processor.
#define a (b++)
int b = 1;
if (a ==1 && a== 2 && a==3) {
std::cout << "Yes, it's possible!" << std::endl;
} else {
std::cout << "it's impossible!" << std::endl;
}
Credits to Gustavo Rodriguez
Similar to C++, in C#, you can provide operator overloading.
class Program
{
static void Main(string[] args)
{
var a = new A();
if (a == 1 && a == 2 && a == 3)
{
Console.WriteLine("Bravo");
} else
{
Console.WriteLine("Dude, it is impossible");
}
}
}
class A
{
public static bool operator ==(A a, int b) => true;
public static bool operator !=(A a, int b) => false;
}