...

View Full Version : Run a recursive list to update?



SuperRoach
08-21-2007, 06:45 AM
Hello there,
I am trying to recursively (as in a loop) update input boxes to make maintaining code for a calculator easier.

There is basically a Rate value, that gets multiplied (and at the end divided by 2) by input boxes that have the name metreA through to metreE. Then I want to put those values in field names totalA through to totalE.

So the forumula would be

theForm.totalA.value = rate * theForm.mertreA.value / 2

If that works, I'd like to run it for metreA,metreB etc, and put it in the right total.

I've started using jQuery by the way - and love it so far! so if some code looks odd, that might be the reason.

The code itself is:

function CalculateSum(form, PriceRate, Avalue, Bvalue, Cvalue, Dvalue, Evalue)
{
var Rate = parseFloat(PriceRate);
var metreA = parseFloat(Avalue);
var metreB = parseFloat(Bvalue);
var metreC = parseFloat(Cvalue);
var metreD = parseFloat(Dvalue);
var metreE = parseFloat(Evalue);
//var result = Rate*metreA;

// Round to two decimal places
//result = Math.round(10000 * result) / 10000;
result = calculatePersonA(Rate,metreA, metreB, metreC, metreD, metreE);
form.total.value = Math.round(result*100)/100;
calculateOthers(form, Rate, metreB, metreC, metreD, metreE);

}

function calculateOthers(form, Rate, B,C,D,E)
{
// LETTER INCREMENT
//create a string containing all letters
var alphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
//initialize a test character to 'A'
var myChar = "A";
//this function accepts a character, increments it
//by one, and returns it
function incrementLetter(letterToIncrement){

//find where the letter is at in the alphaChars string
var indexOfLetter = alphaChars.search(letterToIncrement);

//if it's not the last letter, then return the next
//letter in the string
if (indexOfLetter+1 < alphaChars.length) {
return(alphaChars.charAt(indexOfLetter+1));
}

//otherwise return the input letter
else{
return(letterToIncrement);
}
}
// END OF LETTER INCREMENT
// END OF LETTER INCREMENT

var i=0
while (i<=4)
{
//alert()
//$("input[@name=eval("total" + myChar)]").val("55");
$("input[@name='totalA']).val("50"));

//document.write("The number is " + i + " of " + myChar);
//document.write("<br >");

//eval("form.total" + myChar + ".value") = myChar * Rate;
myChar = incrementLetter(myChar); // phwoar cool
i=i+1
}
// When the loop is done, reset your variables!
//alert(myChar);
myChar = "A";
//var myChar = "A";
}


the actual info its getting from is a simple html page, with code like:

<label class="quote-total">Total <input type="text" name="totalA" value="" >

The line in bold,
$("input[@name='totalA']).val("50"));
Is pretty much what I need help with - how can I make it change to totalA, totalB, totalC as the loop runs?

Kor
08-21-2007, 10:12 AM
$("input[@name='totalA']).val("50"))

? Do you really need the dollar function? And where is the dollar function? Anyway, I guess it is not a correct syntax anyway (because of the wrong and incomplete quotes). What that line suppose to do?

SuperRoach
08-22-2007, 01:24 AM
$("input[@name='totalA']).val("50"))

? Do you really need the dollar function? And where is the dollar function? Anyway, I guess it is not a correct syntax anyway (because of the wrong and incomplete quotes). What that line suppose to do?

The dollar function calls jQuery. It finds a input box with the name of "totalA", and changes its value to 50.

I have the code working by using:


form.totalB.value = B*Rate/2;
form.totalC.value = C*Rate/2;
form.totalD.value = D*Rate/2;
form.totalE.value = E*Rate/2;
But its very clumsy.

Kor
08-22-2007, 09:29 AM
But what is, for instance B in B*rate/2 ? A variable? What variable? Can you clarify again what kind of math operation you intend to do?

SuperRoach
08-27-2007, 06:21 AM
But what is, for instance B in B*rate/2 ? A variable? What variable? Can you clarify again what kind of math operation you intend to do?

My apologies, I see what you mean...

B is a variable from a input box, I grab that by using jQuery (bless it):
$("input[@name='extraA']").val() - i put each letter into a variable to save having to type it many times

The math operation is fairly simple, but might need to be done many times.
rate is like the others, a value typed into a input box (numerical).
I'd like to have a few series of radio boxes, to tell which person (A - E ) has a extra applied to them. There is two potential extras, one thats worked out with x * rate / 2 (x being a person named a,b,c,d or e), and another one which can be on top of it..

At the moment again, I've got it looking very horrible, looking at the selected radio value, then does work with a bunch of if statements

SuperRoach
08-29-2007, 01:23 AM
Is what I'm asking right?

I ask because there's been no replies, so I wonder if what im asking is really "recursive"..... It comes down to that I'd like to run a loop, and set values to a different object based on what another variable is. Say:


theBox = 'a';
numberToPutin = 500

form.answer[theBox].value = numberToPutin

^-- see how i would like to vary which answer I would like to put it in, by changing the theBox variable before hand... is that possible?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum