0

Well, I'm trying to get him to make some fields, if any of them is equal, it's the message and, though my message is giving non-stop, direct, how can I make it stop giving a lot of alert?

function sendAll(){
      for (i = 1;i <=10;i ++) {
         for (o = 1;o <=10;o ++) {
             if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
                 alert("Some field is equal, check again.");
                 break;
                 return false;
             }
          }
      }
}
3
  • Remove the break: that makes it break from the inner loop (the outer loop will keep looping) before the return is executed. Commented Feb 6, 2014 at 19:53
  • I am just glad someone understood the queston Commented Feb 6, 2014 at 19:53
  • @Huangism I'm only 50%, hence a mere comment ;-) Commented Feb 6, 2014 at 19:54

4 Answers 4

2

You don't need break statement, just remove it. return statement will do the rest

use

function sendAll() {
    for (i = 1; i <= 10; i++) {
        for (o = 1; o <= 10; o++) {
            if (document.getElementById("table" + i).value == document.getElementById("table" + o).value) {
                alert("Some field is equal, check again.");
                //break;
                return false;
            }
        }
    }

    //If you are using for validation purpose return true
    // as default
    return true;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Best add a return true; after the outer loop completes or the function will return undefined.
2

Your break simply stops the inner-most loop, but allows the outer loop to continue. To break out of the function entirely, just use a return:

function sendAll(){
      for (i = 1;i <=10;i ++) {
         for (o = 1;o <=10;o ++) {
             if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
                 alert("Some field is equal, check again.");
                 return false;
             }
          }
      }

      return true; // suggested by Mike W
}

2 Comments

Best add a return true; after the outer loop completes or the function will return undefined
@MikeW Good point. It's also possible OP is not actually be using the return value at all, in case return false; should be return;.
0

Try something like this

function sendAll(){
var bEqual = false;
  for (i = 1;i <=10;i ++) {
     for (o = 1;o <=10;o ++) {
         if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
             bEqual = true;
         }
      }
  }
if (bEqual) {
    alert("Some field is equal, check again.");
}
return bEqual;
}

Comments

0

Perhaps you could do:

function sendAll(){
     if (!fieldsOk()) {
         alert('some message');
     } 
}

function fieldsOk() {
    for (i = 1;i <=10;i ++) {
         for (o = 1;o <=10;o ++) {
             if(document.getElementById("table" + i).value==document.getElementById("table" + o).value){
                 return false;
             }
          }
    }
    return true;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.