Javascript syntax - javascript

How do you say if (A == 0) OR (B == 0)?

Just to be snarky:
if (A === 0 || B === 0)

if (A == 0 || B == 0)
or
if ((A == 0) || (B == 0))
Check out Control Structures and Operators on Wikibooks

if ( A == 0 || B == 0 ) {
}

depends if you mean exclusive or inclusive OR :)
Inclusive OR:
if(A == 0 || B == 0)
{
}
Exclusive OR:
if(A == 0 && B != 0 || A != 0 && B == 0)
{
}

Related

Abbreviate check function

I'm just starting to learn javascript and i'm trying to figure out how to make this code shorter.
Right now, the check function evaluates if a = "admin" and after that if a = "manager".
Is it possible to do this evaluation in one line?
Kind of "if (a = "admin" or "manager") ..."
const valid = "User name valid";
const invalid = "User name invalid";
function check(a, b) {
if (a === "admin") {
return valid;
} else if (a === "manager") {
return valid;
} else if (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
console.log(check("manager", "ikey"));
console.log(check("admin", "root"));
console.log(check("user", "ikey"));
console.log(check("user", "Mikey"));
Thanks!!
You can use the logical or operator to handle all valid cases together.
function check(a, b) {
if (a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10) {
return valid;
} else {
return invalid;
}
}
You can also simplify this to one statement using the ternary operator.
function check(a, b) {
return a === "admin" || a === "manager" || b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10 ? valid : invalid;
}
You should do it with or operator || like so:
if (a === "admin" || a === "manager" || (b[0].toUpperCase() == b[0] && b.length > 4 && b.length < 10))
return valid;
else
return invalid;

What means this kind of expression with logics operators

I have an boolean expression in javascript and i don't know what it means.
a = (b === LEFT && -2 || b === RIGHT && 2 || 0)
Please what does it mean ?
The && is a hacky shortcut if:
if (B === LEFT) {
a = -2;
} else if (B === RIGHT) {
a = 2;
} else {
a = 0;
}
one more shortcut with ternary operator
a = b === LEFT? -2: (b === RIGHT? 2 : 0)

Need solution for if statement logic

I need help on a condition logic using if statement as following:
Variables A and B contain 3 properties which is level1, level2 and level3.
level1 and level2 can be 0 or more and level3 is null or numeric.
Variables A and B can be null.
Currently I have this condition:
if (A.level1 == 0 and B.level1 == 0) {
code here
} else if (A.level2 == 0 and B.level2 == 0) {
code here
} else if (A.level3 != null and B.level3 != null) {
code here
}
The problem is that this code doesn't handle the Variables A and B can be null part. The code should handle that part like this:
When A is null, B will still go through the same condition but without A and vice versa.
However, if A and B is null then the condition will be false at once.
I have problem in how to implement the Variables A and B can be null part in my condition, any advice?
Add the A and B isNull check in your condition:
if (A == null && B == null) {
return;
} else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) {
// code here
} else if ((A == null || A.level2 == 0) && (B == null || B.level2 == 0)) {
// code here
} else if ((A == null || A.level3 != null) && (B == null || B.level3 != null)) {
// code here
}
Explanation,
Take A for example in this else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) statement:
By putting the A == null || in (A == null || A.level1 == 0) && (B == null || B.level1 == 0), if A is null, then this A.level1 == 0 check will be ignored, thus the check will be equivalent with else if (B == null || B.level1 == 0).
Since the first if already check A == null && B == null, the else if below won't have A and B both null. Therefore now else if ((A == null || A.level1 == 0) && (B == null || B.level1 == 0)) will be equivalent to else if (B.level1 == 0).
p.s. Here we are taking advantage of the || characteristic, that is if the first condition is fulfilled, the second condition will be ignored.
Seems like this is what you're looking for:
// Both A and B are null
if(A == null && B == null){
// do something
}
// Only A is null
else if(A == null){
if(B.level1 == 0){
// do something
}
else if(B.level2 == 0){
// do something
}
else if(B.level3 != null){
// do something
}
}
// Only B is null
else if(B == null){
if(A.level1 == 0){
// do something
}
else if(A.level2 == 0){
// do something
}
else if(A.level3 != null){
// do something
}
}
// Neither A or B are null
else{
if(A.level1 == 0 && B.level1 == 0){
// do something
}
else if(A.level2 == 0 && B.level1 == 0){
// do something
}
else if(A.level3 != null && B.level3 != null){
// do something
}
}
I suggest to check for falsy values of A or B first and then check the properties.
var A = null,
B = null;
if (!A && !B) {
console.log('A or B is null');
} else if (A.level1 === 0 && B.level1 === 0) {
console.log(1);
} else if (A.level2 === 0 && B.level2 === 0) {
console.log(2);
} else if (A.level3 != null && B.level3 != null) {
console.log(3);
}
Handle A and B separately,
if(!A){
if(A.level1 === 0 && A.level2 ===0){
}
else if(A.level1 === 0){
}
else if(A.level1 > 0){
}
if(A.level2 === 0){
}
else if(A.level2 > 0){
}
if(A.level3 === null){
}
else if (typeof (A.level3) === "number"){
}
}
same for B here...

