# How do I go about forcing three or more fields to add up to a number?

Page 1 of 1

## 3 Replies - 397 Views - Last Post: 19 June 2012 - 09:12 PM

### #1 Chwaa

Reputation: 2
• Posts: 14
• Joined: 26-December 10

# How do I go about forcing three or more fields to add up to a number?

Posted 19 June 2012 - 07:45 PM

This is a question of best practice and how I should implement something.

What I am trying to do is to get three or more fields to all add up to a specific number. For example, I have three fields (A, B, and C) that must add up to 10. At the moment, A = 4, B = 5, and C = 1. However, let's say an end user changes the value of A to 6, upon which B and/or C would rebalance in some way to ensure that A + B + C = 10. It is possible that the user may then go on to change another field and expect the previously changed field (A, in this case) to remain as 6.

I've been racking my head trying to think of an ideal solution, but cannot seem to do so. Any help and suggestions would be greatly appreciated as far as what algorithm to implement that will fulfil the task of ensuring that A, B, and C add up to 10 while the user doesn't get frustrated and confused when trying to edit the value in a field.

Thanks!

Is This A Good Question/Topic? 0

## Replies To: How do I go about forcing three or more fields to add up to a number?

### #2 fromTheSprawl

• Bloodborne

Reputation: 521
• Posts: 2,102
• Joined: 28-December 10

## Re: How do I go about forcing three or more fields to add up to a number?

Posted 19 June 2012 - 08:06 PM

Why not:

- Check if the sum is less than or equal to 10. If the user proceeds with less than 10, prompt that you need 10 as the sum.
ex: 1-4-3 will output "Insufficient funds to launch warhead."
ex: 1-5-4 will proceed

- If the user will proceed inputting numbers which will result in the sum being greater than 10, annoy them.
ex: user inputs 11 on the first field will output "Value greater than 10 would detonate the nuke. Please input something less than 10."
ex: user inputs 1, then 5, then 5. At 5, output the message above, and your code shouldn't let the user proceed without fulfilling your condition.

This is just basically you checking for the sum of the three fields on every change and ensuring proper validation. You can do it.
```function validateTen(a,b,c){
var sum = a + b + c;
if(sum > 10){
alert("Stop right there! Are you stupid? I need numbers that would add up to 10.");
laserFryUsersBrain();
} else {
}
}
}

```

By the way I re-read your question, what is the use of the user inputting a number if you would just randomly generate the other two? If it is that simple, just make a random int object which is less than 10 - the input of the user and give it to a. Then if the sum of a and the field is still insufficient, do c = (a + - 10. If not, set c to 0. That simple.

If you won't delve into specifics like where would you need to do this and what language you would use, the answer you'll get will be general or vague.

I hope I made sense. Good luck!

### #3 Chwaa

Reputation: 2
• Posts: 14
• Joined: 26-December 10

## Re: How do I go about forcing three or more fields to add up to a number?

Posted 19 June 2012 - 08:24 PM

To be more specific, this is meant to be a way to split expenses between several people. In this case, the \$10 is the total expense. A, B, C are three people being charged \$4, \$5, and \$1 respectively. If A is changed to \$6, then the expenses for B and C need to be adjusted in some way to compensate. The issue is complicated in that subsequent changes to B or C should ideally not affect A (unless it has to).

The reason I don't want a simple error message is because it seems too clunky for the website I want to make. I need to think of a solution that is dynamic and sexy.

I'm using Javascript, but I'm not asking about the code, just the conceptual implementation.

### #4 fromTheSprawl

• Bloodborne

Reputation: 521
• Posts: 2,102
• Joined: 28-December 10

## Re: How do I go about forcing three or more fields to add up to a number?

Posted 19 June 2012 - 09:12 PM

Why would A be so important as to not warrant subsequent changes? Is it perhaps because of ordering? If A changes first then if B changes next only C would be affected? By the way, I cooked up some spaghetti with jQuery.

```\$(document).ready(function(){
var a =0;
var b= 0;
var c = 0;
sum = 0;
\$("#custA").change(function(event){
a = \$("#custA").val();
validate('a');
;});
\$("#custB").change(function(event){
b = \$("#custB").val();
sum = parseInt(a) + parseInt(B)/> + parseInt(c);
validate('b');

;});
\$("#custC").change(function(event){
c = \$("#custC").val();
sum = parseInt(a) + parseInt(B)/> + parseInt(c);
validate('c');

;});
\$("a").click(function(event){
event.preventDefault();
\$("#hack").show("slow");});

function validate(pos){
sum = parseInt(a) + parseInt(B)/> + parseInt(c);
console.log("Sum is ",sum);
console.log("Pos is ",pos);
if(sum>10){
var total = 0;
var giveItToHim = 0;
var giveItToHer = 0;
var him = '';
var her = '';
if(pos=='a'){
total = 10 - parseInt(a);
him ='B';
her ='C';
} else if (pos =='b') {
total = 10 - parseInt(B)/>;
him ='A';
her ='C';
} else{
total = 10 - parseInt(c);
him ='A';
her ='B';
}

giveItToHim = parseInt(total / 2);
giveItToHer = total - giveItToHim;
\$("#cust"+him).val(giveItToHim);
\$("#cust"+her).val(giveItToHer);

}
}
});

```

This pasta evens up the three fields to add up to 10, regardless if anything changes. No error message. Hey, if you want sexy, might want to look up jQuery or if not, with your skill in Javascript and CSS you can create unobstrusive error messages(ex. opting for text in the HTML rather than annoying alert boxes)

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}