SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2008
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question checking duplicate numbers

    I want to check duplicate numbers in the boxes. But using the codes below is too long for a lot of boxes. How can I check with shorter and more correct codes ?


    Code JavaScript:
    <SCRIPT type="text/javascript">
     
    function check()
     
    { 
     
    var cella1=document.game.a1.value;
    var cella2=document.game.a2.value;
    var cella3=document.game.a3.value;
    var cella4=document.game.a4.value;
    var cella5=document.game.a5.value;
     
     
     
    if (
    cella1 != cella2  && cella1 != cella3 && cella1 != cella4 && cella1 != cella5 && cella2 != cella3 && cella2 != cella4 && cella2 != cella5 && cella3 != cella4 && cella3 != cella5 && cella4 != cella5)
     
    { alert("ok no duplicate") }
     
     else
     
     { alert("DUPLICATE NUMBERS")}
     
     
    }
     
     
    // -->
    </SCRIPT>
    </HEAD>
     
     
    <BODY>
    <FORM name="game" >
     
    <TABLE summary=""  border="0" width="105" align="center">
    <TR><TD ><INPUT name="a1" size="1"></TD>
    <TD><INPUT  name="a2" size="1"></TD>
    <TD><INPUT  name="a3" size="1"></TD>
    <TD><INPUT  name="a4" size="1"></TD>
    <TD><INPUT  name="a5" size="1"></TD>
     
    <TD>
    </TR>
     
    <TR><TD colspan=3><INPUT class="normal" type="button" value="finish" onclick="check();"></TD></TR>
     
    </TABLE>
     
    <BR>
     
     
    </FORM>

  2. #2
    SitePoint Zealot
    Join Date
    May 2008
    Posts
    182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could always put them in an array and use a for loop. Something like this would work. This is in Java.

    PHP Code:
    int array[] = {25,21,34,25};
            
            
    boolean duplicates false;
            for(
    int x 0< array.length 1x++)
            {
                for(
    int y +1< array.lengthy++)
                {
                    
                    if(array[
    x] == array[y])
                        {
                           
    duplicates true;
                           
    = array.length 2;
                           break;

                        }

                }


            }
            
                
            if(
    duplicates)
                
    System.out.println("You have duplicates"); 

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    If you want to use the indexOf method the code is easier to work with.

    It does mean though adding the following code so that the dumb Internet Explorer browser knows how to get the indexOf an item in an array.

    From the Array.indexOf compatibility section
    Code javascript:
    if (!Array.prototype.indexOf)
    {
      Array.prototype.indexOf = function(elt /*, from*/)
      {
        var len = this.length;
     
        var from = Number(arguments[1]) || 0;
        from = (from < 0)
             ? Math.ceil(from)
             : Math.floor(from);
        if (from < 0)
          from += len;
     
        for (; from < len; from++)
        {
          if (from in this &&
              this[from] === elt)
            return from;
        }
        return -1;
      };
    }

    But with that code for the array indexOf method, the rest of it becomes really easy to do.

    Code javascript:
    function check() { 
        var cells = [];
        var value;
        var duplicates = false;
        for (var i = 1; i < = 5; i += 1) {
            value = document.game['a'+i].value;
            if (cells.indexOf(value) > -1) {
                duplicates = true;
            }
            cells.push(value);
        }
        if (duplicates === false) {
            alert("ok no duplicate");
        } else {
            alert("DUPLICATE NUMBERS");
        }
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •