Boolean equality - javascript

I got this out of an exam question, and couldn't understand how the solution works. This function is supposed to return "true" if the values "x" and "y" are equal, and return False otherwise.
The solution:
function equal_boolean (x , y) {
return x ? y : y ? x : true;
}
Why does this work? As much as I can understand, it would turn out to be evaluating if X is true. If X is true, it would return Y. How is X supposed to be "true"?
If it isn't, it would evaluate whether Y is true, if it's true it would return X, and if it isn't - it would return True.
Is there something wrong with my understanding?

return x ? y : y ? x : true;
parses as
if x
return y // if y is true, then x == y. if y is false, then x != y
else (x is false)
if y
return x // x is false and y is true, y != x, return false
else
return true // x is false and y is false, return true
This is of course a pretty convoluted way to express boolean equality (aka Logical biconditional aka iff). More natural would be an expression like this:
(x && y) || (!x && !y)

The first thing you should do is group the ternary operators together:
(x ? y : (y ? x : true))
if x is true, then return y, whose value also tells you whether x and y are equal
if x is false, then start with the second ternary:
if y is true, then return x (false) as the two are not equal
if y is false, then x and y are equal, so return true

Let's try expanding this a bit:
var myBool = x ? y : (y ? x : true)
return myBool;
First block
x ? y : ...
if X is true, then return value of Y. If Y happens to be true, both are equal. Otherwise return false (no match).
Second block if X was false:
y ? x : true
if Y is true, return X. X was false, so false is returned (no match)
if Y is false, return true - both values are false.

First identify the expressions
return x ? y : y ? x : true;
//turns into
return x ? y : (y ? x : true);
Replace the ?: ternary operators by if statements
if (x) {
return y;
} else {
//here x is false, so we will be able to replace x by false in the next step
if (y) {
return x;
} else {
return true;
}
}
Make if statements more verbose, replace return x by return false
if (x == true) {
return y;
} else {
if (y == true) {
return false;
} else {
return true;
}
}
Finally replace return y; by if (y == true) { return true; } else { return false; }, and check all the possibilities
if (x == true) {
if (y == true) {
return true; // x == true and y == true
} else {
return false; // x == true and y == false
}
} else {
if (y == true) {
return false; // x == false and y == true
} else {
return true; // x == false and y == false
}
}
It works. (As long as x and y are booleans)

Related

ternary operation in js

Maybe I do not understand ternary operation but
if I am right it's
test ? true : false
So this should give
function toto(x, y)
{
return (x > 0 ? x < 7 ? true : false : false &&
y > 0 ? y < 6 ? true : false : false)
}
true only if 0
but if I do
toto(4,6)
it returns true, why? What am I missing ?
just do like this :
function toto(x, y)
{
return (x > 0 ? x < 7 ? true : false : false ) &&
( y > 0 ? y < 6 ? true : false : false)
}
with the bracket before and after the exp1 and exp2
and yes it's a bit unreadable ^^
edit : I also would do
return (x > 0 && x < 7) && (y > 0 && y < 6)
you need eslint to format your code ,this is the formatted code,see:
function toto(x, y) {
return x > 0
? x < 7
? true
: false
: false && y > 0
? y < 6
? true
: false
: false
}
image:
I think ,it is easier to understand
Aren't you trying to achieve this? chceking whether the x is from 0..7 and y is 0..6?
function toto(x, y)
{
return (x > 0 && x < 7) && (y > 0 && y < 6 );
}
Operator precedence affecting it here
function toto(x, y)
{
return ((x > 0 ? x < 7 ? true : false : false) && (y > 0 ? y < 6 ? true : false : false))
}

Can someone explain what this JS snippet is doing?

