I did some looking around and I can't find an answer for this in Javascript. Most of the existing questions I've found were on other languages (Python, C#, SQL, etc) and as a beginner, I'm not quite able to translate concepts between languages yet.
Anyway, what I have is an IF..ELSE statement and the ELSE part always runs even when the IF part evaluates to true and I thought it's supposed to do this:
if (condition) {
block of code to be executed if the condition is true
} else {
block of code to be executed if the condition is false
}
Here's my code:
for(var key in Characters) {
if (Characters[key][matchArr[0]] > 1 && Characters[key][matchArr[1]] > 1 &&
Characters[key][matchArr[2]] > 1 && Characters[key][matchArr[3]] > 1 &&
Characters[key][matchArr[4]] > 1) {
console.log("if...stuff!")
break;
} else {
console.log("Else Stuff");
}
}
I can verify that my first condition is true by simply running it in the console by itself and checking the output (which is true). If run without the ELSE part, I get the expected result "if...stuff!". If run with the ELSE part, I get both.
It seems to have something to do with my condition rather than the how the loop is written or what I'm logging. If I write something like this, the IF..ELSE statement works just as I'd expect.
for(var key in Characters) {
x = true;
if (x === true) {
console.log("if stuff");
break;
} else {
console.log("else stuff");
break;
}
}
What's wrong with my intended condition that it evaluates to true but in an IF...ELSE statement, it still runs the ELSE part.
Characters[key][matchArr[0]] > 1 && Characters[key][matchArr[1]] > 1 &&
Characters[key][matchArr[2]] > 1 && Characters[key][matchArr[3]] > 1 &&
Characters[key][matchArr[4]] > 1
true
Aucun commentaire:
Enregistrer un commentaire