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...
Related
I am trying to make tic tac toe on a website using javascript. That worked and now I'm trying to make a bot that, then you have 2 area's in a row. It needs to block you. But everytime it does block me, it won't do anything else afterwards, because it keeps looping into the error.
The t-numbers are the tile names, 0 means unoccupied,1 means for player 1, and 2 for player 2.
The t-numbers with a 1 behind it are just the tiles but they mustn't interfere with the functions (they are t-numbers too).
How do I fix this?
function tbot(){
while (turn == "player 2") {
if (t41 == 0 &&
(t01 == 1 && t81 == 1) ||
(t11 == 1 && t71 == 1) ||
(t21 == 1 && t61 == 1) ||
(t31 == 1 && t51 == 1)
) {t4()}
else {
var tiles = ['0', '1', '2', '3', '4', '5', '6', '7', '8'];
var tile = tiles[Math.floor(Math.random()*tiles.length)];
if (tile == '0' && t01 == 0) {t0()}
else if (tile == '1' && t11 == 0) {t1()}
else if (tile == '2' && t21 == 0) {t2()}
else if (tile == '3' && t31 == 0) {t3()}
else if (tile == '4' && t41 == 0) {t4()}
else if (tile == '5' && t51 == 0) {t5()}
else if (tile == '6' && t61 == 0) {t6()}
else if (tile == '7' && t71 == 0) {t7()}
else if (tile == '8' && t81 == 0) {t8()}
}
}
}
function update() {
if (t01 == 1 &&
((t11 == 1 && t21 == 1)||(t31 == 1 && t61 == 1))
||t41 == 1 &&
((t31 == 1 && t51 == 1)||(t11 == 1 && t71 == 1)||
(t01 == 1 && t81 == 1)||(t21 == 1 && t61 == 1))
||t81 == 1 &&
((t61 == 1 && t71 == 1)||(t21 == 1 && t51 == 1))
) {
twin = 1;
document.getElementById('turnshow').innerHTML = "Player 1 is the winner!";
document.getElementById('treset').style.backgroundColor = '#888';
} else if (t01 == 2 &&
((t11 == 2 && t21 == 2)||(t31 == 2 && t61 == 2))
||t41 == 2 &&
((t31 == 2 && t51 == 2)||(t11 == 2 && t71 == 2)||
(t01 == 2 && t81 == 2)||(t21 == 2 && t61 == 2))
||t81 == 2 &&
((t61 == 2 && t71 == 2)||(t21 == 2 && t51 == 2))
) {
twin = 1;
document.getElementById('turnshow').innerHTML = "Player 2 is the winner!";
document.getElementById('treset').style.backgroundColor = '#888';
} else {
if (bot == 1) {tbot();}
document.getElementById('turnshow').innerHTML = "It is " + turn + "'s turn!";
}
}
function t4() {
if (t41 == 0 && twin == 0) {
if (turn == "player 1") {
document.getElementById('t4').style.backgroundColor = p1;
turn = "player 2";
t41 = 1;
} else {
document.getElementById('t4').style.backgroundColor = p2;
turn = "player 1";
t41 = 2;
}
}
update();
}
The program never exit the while loop because condition (turn == "player 2") is always true.
If you're expecting t4() to change turn then I guess it doesn't do the job right. Check for (t41 == 0 && twin == 0), turn will never change if this condition is never been true.
I found what i did wrong. When I was checking if there are two area's selected in a row my code was:
if (t41 == 0 &&
(t01 == 1 && t81 == 1) ||
(t11 == 1 && t71 == 1) ||
(t21 == 1 && t61 == 1) ||
(t31 == 1 && t51 == 1)
)
But what that did: t41 and t01 and t81 or t11 and t71 or etc.
I just needed to add "()" around the options :/
if (t41 == 0 &&
((t01 == 1 && t81 == 1) ||
(t11 == 1 && t71 == 1) ||
(t21 == 1 && t61 == 1) ||
(t31 == 1 && t51 == 1))
)
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.
the result I want is second if else statement if code not in the list then alert, I don't get why the first if else statement fail, I thought that just reverse second if else statement ?? do I misunderstand some thing??
https://jsfiddle.net/e6qohvhc/
var code = '500';
if (code != '400' || code != '401' || code != '500') {
console.log('true'); // I don't want it alert here
}
if (code == '400' || code == '401' || code == '500') {
// I have to always leave this empty line ...
} else {
console.log('second true');
}
This has to do with De Morgan's laws:
If you want to invert a statement you have to invert every operator.
!a becomes a, b becomes !b, ||becomes &&, && becomes ||.
So the inversion of your second if would be something like
(code != '400' && code != '401' && code != '500')
You may need to review the Morgan's laws.
Basically, if you want to negate (a || b || c) you need to use (!a && !b && !c)
Hope it helps,
if(code != '400' || code != '401' || code != '500'){}
always will be true because a variable cant be equal to multiple values
The problem is ||
First if statement for 500 is always true, that's why you are having problem/
Do it in this way and it should work the way you wanted it (check it out in your fiddle);
var code = 500;
alert(code);
console.log(code);
if (((code !== 400) || (code !== 401)) && (code !== 500)) {
console.log('true');
alert("123");
}
else if ((code == 400) || (code == 401) || (code == 500)) {
alert("456");
} else {
console.log("second true");
alert("else");
}
I have 20 divs, each one with a speficif class, so I select it and check if is 1 of the 4 'special ones'.
The main issue is that the following code is supposed to work...
$('.cbp-ig-grid li, .cbp-ig-grid li a span object').on('click', function () {
/* Variables Definition */
var item = $(this).find('span').attr('class').split(' ')[1]
}
if((item != 'item1') || (item != 'item2') || (item != 'item3') || (item != 'item4')){
// Always enters here!
}else{
// Never enters here :( (I need to enter here for the 4 cases in the if statement)
}
but when I do for just one ... it works!
if(item != 'item1'){
// do stuff
}else{
// do other stuff
}
I don't know what I'm doing wrong, please any help will be useful
Consider your if statement:
if((item != 'item1') || (item != 'item2') || (item != 'item3') || (item != 'item4')){
}
What that is saying is that if ANY of these conditions are true, the if condition is met and it will execute the if block.
Let's say the item is "item2" now the first expression of your if statement is met as it's not item1 so that part is true. thus it executes the block.
What you want is: &&
if((item != 'item1') && (item != 'item2') && (item != 'item3') && (item != 'item4')){
//when it's not the special case.
}
else
{
//the 4 special cases.
}
if((item != 'item1') || (item != 'item2') || (item != 'item3') || (item != 'item4')){
No chance to go into the else here... item is always different from one or the other.
.hasClass() is your best friend. https://api.jquery.com/hasclass/
$('.cbp-ig-grid li, .cbp-ig-grid li a span object').on('click', function () {
/* Variables Definition */
var item = $(this).find('span');
switch(true) {
case item.hasClass('item1'):
// item 1
break;
case item.hasClass('item2'):
// item 2
break;
case item.hasClass('item3'):
// item 3
break;
case item.hasClass('item4'):
// item 4
break;
default:
// other stuff
}
});
Let's make it simple
if((item != 'item1') || (item != 'item2') || (item != 'item3') || (item != 'item4'))
Let's test it:
1:
item = 'item1':
false || true || true || true
that equals to true; because false || true = true
2:
item = 'theGreatOldOnes'
true || true || true || true - that equal to true
Both are true! That means that your expression is flawed - it doesn't make difference between 'special class' and any 'nonspecial class'
To make it understand difference between 'special' and 'not special' you need to use:
if((item != 'item1') && (item != 'item2') && (item != 'item3') && (item != 'item4'))
Or
if((item === 'item1') || (item === 'item2') || (item === 'item3') || (item === 'item4'))
You can do testing with 'item1' and 'theGreatOldOnes' to get a better grip on those things ^ ^
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)
{
}