Javascript multiple statements in a single if not working - javascript

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 ^ ^

Related

Print null siblings of unbalanced binary tree when traverse in level order

Maybe a duplicate or odd question but I haven't been able to find the answer anywhere:
I want to print out the path in Breadth-First Search order of an unbalanced binary tree with null siblings. My code works and I've tried to improve it but I'm a bit stuck. I just wonder if there's a more clever way of doing it instead of multiple checks.
function traverse(tree) {
const path = [];
let queue = [tree];
while (queue.length > 0) {
const current = queue.shift();
if (current !== null) path.push(current.val);
else {
path.push(null);
continue;
}
if (current.left === null && current.right === null) continue;
else if (current.left !== null && current.right === null) {
queue.push(current.left);
queue.push(null);
}
else if (current.left === null && current.right !== null) {
queue.push(null);
queue.push(current.right);
}
else {
queue.push(current.left);
queue.push(current.right);
}
}
return path;
}
Just don't do the checks? The following code should be equivalent to what you are doing:
if (current.left === null && current.right === null)
continue;
else {
queue.push(current.left);
queue.push(current.right);
}
It pushes the null value when the child is null and pushes the property value otherwise.

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...

javascript multidimensional array is valid

I have an array that looks kind of like this memory[indexGroup][indexItem]. How can I check if that is valid, in other words if it would work when using console.log and getting a value back, not null, undefined or other non values. Empty, 0 and false are valid. These don't give errors.
This is what I ended up with (seems to work) but it's a mess:
function hasMemory() {
if( typeof memory === 'undefined') return;
if( typeof memory[indexGroup] === 'undefined') return;
if( memory[indexGroup] === null ) return;
if( typeof memory[indexGroup][indexItem] === 'undefined') return;
if( memory[indexGroup][indexItem] === null) return;
if( memory[indexGroup][indexItem] !== true ) return;
return true;
}
Scenarios
memory is not set
memory[indexGroup] is not set
memory[indexGroup][indexItem] is not set
Then it should just return but if the full multidimensional array is valid, it should return true.
Is there a shorter/better/safer ways to check this?
function hasMemory() {
return memory && memory[indexGroup] && memory[indexGroup][indexItem]
}
To handle the case where memory[indexGroup][indexItem] is 0 or false, based on
undefined == null
null == undefined
you can add to condition memory[indexGroup][indexItem]!=null
You can check for both null and undefined simultaneously by performing a loose check against null but will not work for 0 or false.
var u = undefined;
var n = null;
var z = 0;
var f = false;
console.log(u == null);
console.log(n == null);
console.log(z == null);
console.log(f == null);
Using that, here's a shorter way of approaching this:
function hasMemory() {
return memory &&
memory[indexGroup] != null &&
memory[indexGroup][indexItem] != null;
}

if else statement reverse

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");
}

Validate CreditCard Number and checked radio button javascript

I have radio buttons radioVisa, and radioMaster. If either one is checked, I need to first check to see which one is selected and then validate that the card number entered is valid. I also need to make sure that only numbers are entered.... I am not allowed to use any regular expression techniques.... If the radioVisa is checked, it seems to work but when I added the code for the radioMaster, if it is checked it does't work.... Can someone tell me what I am doing wrong please....
function isValidCardNumber(num, isVisa, isMaster){
var card = new Array();
if (document.getElementById('radioVisa').checked){
card = isVisa;
}
if (num[0] != '4' || num.length != 16 ){
return false;
} else {
return true;
} else if (document.getElementById('radioMaster').checked){
card = isMaster;
}
if (num[0] != '51' || num[0] != '52' || num[0] != '53' ||
num[0] != '54' || num[0] != '55' || num.length != 16 ){
return false;
} else {
return true;
}
if (num[0] != '51' || num[0] != '52' || num[0] != '53' ||
num[0] != '54' || num[0] != '55' || num.length != 16 )
You can not combine all those numbers.You need to specify individually.
or
var numbers= ["51", "52", "53", "54",55];
var index = numbers.indexOf(num[0]);
It will return -1 if that is not exist otherwise return the index

Categories

Resources