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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculator help in javascript

Status
Not open for further replies.

riffy

Programmer
Mar 29, 2001
106
US
Hi,

I need to create an energy cost calculator for a client site. The user inputs the hours/day he uses a lamp, the days/wk the lamp is used and the cost per kilowatt hour of the lamp.
I've made those three drop down menus. However, to calculate the annual cost, there is no formula, just a number of values that I've been given. For example, if the user inputs 4 hours/day, 5 days/wk and $.06/kwh, then the calculation should equal 0.06. It just takes the value of the previous 3 and gives a number.
I'm thinking that I should set up an array with all the values and then call the array in the annual cost field.

Is that the correct way to do it or is there an easier way?

Thanks
 
another question:

will i have to set up a function that actually figures out what number to output based on what the user has selected?
if not, how do i call the array?
sorry if this sounds basic but i haven't had experience with this before..
or if you can tell me somewhere from where i can get help that will also be appreciated

thanks
 
you can access an arrays attributes like so:

arr = new Array("first element",2,"some other element")

el = arr[0]

el now contains "first element"

you can access the selected value of a dropdown like so:

oSelectBox.options[oSelectBox.selectedIndex].value jared@eae.net -
 
Hi again,

Ok so I can't get this to work. I tried the array way and I can't seem to find out how to call the array in the text box where the calculation needs to get done.
I'm pasting my code, please let me know what's wrong and how it can be fixed.

<script language=&quot;javascript&quot;>
<!-- hide javascript

//var costValues = [.06, .08, .09, .08, .09, .10, .09, .10, .12, .10, .11, .13, .11, .13, .15, .12, .15, .17, .15, .17, .20, .17, .20, .23, .19, .22, .26, .21, .25, .29, .19, .22, .26, .22, .26, .31, .25, .30, .35, .28, .34, .39, .31, .37, .44]

function calculateCost() {
if ((!document.en_cost.hrs_day.options[document.en_cost.hrs_day.selectedIndex].value) || (!document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value) || (!document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value))
{
alert(&quot;Please Fill in ALL boxes!&quot;)
// alert(document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value);
}
else {
if (document.en_cost.hrs_day.options[document.en_cost.hrs_day.selectedIndex].value == 4) {
if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value == 5) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .06;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .08;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .09;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .10;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .11;
break;
}
}
else if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value == 6) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .08;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .09;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .10;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .11;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .13;
break;
}
}
else {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .09;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .10;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .12;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .13;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .15;
break;
}
}
}
else if (document.en_cost.hrs_day.options[document.en_cost.hrs_day.selectedIndex].value == 8) {
if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value==5) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .12;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .15;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .17;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .19;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .21;
break;
}
}
else if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value == 6) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .15;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .17;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .20;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .22;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .25;
break;
}
}
else {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .17;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .20;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .23;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .26;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .29;
break;
}
}
}
else {
if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value == 5) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .19;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .22;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .25;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .28;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .31;
break;
}
}
else if (document.en_cost.days_wk.options[document.en_cost.days_wk.selectedIndex].value == 6) {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .22;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .26;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .30;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .34;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .37;
break;
}
}
else {
switch (document.en_cost.ckwh.options[document.en_cost.ckwh.selectedIndex].value) {
case &quot;.06&quot; :
document.en_cost.ann_cost.value = .26;
break;
case &quot;.07&quot; :
document.en_cost.ann_cost.value = .31;
break;
case &quot;.08&quot; :
document.en_cost.ann_cost.value = .35;
break;
case &quot;.09&quot; :
document.en_cost.ann_cost.value = .39;
break;
case &quot;.10&quot; :
document.en_cost.ann_cost.value = .44;
break;
}
}
}
}
}

//-->
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot;>
<form method=&quot;post&quot; name=&quot;en_cost&quot;>
How many hours per day do you use your lamp?
<select name=&quot;hrs_day&quot; size=&quot;1&quot;>
<option>Select Hours</option>
<option value=&quot;4&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>4</option>
<option value=&quot;8&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>8</option>
<option value=&quot;12&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>12</option>
</select>
<br>
<br>
How many days per week do you use your lamp?
<select name=&quot;days_wk&quot; size=&quot;1&quot;>
<option>Select Days</option>
<option value=&quot;5&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>5</option>
<option value=&quot;6&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>6</option>
<option value=&quot;7&quot; onChange=&quot;this.form.ann_cost.value=this.options[this.selectedIndex].value&quot;>7</option>
</select>
<br>
<br>
What is your energy cost per kilowatt-hour?
<select name=&quot;ckwh&quot; size=&quot;1&quot;>
<option value=&quot;0&quot;>Select Cost</option>
<option value=&quot;.06&quot;>.06</option>
<option value=&quot;.07&quot;>.07</option>
<option value=&quot;.08&quot;>.08</option>
<option value=&quot;.09&quot;>.09</option>
<option value=&quot;.10&quot;>.10</option>
</select>
<br>
<br>
Your annual cost per watt is:
<input type=&quot;text&quot; name=&quot;ann_cost&quot; size=&quot;6&quot; maxlength=&quot;6&quot; onFocus=&quot;calculateCost()&quot;>
<br>
<br>
<br>
<b>Wattage Comparison</b><br>
<br>
How many watts does your incandescent bulb consume?
<input type=&quot;text&quot; name=&quot;incan_watts&quot; size=&quot;6&quot; maxlength=&quot;6&quot;>
<br>
<br>
What is the wattage of a comparable compact fluorescent bulb?
<input type=&quot;text&quot; name=&quot;cfl_watts&quot; size=&quot;6&quot; maxlength=&quot;6&quot;>
<br>
<br>
Total amount of wattage you will save with cfl bulbs:
<input type=&quot;text&quot; name=&quot;watt_save&quot; size=&quot;6&quot; maxlength=&quot;6&quot; readonly onFocus=&quot;this.blur()&quot;;>
<br>
<br>
<br>
<b>Annual Savings</b><br>
<br>
<input type=&quot;text&quot; name=&quot;watt_save&quot; size=&quot;6&quot; maxlength=&quot;6&quot; readonly onFocus=&quot;this.blur()&quot;;>
X
<input type=&quot;text&quot; name=&quot;ann_cost&quot; size=&quot;6&quot; maxlength=&quot;6&quot; readonly onFocus=&quot;this.blur()&quot;;>
= $
<input type=&quot;text&quot; name=&quot;save_lamps&quot; size=&quot;6&quot; maxlength=&quot;6&quot; readonly onFocus=&quot;this.blur()&quot;;>
X
<input type=&quot;text&quot; name=&quot;num_lamps&quot; size=&quot;6&quot; maxlength=&quot;6&quot;>
<br>
<br>
TOTAL SAVINGS :
<input type=&quot;text&quot; name=&quot;tot_save&quot; size=&quot;8&quot; maxlength=&quot;8&quot; readonly onFocus=&quot;this.blur()&quot;;>
</form>
 
Ok,

I set up an array called costValues with all the values (45 in total). Now do I also have to set up a function which calls those values based on the different situations?
Then, I would call that function on the text box which displays the number?

I was just confused on how to call the array when there's no set formula to get the different values so the javascript function will have to contain many if statements.

Thanks
Arif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top