Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help With a Script

    I really, really need a script, and I have very little knowledge with JavaScript, PHP, anything other than HTML...

    I need a form where I can submit 6 numbers, and with those 6 numbers, the script pairs of 2 sets of 3 numbers that are closest to each other in equality. For example:
    122
    981
    735
    301
    202
    100

    981, 122, 100 vs. 735, 301, 202
    1203 vs. 1235

    Is this possible, or do I need more clarification?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Put them into an array and sort the array.
    http://www.w3schools.com/js/js_obj_array.asp

    Grab however many values at a time you want.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Err...

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    LOL

    Okay, show me the part where the user enters the 6 numbers.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You need a code that will try every possible combination
    and then show the one that produce the smallest
    difference between left side and right side?

    First we need to figure our how many possible combinations are there.

    first one: 1,2,3 - 4,5,6
    second : 2,3,4 - 5,6,1
    third : 3,4,5 - 6,1,2
    fourth : 4,5,6 - 1,2,3
    fifth : 1,2,4 - 3,5,6
    sixth : 1,2,5 - 3,4,6
    and so on.

    How many possible ways are there?
    A simple loop to try them them all?
    Last edited by tonyp12; 02-16-2007 at 11:26 PM.

  • #6
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tonyp12 View Post
    You need a code that will try every possible combination
    and then show the one that produce the smallest
    difference between left side and right side?

    First we need to figure our how many possible combinations are there.

    first one: 1,2,3 - 4,5,6
    second : 2,3,4 - 5,6,1
    third : 3,4,5 - 6,1,2
    fourth : 4,5,6 - 1,2,3
    fifth : 1,2,4 - 3,5,6
    sixth : 1,2,5 - 3,4,6
    and so on.

    How many possible ways are there?
    A simple loop to try them them all?
    Yes! Exactly... there should be... 16 combinations? That doesn't sound right... Do you need me to list them all?
    1,2,3 - 4,5,6
    2,3,4 - 1,5,6
    2,5,6 - 1,3,4
    2,4,6 - 1,3,5
    3,5,6 - 1,2,4
    3,4,6 - 1,2,5
    1,2,6 - 3,4,5
    1,2,5 - 3,4,6

    I think that's them all?
    Last edited by CSS; 02-16-2007 at 11:57 PM.

  • #7
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm thinking maybe 64 combinations.

    Each number can be on the left or right
    so each number have two possible location.

    2*2*2*2*2*2 = 64

    But that would allowed all 6 to be on the left and 0 on the right so that is not correct either.

    So if you take the decimals 0-64 and check what number have ONLY 3 bits as 1's when you get the correct number and that how the loop should work too.


    This says 20 combos are possible.

    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    function findcombo(){
    
     for (var i=0; i<64; i++){
      if ((i&1) + ((i&2)>>1) + ((i&4)>>2) + ((i&8)>>3) + ((i&16)>>4) + ((i&32)>>5) == 3){
      document.write(i +"<br>")
      }
     }
    }
    </script>
    </head>
    <body onLoad="findcombo()">
    </body>
    </html>
    Last edited by tonyp12; 02-17-2007 at 01:51 AM.

  • #8
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here we test the 20 possible combos
    But I do see that the sums are represented as mirrored.
    That makes sence as with 1,2,3 - 4,5,6 you would not need to test 4,5,6 - 1,2,3



    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    var inputs= new Array(100,167,200,79,145,12)
    var combo= new Array (7,11,13,14,19,21,22,25,26,28,35,37,38,41,42,44,49,50,52,56)
    
    function calccombo(){
      for (var i=0; i<20; i++){
     var a=0,b=0
     if (combo[i]&1){a+=inputs[0]} else {b+=inputs[0]}
     if (combo[i]&2){a+=inputs[1]} else {b+=inputs[1]}
     if (combo[i]&4){a+=inputs[2]} else {b+=inputs[2]}
     if (combo[i]&8){a+=inputs[3]} else {b+=inputs[3]}
     if (combo[i]&16){a+=inputs[4]} else {b+=inputs[4]}
     if (combo[i]&32){a+=inputs[5]} else {b+=inputs[5]}
     
     document.write(a + " vs " + b + "<br>")   
     }
    }
    </script>
    </head>
    <body onLoad="calccombo()">
    </body>
    </html>
    Last edited by tonyp12; 02-18-2007 at 12:32 AM.

  • #9
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    There is only one step now left, sort the output and show
    what combo created the smallest difference.

    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    var inputs= new Array(109,166,202,78,115,19) // any numbers you want.
    var combo= new Array (7,11,13,14,19,21,22,25,26,28)
    
    function calccombo(){
     for (var i=0; i<10; i++){
     var a=0,b=inputs[5]
     if (combo[i]&1){a+=inputs[0]} else {b+=inputs[0]}
     if (combo[i]&2){a+=inputs[1]} else {b+=inputs[1]}
     if (combo[i]&4){a+=inputs[2]} else {b+=inputs[2]}
     if (combo[i]&8){a+=inputs[3]} else {b+=inputs[3]}
     if (combo[i]&16){a+=inputs[4]} else {b+=inputs[4]}
     
     document.write(a + " vs " + b + " &nbsp; a difference of &nbsp; " + Math.abs(a-b) + "<br><br>")   
     }
    }
    </script>
    </head>
    <body onLoad="calccombo()">
    </body>
    </html>
    you could use: var inputs= new Array(1,2,4,8,16,32)
    just to get an example where two outputs are impossible to be the same.
    Last edited by tonyp12; 02-17-2007 at 04:32 AM.

  • #10
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can you get it to actually show the numbers added together in the written results?

  • #11
    New Coder
    Join Date
    Jan 2007
    Posts
    93
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As requested.
    Code:
    <html>
    <head>
    <script type="text/JavaScript">
    
    var inputs= new Array(1,2,4,8,16,32) // any numbers you want.
    var combo= new Array (7,11,13,14,19,21,22,25,26,28)
    
    function calccombo(){
     for (var i=0; i<10; i++){
    
     var a=[6],j=0,l=3;a[l++]=inputs[5]  
     if (combo[i]&1){a[j++]=inputs[0]} else {a[l++]=inputs[0]}
     if (combo[i]&2){a[j++]=inputs[1]} else {a[l++]=inputs[1]}
     if (combo[i]&4){a[j++]=inputs[2]} else {a[l++]=inputs[2]}
     if (combo[i]&8){a[j++]=inputs[3]} else {a[l++]=inputs[3]}
     if (combo[i]&16){a[j++]=inputs[4]} else {a[l++]=inputs[4]}
     
     var diff= Math.abs((a[0]+a[1]+a[2]) - (a[3]+a[4]+a[5]))
     document.write(a[0]+" + "+a[1]+" + "+a[2]+" &nbsp; vs &nbsp; "+a[3]+" + "+a[4]+" + "+a[5]+" &nbsp;&nbsp; a difference of &nbsp;" + diff + "<br><br>")   
     }
    }
    </script>
    </head>
    <body onLoad="calccombo()">
    </body>
    </html>

  • #12
    CSS
    CSS is offline
    New Coder
    Join Date
    Jun 2006
    Posts
    40
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey, this script is working great, it's helped me a lot! What's a little tedious is editting it with Notepad so much. Is there any way to make it bring up 6 command prompts that ask for the 6 numbers when the webpage is loaded, rather than having to insert it directly into the source?


  •  

    Posting Permissions

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