Jump to content
xisto Community
warbird1405241485

Problem With Javascript calculate() doesnt work

Recommended Posts

Ok here's the problem: I'm trying to make a calculator thing for in my site. Problem is, if I push the = button, then it should trigger the function calculate() but for some reason it doesn't work. Here's the script:

<HTML><HEAD><script LANGUAGE="JavaScript"> function fieldWrite(t) {   with (document.Calc) {     textField.value += t;   } } function clearField(p) {   with (document.Calc) {     a = parseFloat(textField.value);     textField.value = "";   } }  function calculate()  {   with (document.Calc) {     b = parseFloat(textField.value);     switch(o) {       case "+":         c = a + b;         break;       case "-":         c = a - b;         break;       case "*":         c = a * b;         break;       case "/":         c = a / b;         break;       case "%":         c = a % b;         break;     textField.value = c;     }   } }</SCRIPT></HEAD><BODY><FORM NAME="Calc"><INPUT TYPE="text" ID="textField" ALIGN="right"><P><INPUT TYPE="button" VALUE="  1  " onclick=fieldWrite("1")><INPUT TYPE="button" VALUE="  2  " onclick=fieldWrite("2")><INPUT TYPE="button" VALUE="  3  " onclick=fieldWrite("3")><INPUT TYPE="button" VALUE="  +  " onclick=clearField("+")><BR><INPUT TYPE="button" VALUE="  4  " onclick=fieldWrite("4")><INPUT TYPE="button" VALUE="  5  " onclick=fieldWrite("5")><INPUT TYPE="button" VALUE="  6  " onclick=fieldWrite("6")><INPUT TYPE="button" VALUE="   -  " onclick=clearField("-")><BR><INPUT TYPE="button" VALUE="  7  " onclick=fieldWrite("7")><INPUT TYPE="button" VALUE="  8  " onclick=fieldWrite("8")><INPUT TYPE="button" VALUE="  9  " onclick=fieldWrite("9")><INPUT TYPE="button" VALUE="   /  " onclick=clearField("/")><BR><INPUT TYPE="button" VALUE="  #  " ><INPUT TYPE="button" VALUE="  0  " onclick=field_write("0")><INPUT TYPE="button" VALUE="  =  " onclick=calculate()><INPUT TYPE="button" VALUE="   *  " onclick=clearField("*")><BR></BODY></HTML>


Please tell me what's wrong with it.

Share this post


Link to post
Share on other sites

I see several problems. The first is that a, b and o are all local variables, so a cannot be accessed by the calculate function. To change this, before all of the functions add these lines:

var a;var b;var o;
The next is that you never define o. To fix this add in the clearField function just after textField = "";
o = p;
.
The third problem is that your statement textField.value = c; in the calculate function is inside the switch block. Simply move it outside of the switch block like so:
function calculate(){  with (document.Calc) {    b = parseFloat(textField.value);    switch(o) {      case "+":        c = a + b;        break;      case "-":        c = a - b;        break;      case "*":        c = a * b;        break;      case "/":        c = a / b;        break;      case "%":        c = a % b;        break;    }    textField.value = c;  }}
The last problem is that the user must hit enter after every operation instead of being able to do two additions without the equal button, but I leave fixing that up to you. Otherwise this is a very well done calculator.

~Viz

Share this post


Link to post
Share on other sites

The reputation system is currently offline because as Xisto updates its system we are doing so piece by piece to minimize headaches. But I appreciate it and want you to know that all previous reputation data is still stored in the database and the system will be coming back.~Viz

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.