...

View Full Version : Simple Calculator - I've hit a wall..



autotom
11-14-2010, 06:55 AM
Hey,

This is my first post!

I'm trying to learn javascript and i have tried to create a simple calculator, I have a calculator that does

x * x = x

but when i try to cut / paste it into another project it just refuses to work!
This is undoubtably VERY BASIC but any help is VERY much appreciated :)




<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function fmtPrice(value) {
result="$"+Math.floor(value)+".";
var cents=100*(value-Math.floor(value))+0.5;
result += Math.floor(cents/10);
result += Math.floor(cents%10);
return result;
}
function compute() {
var total_price = (document.forms[0].cost.value)*(document.forms[0].qty.value);
document.forms[0].total_price.value=total_price;
var total_price = fmtPrice(total_price);
document.forms[0].total_price.value=total_price;
}
function resetIt() {
document.forms[0].cost.value="0";
document.forms[0].qty.value="0";
document.forms[0].total_price.value="";
}
// End -->
</SCRIPT>



Here is a link to the page - http://dl.dropbox.com/u/29087/Java%20Laptop%20Calc.html
( so you can view source code, saving me from posting it all here )

what am i doing wrong?!! man java is so much harder than VB!

Philip M
11-14-2010, 10:05 AM
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

<script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

x * x = x is not valid syntax. It should be x = x * x.


Finally, do not use the same name (such as total_price) for an HTML element and a Javascript variable.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

autotom
11-14-2010, 10:50 AM
Finally, do not use the same name (such as total_price) for an HTML element and a Javascript variable.



ok, so if i have a textbox that is


<input type="text" name="total_price" size="15" />

and in the header im calling it total_price this is bad? what do i need to do instead?
Ie.


document.forms[0].<b>total_price</b>.value

Forgive me, i am completely new to this and in all honesty i think my brain is just wired in a way which refuses to comprehend it.

if you know of any good books / information that i should go read i'd love to hear of them, the tutorials i've found on the internet so far have done nothing but confuse me further.

Philip M
11-14-2010, 11:45 AM
There is nothing wrong with total_price as such.

But you are using the same name as both a Javascript variable and an HTML element.

document.forms[0].total_price.value=total_price;

Apart from being confusing, that can cause big problems in IE.
JScript (Internet Explorer's equivalent to JavaScript) automatically maps HTML fields with names and ids to the equivalent variable in JScript so you need to avoid using the same name in both places. This is one of the most common reasons for scripts, that run quite nicely in most web browsers, to malfunction when you test them in Internet Explorer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum