Closed Thread Icon

Preserved Topic: Simple calculations using javascript????? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=17916" title="Pages that link to Preserved Topic: Simple calculations using javascript????? (Page 1 of 1)" rel="nofollow" >Preserved Topic: Simple calculations using javascript????? <span class="small">(Page 1 of 1)</span>\

 
TheTrixter
Bipolar (III) Inmate

From: Derbyshire, UK
Insane since: Jul 2000

posted posted 01-12-2001 09:30

I don't even know if this can be done using javascript (or it is java?) but stay with me . . . . .

If I have a form on a site for example where the surfer inputs three numerical values A, B, and C.
Is there an easy way to perform simple calculations using these values (eg (a+B)xC and then display the results on screen???

It seems like such a simple thing to do (takes me back to when I was a kid learning BASIC on a ZX Spectrum, BUT THEN I FORGOT ABOUT PROGRAMMING FOR 15 YEARS, DAMN IT WHY DIDN'T I CARRY ON LEARNING INSTEAD OF DRINKING!!! ).

Thanks in advance guys (and girls of course)

mobrul
Bipolar (III) Inmate

From:
Insane since: Aug 2000

posted posted 01-12-2001 21:25

This is interesting. I've been thinking lately about trying to make a calculator for a website. I created a nice looking calculator in PS and wanted it to actually work in a browser, like a real calculator. You know, click on a number and that number appears in the screen (all precreated graphics) then each of the + - / * and = signs would be on buttons, and some script would keep track of all that was going on to work like a real calculator. Wasn't really going to worry about it yet, but since Trixter asked first, thought I'd throw this in. Any thoughts on how to get started???

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-12-2001 23:44

Go to Blood Alcohol Calculator and click on the button. You'll see one that I am working on. Nothing fancy, but it might answer your question.

Pat Richard
Web weenie
http://www.gurusnetwork.com
ICQ 23113317

[This message has been edited by Pugzly (edited 01-12-2001).]

SCarab
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2000

posted posted 01-13-2001 05:24

I may not make the prettiest web pages in the world, but this sort of question is something I can handle. To see something I did with forms and calculations, swing by:
http://home.earthlink.net/~scarabz/designs/juggler/juggler.html

The code gets rather long, just because there's a wide variety of things it can do. If you want me to give a code snippet for something simpler, rather than trying to figure out that mass of javascript code, let me know, and I'll come up with something.

Good luck; I'm curious as to just what you wanted to do with this.

--SCarab

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-13-2001 06:44

Man, stuff like that should be on the gurusnetwork!

Pat Richard
Web weenie
http://www.gurusnetwork.com
ICQ 23113317

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 01-13-2001 06:55

You can also perform the computations and include the results "inline" by including javascript code in the body of your page. Usually you put the javascript in the header. Here's an example page:

code:
<html>
<body>
<script language="JavaScript">
var a=2
var b=3
var c=5
document.write( "a = "+a+"<br>" )
document.write( "b = "+b+"<br>" )
document.write( "c = "+c+"<br>" )
document.write( "( a + b ) &times c = "+((a+b)*c) )
</script>
</body>
</html>



TheTrixter
Bipolar (III) Inmate

From: Derbyshire, UK
Insane since: Jul 2000

posted posted 01-13-2001 07:00

Scarab, I like the juggler, but I have no idea which pieces of code I need to strip out. Could you help me out with a real simple example.
What I need to do is for a client that rents student accomodation. He want a site where students can input some variables lets say for example:-
Number of bedrooms: (betweeon 1 to 4)
Location: ((4 different areaas, ABC&D with different price structures to each)
Quality of accomodation: (From 1 to 4)
Completely made up of course, he will obviously have something sensible in mind.
Anyway what I need to know it how to get students to input these three fields and then perform a simple calculation on them:
Lets say Field1*(Field2+Field3)

Just something that simple. If you could give me a working example without all the clutter I'm hoping I would be able to follow it and figure out how it was done.
Thanks

TheTrixter
Bipolar (III) Inmate

From: Derbyshire, UK
Insane since: Jul 2000

posted posted 01-13-2001 07:03

Oob Bug, you beat me by 5 minutes. That looks very close to what I'm after, except that the value for the variable a, b & c need to come from the forms input, how do you factor that into the calculation?

EDIT: I don't know if using forms is the right way to do this now, can you still use the variable created for each field because normally I have got the "form method=post" thing goin on so will that try to send the variable somewhere as an email?
What do I have to use instead. Blimey this is turning out to be more comlicated than I thought.
HHEEELLLLPPP



[This message has been edited by TheTrixter (edited 01-13-2001).]

TheTrixter
Bipolar (III) Inmate

From: Derbyshire, UK
Insane since: Jul 2000

posted posted 01-13-2001 07:13

Pugzly, I've just had a look at the alcohol calculator! Excellent.
That is exactly the kind of thing I'm after, but I have no idea what all the code means, I will try and study it over the weekend but I looksa little heavy for me at the moment.

SCarab
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2000

posted posted 01-15-2001 04:24

OK, it looks like you have everything you need except how to pull data out of a form. It sounds like all of the forms you're looking for can be "selects" rather than anything where you type... So, in the body of the html, you should have something like:

<form name="theForm" onSubmit="doTheFunction(); return false;">
Choose item 1:<BR>
<select name="itemOne" size=1>
<option>1
<option>2
<option>3
<option>4
</select><BR>
Choose item 2:<BR>
<select name="itemTwo" size=1>
<option>1
<option>2
<option>3
<option>4
</select><BR>
</form>

Assuming that the above stuff isn't in a layer or something, then wherever you decide to put your script (usually in the head), you should have something like:

<SCRIPT LANGUAGE="JavaScript">
//some stuff...
function doTheFunction(){
var the_select = window.document.theForm.itemOne;
var selected = the_select.options[the_select.selectedIndex].text;
inItemOne = parseInt(selected);
the_select = window.document.theForm.itemTwo;
selected = the_select.options[the_select.selectedIndex].text;
inItemTwo = parseInt(selected);
// inItemOne and inItemTwo hold the numbers chosen in each of the selects
// stuff to do calculations
</script>

The above stuff assumes that the stuff in the selects are just numbers; if not, then you can use some if statements or something rather than using parseInt(selected).

BTW, I learned how to do all of this stuff by doing the WebMonkey tutorials over a few days... they're very well written, and pretty easy to understand.

Good luck with it.

--SCarab

SCarab
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2000

posted posted 01-15-2001 04:31

Um, Pugzly, was that comment about gurusnetwork directed at me? I thought my juggler page was fun, but wouldn't be the best subject of a tutorial: the javascript code could still use a bit of "cleaning up" and would be rather long (and, thus, annoying to wade through) in any event. I suppose I could make a much-simplified version, to use as an example of grabbing data from javascript forms, but as I noted, WebMonkey does a pretty darned good job of that already.

I'm glad I could help someone out, and welcome any comments about my stuff.

--SCarab

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 01-15-2001 05:04

Trixter - There is a lot of validation code in there to make sure someone enters data in all four fields. That's optional.

SCarab - Yes, I was referring to you. So clean it up and post it!

Pat Richard
Web weenie
http://www.gurusnetwork.com
ICQ 23113317

« BackwardsOnwards »

Show Forum Drop Down Menu