Can (A== 1 && a ==2 && A==3) Ever Evaluate to True

Can (a== 1 && a ==2 && a==3) ever evaluate to true?

If you take advantage of how == works, you could simply create an object with a custom toString (or valueOf) function that changes what it returns each time it is used such that it satisfies all three conditions.

const a = {  i: 1,  toString: function () {    return a.i++;  }}
if(a == 1 && a == 2 && a == 3) { console.log('Hello World!');}

Is it possible a === 1 && a === 2 & a === 3 return true in Javascript?

Some of the methods on the other page work with strict equality, just avoid the ones that utilize toString or valueOf.

let num = 1;
Object.defineProperty(window, 'a', { get() { return num++ } });
// or use `with` and an object with a getter instead of putting it on the window

if(a === 1 && a === 2 && a === 3) {
console.log('Never catch!');
}

Can (a==1 && a==2 && a==3) ever evaluate to true in C or C++?

Depends on your definition of "a is an integer":

int a__(){ static int r; return ++r; }
#define a a__() //a is now an expression of type `int`
int main()
{
return a==1 && a==2 && a==3; //returns 1
}

Of course:

int f(int b) { return b==1&&b==2&&b==3; }

will always return 0; and optimizers will generally replace the check with exactly that.

Can ($a == 1 && $a == 2 && $a == 3) ever evaluate to true?

I just tried this:

$a = true;
echo ($a == 1 && $a == 2 && $a == 3);

and it echoed 1.

Because of the type casting and not type checking, 1, 2, 3 will be treated as true when compared to a boolean value.

Answer to the edit: No it can't be done.

Hackish method which @FrankerZ commented about:

Zero byte character = 0xFEFF

http://shapecatcher.com/unicode/info/65279

http://www.unicodemap.org/details/0xFEFF/index.html

$var  = "1";
$var = "2";
$ var = "3";

echo ($var  === "1" && $var === "2" && $ var === "3") ? "true" : "false";

This code runs with this character because the name $ var and $var  seems to be valid for the PHP compiler and with the appropiate font, it can be hidden. It can be achieved with Alt + 65279 on Windows.

Can (a==1 && a==2 && a==3) evaluate to true in C# without multi-threading?

Sure, its the same concept as a few of the javascript answers. You have a side effect in a property getter.

private static int _a;
public static int a { get { return ++_a; } set { _a = value; } }
static void Main(string[] args)
{
a = 0;
if (a == 1 && a == 2 && a == 3)
{
Console.WriteLine("Hurraa");
}
Console.ReadLine();
}

(a== 1 && a ==2 && a==3) ever evaluate to true in pl sql?

Just for fun of course

SQL> create or replace
2 package pkg is
3 x int := 0;
4 end;
5 /

Package created.

SQL>
SQL> create or replace
2 function a return number is
3 begin
4 pkg.x := pkg.x + 1;
5 return pkg.x;
6 end;
7 /

Function created.

SQL>
SQL>
SQL> set serverout on
SQL> begin
2 if a = 1 and a = 2 and a = 3 then
3 dbms_output.put_line('BINGO!');
4 end if;
5 end;
6 /
BINGO!

PL/SQL procedure successfully completed.

JS syntax for if a or b or c but not all of them

Convert them to booleans, then add them up, and check that the result is 1. (true will be coerced to 1; false will be coerced to 0.)

if (Number(Boolean(a)) + Number(Boolean(b)) + Number(Boolean(c)) === 1) {
// ...
}

or

const sum = [a, b, c].reduce((sum, item) => sum + Number(Boolean(item)), 0);
if (sum === 1) {
// ...
}

Why does ```typeof(a) != Number``` //evaluate True - even when typeof(a)==Number?

If you want to individually check for a<0 and b<0, you cannot do (a||b)<0. Reason: a||b gets evaluated first and you get the first truthy value out of a and b. So, it becomes a matter of order. If you pass 1,-2 you will get 1 as your result of a||b and obviously 1<0 return false.

function func(a,b){
console.log(a||b);
console.log((a||b)<0);
}

func(1,-2);
func(-1,2);
func(1,2);


Related Topics



Leave a reply



Submit