简体   繁体   中英

Why does my if and else statement print at the same time?

Why does the else and if statement print? Checking if arrays have matching elements on line 5 which they do...so just my if statement should print. However my alert condition prints with the if condition.I've tried rearranging code,still no luck.

var array1 = [1, 89, 3, 4, 5];

var array2 = [1, 2, 7, 10, 89];

for (var i = 0; i < 6; i++) {
    for (var j = 0; j < 6; j++) {
        if (array1[i] == array2[j]) {
            document.getElementById("demo").innerHTML =
                "Hello World" //Should just print this since elements match
            break;
        } else {
            alert("Error");
            break;
        }
    }
}

If you want to stop the outer loop when you find a match in the inner loop, you need to give a label parameter to break .

outer:
for (var i = 0; i < 6; i++) {
    for (var j = 0; j < 6; j++) {
        if (array1[i] == array2[j]) {
            document.getElementById("demo").innerHTML =
                "Hello World" //Should just print this since elements match
            break outer;
        } else {
            alert("Error");
            break;
        }
    }
}

You still may get some alerts before Hello World is displayed. Your code alerts for every non-matching pair of elements until it finds a match. So unless the matching elements are first in both arrays, you'll get a bunch of alerts before it displays Hello world .

Also, since you have break in both the if and else blocks, you'll never get past the first iteration of the j loop.

If you just want a single alert that indicates that no matching elements were found, you need to move it out of the loop.

var match_found = false;
outer:
for (var i = 0; i < 6; i++) {
    for (var j = 0; j < 6; j++) {
        if (array1[i] == array2[j]) {
            document.getElementById("demo").innerHTML =
                "Hello World" //Should just print this since elements match
            match_found = true;
            break outer;
        }
    }
}
if (!match_found) {
    alert("Error");
}

a for loop will repeat very quickly, which is why you can not see the them doing it at diffrent times; your code does not output at the same time, it is just to fast.

if you are looking into delay, take a look at setInterval .

Just skip the else part. You don't really need that. Unless you wish to do something else if the condition is not true, omit the else statement.

When you call break it only terminates the inner loop. The outer loop will continue to iterate, your conditions will be checked again, and then the else statements run instead.

An alternative way to do this would be:

(function() {
    for (var i = 0; i < 6; i++) {
        for (var j = 0; j < 6; j++) {
            if (array1[i] == array2[j]) {
                document.getElementById("demo").innerHTML =
            "Hello World" //Should just print this since elements match
                return;
            } else {
                alert("Error");
                return;
            }
        }
    }
})();

Wrapping the code in a self executing function allows you to replace break with return to exit the entire function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM