PDA

View Full Version : Help With a Script

CSS
02-16-2007, 08:58 PM
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?

nikkiH
02-16-2007, 09:01 PM
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.

CSS
02-16-2007, 10:19 PM
Err...

nikkiH
02-16-2007, 10:26 PM
LOL

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

tonyp12
02-16-2007, 11:17 PM
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?

CSS
02-16-2007, 11:53 PM
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?

tonyp12
02-17-2007, 01:24 AM
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.

<html>
<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>
</body>
</html>

tonyp12
02-17-2007, 02:38 AM
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

<html>
<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>
</body>
</html>

tonyp12
02-17-2007, 02:55 AM
There is only one step now left, sort the output and show
what combo created the smallest difference.

<html>
<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>
</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.

CSS
02-17-2007, 04:34 AM
Can you get it to actually show the numbers added together in the written results?

tonyp12
02-17-2007, 03:01 PM
As requested.

<html>
<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>