...

View Full Version : Script Problem that i just cant see :(



masschino
11-08-2004, 07:56 AM
Hi there, first time poster on this forum. I'll be the first one to point out that i am infact a novice - and to my knowledge the javascript im about to post should work, but ive done my head in trying to figure out where ive went wrong.

The javascript;


function load()
{
window.status = "Made by Marshall F 2004"
}

function count()
{
var item1price = 800;
var item2price = 800;
var item3price = 800;
var item4price = 800;
var item5price = 800;
var item6price = 800;
var item7price = 800;
var item8price = 800;
var item9price = 800;
var item10price = 800;
var item11price = 800;
var item12price = 800;
var item13price = 800;
var item14price = 800;
var item15price = 800;
var item16price = 800;
var item17price = 52;
var item18price = 12;




if (calc.item1.checked){
var witem1 = document.calc.item1.value = item1price;
} else {
var witem1 = document.calc.item1.value = 0;
}

if (calc.item2.checked){
var witem2 = document.calc.item2.value = item2price;
} else {
var witem2 = document.calc.item2.value = 0;
}

if (calc.item3.checked) {
var witem3 = document.calc.item3.value = item3price;
} else {
var witem3 = document.calc.item3.value = 0;
}

if (calc.item4.checked) {
var witem4 = document.calc.item4.value = item4price;
} else {
var witem4 = document.calc.item4.value = 0;
}

if (calc.item5.checked) {
var witem5 = document.calc.item5.value = item5price;
} else {
var witem5 = document.calc.item5.value = 0;
}

if (calc.item6.checked) {
var witem6 = document.calc.item6.value = item6price;
} else {
var witem6 = document.calc.item6.value = 0;
}

if (calc.item7.checked){
var witem7 = document.calc.item7.value = item7price;
} else {
var witem7 = document.calc.item7.value = 0;
}

if (calc.item8.checked){
var witem8 = document.calc.item8.value = item8price;
} else {
var witem8 = document.calc.item8.value = 0;
}

if (calc.item9.checked) {
var witem9 = document.calc.item9.value = item9price;
} else {
var witem9 = document.calc.item9.value = 0;
}

if (calc.item10.checked) {
var witem10 = document.calc.item10.value = item10price;
} else {
var witem10 = document.calc.item10.value = 0;
}

if (calc.item11.checked) {
var witem11 = document.calc.item11.value = item11price;
} else {
var witem11 = document.calc.item11.value = 0;
}

if (calc.item12.checked) {
var witem12 = document.calc.item12.value = item12price;
} else {
var witem12 = document.calc.item12.value = 0;
}

if (calc.item13.checked){
var witem13 = document.calc.item13.value = item13price;
} else {
var witem13 = document.calc.item13.value = 0;
}

if (calc.item14.checked){
var witem14 = document.calc.item14.value = item14price;
} else {
var witem14 = document.calc.item14.value = 0;
}

if (calc.item15.checked) {
var witem15 = document.calc.item15.value = item15price;
} else {
var witem15 = document.calc.item15.value = 0;
}

if (calc.item16.checked) {
var witem16 = document.calc.item16.value = item16price;
} else {
var witem16 = document.calc.item16.value = 0;
}

if (calc.item17.checked) {
var witem17 = document.calc.item17.value = item17price;
} else {
var witem17 = document.calc.item17.value = 0;
}

if (calc.item18.checked) {
var witem18 = document.calc.item18.value = item18price;
} else {
var witem18 = document.calc.item18.value = 0;
}

document.calc.pay.value = witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8 + witem9 + witem10 + witem11 + witem12 + witem13 + witem14 + witem15 + witem16 ;
document.calc.weekly.value = witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8 + witem9 + witem10 + witem11 + witem12 + witem13 + witem14 + witem15 + witem16 / witem17 ;
document.calc.monthly.value = witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8 + witem9 + witem10 + witem11 + witem12 + witem13 + witem14 + witem15 + witem16 / witem18 ;
}


the above then works with a page to calculate anywhere from 1 to 16 check boxes, then i have 3 text boxes to spit out "pay" "weekly" "and monthly"

basically, its to add up all the boxes, then display a total, weekly amount, & monthly. I thought id done it right.

Here is a snippet of the HTML page just in case ive made some mistake



<tr>
<td bgcolor="#eeeeee"><span class="normalText">Monthly</span></td>
<td bgcolor="#eeeeee" align="right">
<input type="checkbox" name="item18" size="10" value="ON"></td>
</tr>
<tr>
<td bgcolor="#cccccc" colspan="2"><span class="normalText"><b>Please Note:</b><br>Both options include:<br>- cost of return airfares to Brazil<br>- all necessary airport taxes during your travel<br>- airport cost from and to the airport<br>- full accommodation<br>- provision of necessary papers and useful information during your stay</span></td>
</tr>

<tr>
<td bgcolor="#cccccc" colspan="2" align="right"><span class="normalText">Price
$</span>
<input type="text" name="pay" size="10" style="background-color:#ffffff; color:#000000; border:1px solid #000000; font-family:tahoma; font-size:8pt; letter-spacing=1px"><input type="text" name="weekly" size="10" style="background-color:#ffffff; color:#000000; border:1px solid #000000; font-family:tahoma; font-size:8pt; letter-spacing=1px"><input type="text" name="monthly" size="10" style="background-color:#ffffff; color:#000000; border:1px solid #000000; font-family:tahoma; font-size:8pt; letter-spacing=1px"></td>
</tr>
<tr>
<td bgcolor="#eeeeee" align="center"></td>
<td bgcolor="#eeeeee" align="right"><input type="button" onClick="count()" value="Calculate" style="background-color:#ffffff; color:#000000; border:1px solid #000000; font-family:tahoma; font-size:8pt; letter-spacing=1px">
<input type="reset" value="Reset" style="background-color:#ffffff; color:#000000; border:1px solid #000000; font-family:tahoma; font-size:8pt; letter-spacing=1px"></td>
</tr>


above all that are item16, item15, i just thought i wouldnt bother you with that.

Is there a problem with my javascript, can it do what i want it to?

jbot
11-08-2004, 10:19 AM
Is there a problem with my javascript, can it do what i want it to?

well, you tell us. does the script return an error when run, and if so, what is it?

Kor
11-08-2004, 10:22 AM
if (document.calc.item1.checked){
...
and

document.calc.weekly.value = (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8 + witem9 + witem10 + witem11 + witem12 + witem13 + witem14 + witem15 + witem16 )/ witem17 ;

document.calc.monthly.value = (witem1 + witem2 + witem3 + witem4 + witem5 + witem6 + witem7 + witem8 + witem9 + witem10 + witem11 + witem12 + witem13 + witem14 + witem15 + witem16 )/ witem18 ;
}

I presume you want to divide the wholw sum, not only the last term

On the other hand you may squeeze and simplify the script using arrays and loops, something like that (untested):



<script language="JavaScript" type="text/JavaScript">
var itemvalue=new Array()
for(var i=1;i<17;i++){
itemvalue[i]=800;
}
itemvalue[17]=52;
itemvalue[18]=12;
function count(){
c=document.calc.elements;
pval=0;
for(var i=1;i<19;i++){
if(c['item'+i].checked){
c['item'+i].value=itemvalue[i];
}
else{
c['item'+i].value=0
}
if(i<17){
pval=pval+c['item'+i].value;
}
}
document.calc.pay.value=pval;
document.calc.weekly.value=pval/itemvalue[17];
document.calc.monthly.value=pval/itemvalue[18]
}
</script>

masschino
11-08-2004, 10:22 AM
it works but has its limitations

if i select item 15 or 16, then select item 17 (display as weekly) or 18 (display as monthly) and click calculate, it will do it no worries.

if i select anything else, it will simply add the value of the other text boxes to the total.

Just cant think of what im doing wrong

masschino
11-08-2004, 10:31 AM
oh my god, why didnt i think of that.

brackets!

it completely works now (& seems bullet proof)

thank you!

jbot
11-08-2004, 10:34 AM
you guys repsonded too quickly ... you been quaffing rocket fuel or something :confused: LOL

Kor
11-08-2004, 10:58 AM
how many posts does it take to become a CF Nut ... i wanna be a nutter!!


i donno... :rolleyes: Keepi it on... I have harly noticed the Nut badge on my... 1000 I presume... But not 1000 replays, 1000 threads replayed... :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum