Any Way to Chain == and || Operands

any way to chain == and || operands

Try this:

if [a, b, c].contains(foo) {
//do stuff
}

Chaining logical operators

Translating what you said to code:

// I want the if condition execute:
// if a and b are different but not when one of them equals EOF
if ( a != b && ! (a == EOF || b == EOF) )

Then applying DeMorgan's rule which moves the NOT inside and switches the OR to an AND:

   if ( a != b  &&  a != EOF && b != EOF )

short hand for chaining logical operators in javascript?


var a = [1, 16, -500, 42.42, 'something'];
var value = 42;
if (a.indexOf(value) > -1){
// blah blah blah
}

Upd:
Utility function sample as proposed in comments:

Object.prototype.in = function(){
for(var i = 0; i < arguments.length; i++){
if (this == arguments[i]) return true;
}
return false;
}

So you can write:

if (value.in(1, 16, -500, 42.42, 'something')){
// blah blah blah
}

Multiple using of || and && operands

The best way to solve this problem is using brackets.
You should always use them even if you know the binding prioritys, to increase readability of your code.

(x.IsPaid == true || x.IsPaid == null) && (x.TypeId == 1 || x.TypeId == 2) && x.CityId == 1 && x.CategoryId == 2



&& has a higher proirity than ||

So false && false || true would be translated to (false && false) || true => true




Sidenote as mentioned by @Joey:

Instead of (x.IsPaid == true || x.IsPaid == null) you can write (x.IsPaid != false).

Possible to chain comparison operators?

No, PHP doesn't have anything like this.

Chaining Bool values give opposite result to expected

== groups from left to right, so if all values are zero then:

0 == year // true
(0 == year) == month // false, since month is 0 and (0 == year) converts to 1
((0 == year) == month) == day // true

And so on.

In general, x == y == z is not equivalent to x == y && x == z as you seem to expect.

Chaining logical operators gives wrong result in javaScript

I believe one of your problems is that your function countThis(numberToCount) has no return statement, so is returning undefined.

Simply adding return count; should fix things.

function countThis(numberToCount) {
var count = 0;
for (var i in arr) {
if (arr[i] == numberToCount) {
count++;
}
}
return count;
}

It would also make your code clearer and less prone to errors if you did your if block the other way round, checking from 6-0, requiring one comparison per if statement. This was where you were going wrong before, and just a little bit of rearrangement and planning can help:

function scoreArr() {
var score = 0;
if (countThis(4)>1) {
score = 6;
} else if (countThis(4)==1) {
score = 5;
} else if (countThis(3)>=1) {
score = 4;
} else if (countThis(2)>1) {
score = 3;
} else if (countThis(2)==1) {
score = 2;
} else if (countThis(1)>=1) {
score = 1;
} else {
score = 0;
}
return score;
}

I made a jsFiddle so you can see the result: https://jsfiddle.net/Daniel300/evLqzuvs/5/

EDIT:

Having taken another look at your code, I realised there are a couple of other issues that you might want to look at:

If you want your functions to work correctly, you should declare all variables you are using inside the function, and just return what you need.

Instead of:

addOne(1);
function addOne(num) {
result=num+1;
}
alert(result);

Try:

function addOne(num) {
var result = num + 1; //Local variable, keeps it inside the function only.
return result; //Returns only what you need
}
alert(addOne(1)); //MUCH simpler :)

//alert(result); would give undefined

Chain equality operator in javascript

Reading left to right:

1==2 is false

false==false is true

Can I chain comparisons together in an IF statement?

Your if condition is wrong.

You should replace it with the code below:

if ((Board[1][1] == Board[1][2]) && 
(Board[1][2] == Board[1][3]) &&
(Board[1][3] == 'O'))

When you are checking for horizontal winning condition, all three blocks in the horizontal line must have a value of 0.



Related Topics



Leave a reply



Submit