# URGENT Help Needed with JavaScript Calculator

Community Member Posts: 389 ♪ Opening Act ♪
Hi, all -

I've been using a little JavaScript calculator that I picked up somewhere on the Internet many, many years ago  in many of my Lectora e-courses and never noticed until today that it has a significant flaw, which is affecting one of the practice activities that I have in a course I'm currently working on.

The calculator works fine EXCEPT that, if I enter a decimal point followed by a zero and then a number, it removes the decimal point, deletes the zero, and turns the number into a whole number (for example, if I enter ".05" it changes it to "5"). If I enter, say, ".34" then it stays ".34" -- it's only when there's a zero after the decimal point that it has this problem and immediately after typing the 0, deletes the decimal point and the zero and types whatever comes next as a whole number.

Here is the code I've been using:

x = 0;
ops = "n";
token = 0;

function calc(op)
{ if(!isNaN(op) || op==".")
{ if(!token)
{ if(document.calculator.win.value == 0)
{ document.calculator.win.value = op; }
else
{ document.calculator.win.value = document.calculator.win.value + op; }
}
else
{ document.calculator.win.value = op;
token = 0;
}
return;
}
else
{ if(op=="C")
{ document.calculator.win.value = 0;
token = 0;
return;
}

if(op=="CE")
{ document.calculator.win.value = 0;
return;
}

if(op=="%")
{ document.calculator.win.value = document.calculator.win.value / 100.0;
token = 1;
return;
}

if(op=="+/-")
{ document.calculator.win.value = -document.calculator.win.value;
token = 1;
return;
}

if(op=="+" || op=="*" || op=="/" || op=="-" || op=="=")
{ token = 1;
if(ops!="n")
{ if(ops=="+")
{ x = x -(- document.calculator.win.value);
document.calculator.win.value = x;
}
if(ops=="-")
{ x = x - document.calculator.win.value;
document.calculator.win.value = x;
}
if(ops=="/")
{ x = x / document.calculator.win.value;
document.calculator.win.value = x;
}
if(ops=="*")
{ x = x * document.calculator.win.value;
document.calculator.win.value = x;
}
}
else
{ x = document.calculator.win.value; }

if(op!="=") { ops=op; }
else { ops="n"; }
return;
}
}
}

Unfortunately, I'm not a JavaScript coder and can't figure out how to fix this, despite having spent some time researching this problem and finding a discussion about it here (http://www.anaesthetist.com/mnm/javascript/calc.htm) that I think addresses it, but I can't figure out how to translate what I'm reading there into what changes I need to make in my version, since they're not written similarly enough for this non-coder to decipher. So, I'd be deeply grateful if someone can give me  the fixed code in a way that I can just copy and paste it into (or on top of) the contents of my existing calculate.js file (or just send me back my file all fixed).

Laura

P.S. In case it helps, I've attached the original .js file and another file that contains the HTML code I used to provide the UI for the calculator.

• Community Member Posts: 1,195 ♦ Idol ♦
As far as I found out the problem results from line 3 in the function:

function calc(op)
{ if(!isNaN(op) || op==”.”)
{ if(!token)
{ if(document.calculator.win.value == 0)
{ document.calculator.win.value = op; }

It says that the value in the calculator is replaced by the entered value if the current value in the calculator is equal to "0". That is made to replace the initial "0". If you enter ".0" the value is still equal to "0" so it's replaced by the next number you enter that is not a "0".

Modify the bold line to be:

`{ if(document.calculator.win.value == 0 && document.calculator.win.value.indexOf(".0") == -1 )`

Now it will replace the value only if it's equal to "0" AND does not contain ".0".

Tim

• Community Member Posts: 916
You might wanna use the .toFixed(2) quotation.``` ```

for example:

var myCalculatedNumber = 12.3450679;

var myFixedNumber = myCalculatedNumber.toFixed(2);

this will give: 12.34

So toFixed , fixes the number to a 2 decimal value

Regards,

Math
• Community Member Posts: 389 ♪ Opening Act ♪
OMG - Thank you!!! Would it be inappropriate if I told you that I love you?

Seriously - you've just made my day!

(I thought that line might be the problem, but had no idea how to fix it.)
• Community Member Posts: 389 ♪ Opening Act ♪
Thanks, Math - it's not a bad idea, but I don't think I'm going to mess with that at this point (although I might consider it for my next course). Really appreciate your taking the time to respond and offer that info!
• Community Member Posts: 1,466 ☆ Roadie ☆
I think Tim's solution should work.
P.S. I must say that whoever wrote the original code shouldn't be posting tutorials on the internet.