Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Click here to return to the 'Not a trivial fix + it's not a calculator, it's an adding machine' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Not a trivial fix + it's not a calculator, it's an adding machine
Authored by: Gerk on May 25, '05 12:28:19PM
So to everyone that's whining about this, the way it behaves right now it it NOT a calculator, it's an 'adding machine', plain and simple. For everyone comparing it to $5 calculators .. they are not calculators either then, they are also adding machines. The cost of the calculator should not determine whether it obeys the laws of mathematics or not.

So here's their methods for doing the actual work, and as you can see it's not a trivial fix to make it work properly LOL

function add() {
	a = evaluator(a+"+"+x);

function sub() {
	a = evaluator(a+"-"+x);

function mult() {
	a = evaluator(a+"*"+x);

function div() {
	a = evaluator(a+"/"+x);

They need to rewrite a big piece of it (while they are at it they should just replace the whole thing, it's pretty messy). They should be keeping a running 'equation' in the process and not just blindly doing what they are doing above. To work like a real calculator it should evaluate this running equation at each step and report this back to the output display ...

It's not rocket science (or is it? this thread makes me wonder LOL). Let's just say that whoever wrote this 'calculator' widget wouldn't make it onto my development team, paid or not!

[ Reply to This | # ]

Not a trivial fix + it's not a calculator, it's an adding machine
Authored by: Gerk on May 25, '05 12:31:02PM

incidentally is it just me, or is dashboard/widget combo just a security breach waiting to happen? And I don't mean just in the malicious widget front ... I mean you are getting a lot of access here, and for some reason I suspect that there are many things that a crafty coder can take advantage of here . . .

Some may call me paranoid, but ... I prefer to call myself a realist.

[ Reply to This | # ]
Yes it is a trivial fix
Authored by: mcroft on May 25, '05 01:05:04PM
Yes, it's a trivial fix.

I wrote this after work last night. Here you go. It works both ways and has a persistant preference to let you pick which you want.

The reason it's a trivial fix is the same reason that pasting "1+2*3" calculates properly. The basic resolver is eval(display). As long as you build a string, it can resolve it.

[ Reply to This | # ]

Yes it is a trivial fix
Authored by: Gerk on May 25, '05 04:31:38PM

"Trivial fix" and "Easy Fix" are 2 different things ... it was an "easy fix" because you did it correctly by keeping a running tally of the equation. I don't consider a 400 line diff trivial considering it was a 540 line javascript to start with ;) hehe .. it's all relative!

[ Reply to This | # ]
OK, it's an easy fix
Authored by: mcroft on May 25, '05 05:05:20PM

Yeah, well. Calculator.js was as small as it was before because it didn't have an info button and backside and those require a lot of new functions to be copied and pasted from the documentation.

Anyway, the processing is easier than the processing that's being done to provide immediate evaluation.

The sub/add/mult/div functions are way too repetitive, as well. If I find any reason to touch it again, I'll fix that.

[ Reply to This | # ]
More on why it's a trivial fix
Authored by: mcroft on May 25, '05 01:23:37PM
The key here isn't to change the add/sub/mult/div methods, but to intercept the keydown and add it to the display string.

This is how I handled the add key in the variant I posted. deferredEvaluation is a variable I set at startup to tell me which file to process. OpList is a list of operators that can't be entered twice (no "9++4"). The final "else" is the prior "case add" code to call function add if deferredEvaluation is false.

It took me more time to work through making and saving a preference, a back, flipping the app, etc., than it did to update the JavaScript.

var deferredEvaluation = true;
var OpList="^!()+-*/";
case "add":
if (deferredEvaluation) {		
    if (!display.length) {
    if (OpList.indexOf(display.substr(display.length-1,1)) != -1 ) {
        display = display.substr(0,display.length-1)
   if (display.length < 9)
        display = display + "+";
} else {
    if (a && !isResult && !resetDisplay) {
    } else {
        a = evaluator(display);
        isResult = false;
    currentOp = buttonTitle;
    hiliteOp = buttonTitle;
    resetDisplay = true;

[ Reply to This | # ]