Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How come this doesn't add up??

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
I'm adding the total from the answer textarea and the answer2 textarea and put it into total textarea. this is what I used for that:

function calculate() {
var x = document.addem.answer.value
var y = document.addem.answer2.value
var max = document.addem.total.value
if(x>y) {
return max= x + y;
}
if(x<y) {
return max= x - y;
}

}

But nothing happens. How come?? Any ideas??
 
Hi GUJUmOdel,

in your calculate functin , you return the result but did not update the total textarea,

here is what you should change to,

function calculate() {
var x = document.addem.answer.value
var y = document.addem.answer2.value
var max = document.addem.total.value
if(x>y) {
document.addem.total.value = x + y;
}
if(x<y) {
document.addem.total.value = x - y;
}
}


hope this helps, Chiu Chan
cchan@gefmus.com
 
Hi, PepperPepsi, I tried your way it didn't work also. I uploaded my file to this site so you can see what i'm trying to do.


the total box should take the values from 'answer' and 'subanw' and add them up. the value in 'answer' is positive and the values in 'subanw' is negative. so if 'subanw' has a higher value then 'answer' the 'total' box should have a negative sign then the sum. Know what I mean?

thankx man...
 
Hi GUJUmOdel,

here is what i change, and it works fine with me, check it out =)

function calculate() {
var x = addthem()
var y = subtractthem()
if(x>y) {
max = x+y
document.addem.total.value = dollarformat(max)
}
if(x<y) {
max = dollarformat(y-x)
max = &quot;-&quot; + max
document.addem.total.value = max
}
}

the only thing you may need to consider is what happen when x and y (or answer and the subanw are equal)? In that case, the total value will not get update may you should do if (x=y) document.addem.total.value = dollarformat(0).

hope this helps

Chiu Chan
cchan@gefmus.com
 
Hi again,

you should also call calculate() function when you doing the addition or subtration,

function addition() {

document.addem.answer.value = addthem()
document.addem.answer.value = dollarformat(document.addem.answer.value)
calculate()
}

function subtration() {
document.addem.answer2.value = subtractthem()
document.addem.answer2.value = dollarformat(document.addem.answer2.value)
calculate()
}

Chiu Chan
cchan@gefmus.com
 
Hi PepperPespi, thanx man it worked. I inputed a
if (x==y) {
max = x+y
document.addem.total.value = dollarformat(max)
}

and it works. Thanx...
 
BTW, just thought I should mention that your dollar format function doesn't work properly on numbers larger than 999. jared@aauser.com
 
did you write the dollar format function? it's really good, and I want your permission to put it inside my JSX library (with full credits of course) jared@aauser.com
 
hey jaredn, yeah you have my permission to use that code in your JSX library. after the help you gave me, its the least i can do. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top