Tic Tac Toe Winning Function

function checkWin(){
if (arro[0] === arro[1] === arro[2] === 1 || arro[3] === arro[4] === arro[5] === 1 || arro[6] === arro[7] === arro[8] === 1 || arro[0] === arro[4] === arro[8] === 1 || arro[2] === arro[4] === arro[6] === 1 || arro[0] === arro[3] === arro[6] === 1 || arro[1] === arro[4] === arro[7] === 1 || arro[2] === arro[5] === arro[8] === 1) {
console.log("O Won");
return "O";
}
else if (arrx[0] === arrx[1] === arrx[2] === 1 || arrx[3] === arrx[4] === arrx[5] === 1 || arrx[6] === arrx[7] === arrx[8] === 1 || arrx[0] === arrx[4] === arrx[8] === 1 || arrx[2] === arrx[4] === arrx[6] === 1 || arrx[0] === arrx[3] === arrx[6] === 1 || arrx[1] === arrx[4] === arrx[7] === 1 || arrx[2] === arrx[5] === arrx[8] === 1){
console.log("X Won");
return "X";
}
else
return "notwin"; }
Here, the arro is the matrix for O and the arrx is the array for X.
Running this in the console returns notwin everytime. Some help would be great. Thanks.
You can't combine condition checks like that. When you do a === b === c, what you're doing is comparing the result value of the a === b expression (which will be true [if they're the same] or false [if not]) with the value of c.
Instead, you need to combine them with &&, e.g. a === b && b === c.
E.g.:
function checkWin() {
if ((arro[0] === arro[1] && arro[1] === arro[2]) ||
(arro[3] === arro[4] && arro[4] === arro[5]) ||
/*...and so on...*/
) {
console.log("O Won");
return "O";
}
// ...
Side note: If you return from the block attached to the if, there's no need for the else prior to the next if. It's harmless, but pointless.

Multiple Sets of Sets of ORs

I have a feeling this isn't possible? I'm trying to check sets of conditions here and my tests should be passing after I moved a couple of those sets out of 2 other methods and into this if statement below as to combine all the checks into one if statement:
if ((move.coordinates.x === 0 && move.coordinates.y === 0 ||
move.coordinates.x === 0 && move.coordinates.y === 1 ||
move.coordinates.x === 0 && move.coordinates.y === 2)
||
(move.coordinates.x === 1 && move.coordinates.y === 0 ||
move.coordinates.x === 1 && move.coordinates.y === 1 ||
move.coordinates.x === 1 && move.coordinates.y === 2)
||
(move.coordinates.x === 2 && move.coordinates.y === 0 ||
move.coordinates.x === 2 && move.coordinates.y === 1 ||
move.coordinates.x === 2 && move.coordinates.y === 2))
{
...then do something
}
Doesn't seem the parens are making a difference here in separating out the conditional sets.
I'm trying to trim down on duplicate code I had those 3 sets in different methods where the logic was mostly the same, but checked for different states.
So I pasted in the two other state sets (sets meaning the group of 3 ors that make up a given state) and trying to check these 3 rows basically in one method instead of splitting out into 3 to cut down dup code.
Simplifying the notation, you have
(x == 0 && y == 0 || x == 0 && y == 1 || x == 0 && y == 2)
||
(x == 1 && y == 0 || x == 1 && y == 1 || x == 1 && y == 2)
||
(x == 2 && y == 0 || x == 2 && y == 1 || x == 2 && y == 2)
Since the operator || is associative, you can remove the parentheses.
And then you can use the distributive property to group the x.
x == 0 && (y == 0 || y == 1 || y == 2) ||
x == 1 && (y == 0 || y == 1 || y == 2) ||
x == 2 && (y == 0 || y == 1 || y == 2)
And even use the distributive property again
(x == 0 || x == 1 || x == 2) && (y == 0 || y == 1 || y == 2)
Finally, you can store the allowed values in an array and use indexOf to avoid repeating the variables:
var values = [0, 1, 2];
values.indexOf(x) >= 0 && values.indexOf(y) >= 0;
you can use logical rules that cover all the possibilities you allow. if you look at the repeating values in your code, you see that you are trying to exhaust all combinations of x values 0,1,2 with combinations of y values 0,1,2. so if you make a rule that will encompass x values from 0 to 2 and the same with y values from 0 to 2, you can simplify your if statement like the example below.
var coordx = move.coordinates.x;
var coordy = move.coordinates.y;
if ((coordx >= 0 && coordx <= 2) && (coordy >= 0 && coordy <=2)) {
// do something
}
One issue I see with your code is the order of operations (take a look here to see the basics), consider using more brackets to be as explicit as possible. It looks like you want to do this:
if (( (move.coordinates.x === 0 && move.coordinates.y === 0) ||
(move.coordinates.x === 0 && move.coordinates.y === 1) ||
(move.coordinates.x === 0 && move.coordinates.y === 2))
||
...
But, if you want to be a little more efficient and clear, I would recommend reformatting your logic with nested if statements:
if (x==0 || x==1 || x==2)
{
if(y==0 || y==1 || y==2)
{
do something...

Categories

Resources