[Web4lib] library calculator/java question
Andrew Hankinson
andrew.hankinson at gmail.com
Mon Mar 23 22:13:26 EDT 2009
Here's a simple rewrite of the doCalculator() function; hopefully it will
help reduce some of the unnecessary verbiage.
I've tried to comment it too, for anyone wishing to learn from it - I know I
was always thirsty for examples when I was learning JS.
/**********
New DoCalculator();
***********/
function doCalculator(){
d = document; // convenience reference (so we don't have to
type "document." all the time.)
total = 0; // a running total
/*
This helps cut down on a lot of the repeated code (and the possibility
of errors).
It essentially sets the static value of the service (i.e "books" are
worth 27.00).
We can then reference this quite easily; to get the value of the
books, it would simply be services[books], which will return 27.00.
*/
services = {
books: 27.00,
paper: 7.00,
child: 20.00,
magazines: 3.50,
movies: 22.00,
audiobooks: 30.00,
magazineuse: 1.00,
ill: 30.00,
meetingroom: 50.00,
// auditorium: 250.00,
adultprogram: 10.00,
childrensprogram: 6.00,
computerhours: 12.00,
// newsarticles: 1.00,
databases: 20.00,
reference: 15.00
};
/*
Once we have the service: value object, we can iterate through them
all, appending their calculated value to the running total.
*/
for(service in services) {
// get the element value for the *input* box and multiply it by the
value of that service in our services object.
itemvalue = (d.getElementById(service).value *
services[service]).toFixed(2);
// Here we simply append "Result" to the name of the services to
address the *output* box, and set it to our calculated value.
d.getElementById(p + "Result").value = parseFloat(itemvalue);
// finally we update our running total...
total += parseFloat(itemvalue);
}
// ... and set the value to the calculated amount.
d.getElementById("totalResult").value = total.toFixed(2);
}
/******
End doCalculator();
******/
On Mon, Mar 23, 2009 at 3:06 PM, Richard James <richardjam at gmail.com> wrote:
> I'm trying to install a "library value calculator" on my own site, with
> next
> to no knowledge of java. Any guidance gladly accepted.
> I carefully cribbed and cleaned up source code from the standard calculator
> as provided by Maine, but I'm pretty sure I must be missing something
> important because none of the total fields will calculate at all:
>
> http://lib.de.us/libraryvalue.html
>
> grateful for any help!
>
> Richard James
> _______________________________________________
> Web4lib mailing list
> Web4lib at webjunction.org
> http://lists.webjunction.org/web4lib/
>
>
More information about the Web4lib
mailing list