0

I have this simple code in Javascript (jQuery) and I need to acces inside a IF statement in case one condition became TRUE, but I always obtain TRUE, even the condition been FALSE, and I don't understand why. Here is the simple code:

for(var i = 0; i < vector.length; i++) {
    genere = vector[i].gender;
    pitch = synthProcess[i].pitch;
    mood = synthProcess[i].mood;
    speed = synthProcess[i].speed;
    identifier = synthProcess[i].ident;

    if (genere !== "auto" || pitch !== "0" || mood !== "0" || speed !== "0" || identifier !== "0"){
        console.log("EXECUTING COMMAND");
    }  
}

What I want is ONLY when genere is different from 'auto', pitch different from '0' etc, then, enter inside the IF but always is entering. I tried with "0" (string) and 0 (int) options, but still getting the same result, in every iteration I get the Log inside the IF

Jai
  • 74,255
  • 12
  • 74
  • 103
Sergi
  • 417
  • 6
  • 18

2 Answers2

1

You are doing a strict comparison, which checks the type of your variables as well. Like this:

var a = 'auto';
var b = 0;
var c = 0;

if (a !== 'auto' || b !== '0' || c !== '0') {
    // this will be called since (0 !== '0') = true
    console.log('called!');
}

if (a !== 'auto' || b != '0' || c != '0') {
    // this wont be called since (0 != '0') = false
    console.log('not called');
}

That is, if you compare a zero int with a 0 String using strict comparison, they will be considered different:

console.log(0 === '0') // prints false
console.log(0 !== '0') // prints true

If you compare then using a normal comparison, they will be considered the same:

console.log(0 == '0') // prints true
console.log(0 != '0') // prints false

Check out this explanation and my fiddle.

Community
  • 1
  • 1
Bruno Krebs
  • 3,059
  • 1
  • 24
  • 36
1

if values of pitch etc are int use-

var  genere = 'auto';
var pitch = 0;
var   mood = 0;
var speed = 0;
var  identifier = 0;
if (genere !== "auto" || pitch !== 0 || mood !== 0 || speed !== 0 || identifier !== 0){
        alert("EXECUTING COMMAND");
}

else, values of pitch etc are string [ex: '0'] then use

var  genere = 'auto';
var pitch = "0";
var   mood = "0";
var speed = "0";
var  identifier = "0";
if (genere !== "auto" || parseint(pitch) !== 0 || parseint(mood) !== 0 || parseint(speed) !== 0 || parseint(identifier) !== 0){
        alert("EXECUTING COMMAND");
}
Divesh Oswal
  • 250
  • 3
  • 13