简体   繁体   中英

how can I code a Javascript for loop so that it breaks when the first condition is met?

I was trying to write a script to find the greatest common factor (highest common factor/HCF), however the loop was counting downward, so I want to break as soon as it finds the first common factor (since it's counting downwards so this is also the HCF).

However, my code turned out to print all the common factors and did not break out of the loop as soon as it found 4, for HCF of 12 and 8.

Here it is:

function maxi(a, b) {
  for (var i = a; i >= 1; i--) {
    for (var j = b; j >= 1; j--) {
      if (a % i == 0) {
        if (b % j == 0 && i == j) {
          console.log(i);
          break;
        }
      }
    }
  }
}
maxi(12, 8);

i'm guessing you have the logic right.

just change

 if (b % j == 0 && i == j){
                    console.log(i);
                    break;
            }

to

   if (b % j == 0 && i == j){
                        console.log(i);
                        return;
                }

One way of doing this is using "return" instead of "break".

 function maxi(a,b){ for (var i=a; i>=1; i--){ for (var j=b; j>=1; j--){ if (a % i == 0){ if (b % j == 0 && i == j){ return i; } } } } } console.log(maxi(12,8)); 

Label your loops like thus:

function maxi(a,b) { 
loop1:
    for (var i = a; i >= 1; i--){
loop2:
        for (var j = b; j >= 1; j--){
            if (a % i == 0){
                if (b % j == 0 && i == j){
                    console.log(i);
                    break loop2; // Breaks out of all loops.
                }
            } 
        }
    }
}

maxi(12,8);

People forget these exist, but they are a completely valid part of the language.

You just need to test whether the desired output has been reached and put in a second break point in your outer for loop like this.

function maxi(a,b){
    var iAmFinished = false;
    for (var i=a; i>=1; i--){
        for (var j=b; j>=1; j--){
            if (a % i == 0){
                if (b % j == 0 && i == j){
                    console.log(i);
                    iAmFinished = true;
                    break;
            }
            }
        }
        if (iAmFinished) {
            break;
        }
    }
}
maxi(12,8);

When you break, unless you specify otherwise, you break from the nearest loop.

You can use a label to specify a specific loop to break from.

 function maxi(a, b) { outer: for (var i = a; i >= 1; i--) { inner: for (var j = b; j >= 1; j--) { if (a % i == 0) { if (b % j == 0 && i == j) { console.log(i); break outer; } } } } } maxi(12, 8); 

There's two equally elegant ways to achieve the same effect.

Option 1 :

Use return instead of break :

function maxi(a, b) {
  for (var i = a; i >= 1; i--) {
    for (var j = b; j >= 1; j--) {
      if (a % i == 0) {
        if (b % j == 0 && i == j) {
          console.log(i);
          return;
        }
      }
    }
  }
}
maxi(12, 8);

When a return statement is called in a function, the execution of this function is stopped. If specified, a given value is returned to the function caller. If the expression is omitted, undefined is returned instead.

source


Option 2 :

Use break in combination with a label :

function maxi(a, b) {
  outerloop:
  for (var i = a; i >= 1; i--) {
    for (var j = b; j >= 1; j--) {
      if (a % i == 0) {
        if (b % j == 0 && i == j) {
          console.log(i););
          break outerloop;
        }
      }
    }
  }
}
maxi(12, 8);

The labeled statement can be used with break or continue statements. It is prefixing a statement with an identifier which you can refer to.

[...]

You can use a label to identify a loop, and then use the break or continue statements to indicate whether a program should interrupt the loop or continue its execution.

source

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