Can someone please tell me what this snippet is doing?
Is it setting x to true if y is undefined?
var x = false;
var y = x === true;
The code
var x = false;
var y = x === true;
console.log(y);
is simply checking the condition x === true, like other programming language it will result to either true or false. Since you have var x = false; the condition x === true will result in false as false === true is always false. Now, the result of this condition is being assigned to the new variable y as var y = x === true; so the value for y will be false.
Set x to false
Is x exactly-equal to true?
2.1. If so, then set y to true
2.2. Otherwise, set y to false
There is no invokation of of global.undefined in this fragment.
The first line should be pretty clear, it sets x to false. The second line is a bit more difficult, first, it compares x to true. If x is exactly equal to true, it will set y to true. Since x is false, the comparison will also return false, so y will be set to false.
TL;DR, this is a convoluted way of setting y to false.
=== is a strict comparison operator. The value of variable y will only true if x is boolean and true, y will be false otherwise. Check this snippet out for example:
let a = 1;
console.log(a == true); // print true
let b = 1;
console.log(b === true); // print false, because `b` is a number, not boolean
let c = true;
console.log(c == true); // print true
let d = true;
console.log(d === true); // print true, because `d` is a boolean and its value is `true`
This var y = x === true; statement means: keep the returned value of (x === true) in the y variable. In your case, y will hold false as its value since x is a boolean, but its value is false.
Strict Equality Operator, === has a greater precedence over Assignment Operator, =. Therefore the check for x is done before assigning its value to y.
You can break this down into three statements.
var x = false
var tmp = (x === true)
var y = tmp
Since x is false, y will also get set to false.

Why can't I assign and then check a variable in the same if statement in Javascript?

In other words, why doesn't this show an alert?
var x;
if (x = 1 && x > 0) {
alert(x);
}
As far as I understand, x = 1 should assign 1 to x and also return 1. The x > 0 check is failing. Why?
Actually, the && operation will have precedence over the assignment.
In you case, x will be the result of 1 && x > 0 which is false.
var x;
if (x = 1 && x > 0) {
alert(x);
}
console.log(x); // false
You can enforce the order of operations using parentheses, as shown by Nina Scholz.
You need some parens to separate the assignment from the ongoing expression.
var x;
if ((x = 1) && x > 0) {
alert(x);
}

how to get the same result by using function and closure together in javascript

I need to make the following(below) function call to give the same result in both situations:
sum(5,4); // 9
sum(5)(4); // this should also print 9
I tried the following but it's not working:
function sum(x,y){
var a = x;
var b = y;
if (y == undefined && y == ''){
return function (a,b){
return a +b;
}
}
else {
return a +b;
}
}
Any suggestions?
Try to curry your function for your requirement,
function sum(x,y){
if(y === undefined){
return function(y){ return x+y; }
} else {
return x + y;
}
}
sum(5,4); // 9
sum(5)(4); // 9
The "cool" one line answer:
function sum(x, y){
return y != undefined? x+y : function(a){return x + a};
}
You should use logical OR(||), not AND(&&)
function sum(x,y){
if (y == undefined || y == ''){
return function (y){
return x + y;
}
}
else {
return x + y;
}
}
Careful: You probably won't need that functionality, it's simply redundant.
You can use conditioning like that:
function sum( x , y ){
if(y == undefined){
return function( y ){
return x + y;
};
}
else{
return x + y;
}
}

Multiple comparison operators in a JavaScript boolean expression

I'm trying to check whether the variable y is less than x and greater than z, but this boolean expression is returning false for some reason. Does JavaScript allow boolean expressions to be written concisely like this? If so, what is the correct syntax?
x = 2;
y = 3;
z = 4;
if(x > y > z){
alert("x > y > z"); //Nothing happens!
}
Try using the logical and operator:
if (x > y && y > z) {
to guarantee both conditions are true.
DEMO: http://jsfiddle.net/3sxvy/
If you need to put this into a function, you could try:
function compareNumbers(direction) {
var inOrder = (function () {
if (direction === "desc") {
return function (current, before) {
return current <= before;
};
} else if (direction === "asc") {
return function (current, before) {
return current >= before;
};
}
})();
var valid = true;
for (var i = 2; i < arguments.length; i++) {
if (!inOrder(arguments[i], arguments[i-1])) {
valid = false;
break;
}
}
return valid;
}
if (compareNumbers("desc", 33, 5)) {
console.log("Good");
} else {
console.log("Bad");
}
DEMO: http://jsfiddle.net/kn6M4/1/
Change your test to
if (x > y && y > z){
When you write (x > y > z), this is equivalent to ((x>y)>z), so you're comparing a boolean (x>y) to z. In this test, true is converted to 1, which isn't greater than 2.
You want
if(x > y && y > z){
alert("x > y > z"); //Nothing happens!
}
Javascript will try to parse your original statement from left to right and you'll end up comparing z to a boolean value which will then be parsed to a number (0 or 1).
So your original statement is equivalent to
if( (x > y && 1 > z) || (x <= y && 0 > z))

Categories

Resources