Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript calculator 1

Status
Not open for further replies.

greelmo

Programmer
May 16, 2003
89
US
I made a JavaScript calculator that works... you can see it at
however, in order for it to calculate long strings of data (ex: 2+2+2+2+2+2+2) you have to press '=' in between each section (ex: 2+2=...+2=...+2=...etc.)
I'm dreaming up a function to make it so that you don't have to do that, but i'm not totally sure... does anyone have any ideas?

Later,
Greelmo
 
Greelmo,

Well, the link's still bogus on my end, but the solution is simple -- do it the way you would do it as a human brain.

Addition is commutative, right? So...

2 + 2 + 2 + 2 + 2 + 2 + 2

is the same as

2 + (2 + 2 + 2 + 2 + 2 + 2)

which is the same as

2 + (2 + (2 + 2 + 2 + 2 + 2))

and so on until you get to

2 + (2 + (2 + (2 + (2 + (2 + 2)))))

Thus, you remember that Addition is a binary operator.

Recursion -- speaking of the devil -- would be an ideal solution to this.

pseudocode:

[ignore]
function RecursiveAdd(StringToParse)
// look for first "+" operator
// everything to the left of that operator is Addend1
// everything to the right is Addend2
// if Addend2 contains "+" then Addend2 = RecursiveAdd(Addend2)
// return Addend1 + Addend2
[/ignore]

Well, something like that, anyway. It's easy to get confused when we forget that Addition is essentially a binary operator and we try to think of it as an n-operator.

Of course, there's no kind of error correction in this. [smile] I presume that no string enters the function unless it contains only elements of the set ["0","1","2",...,"8","9","+"]

You get to figure out decimals on your own. [lol]

Here's my calculator, in case you feel like trolling through my JavaScript. It specializes in extremely high precision, performing operations of numbers containing hundreds or thousands of digits. Obviously, it slugs a bit when you multiply two 2500-digit numbers, but it seems to produce the correct number. Plus, it has a more useful memory feature than most calculators and actually shows you the two arguments for all binary operators (the handheld calculator has totally tweaked the math world, darn it!). It also lets you perform manipulation of the operators, such as swapping them, or inverting them quickly, rather than using cut-n-paste.

High Precision JavaScript Calculator

Hope that helped!

Cheers,




[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
edward :
nice function.
however it froze my IE when trying to do
1^9000000000000000000000000000.
shouldn't be a conditional saying that if the value is 1 no mather what the exponent is the result is directly 1?

same thing when trying to do 0^1232123.



grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
 
grtfercho, there are lots of little short-cuts programmed in. I forgot to add those.

I should point out that in all likelihood, it didn't "freeze". It simply was multiplying. How fast do you suppose you could perform 9000000000000000000000000000 multiplications? [lol]

The multiplication routine has been simplified such that it won't actually calculate n*1 or n*0 -- it already knows the answers to those. The powers routine uses the multiplication routine (of course), but still, as quickly as it can get through the multiplication routine, it still has to get through it 9000000000000000000000000000 times!

But shortcuts are good.

Thanks!


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
Greelmo,

Have you considered doing your serial addition the same way hand calculators do it? You enter "2", then click "+" and "+" again (and some little indicator lights up to show that it's sequential) and then just click "=", "=", "=" repeatedly to keep adding the last addend?

Cheers,


[monkey] Edward [monkey]

"Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!" -- inventor of the cat door
 
i think i figured out how to do it. Im going to create another hidden input and have the compute run into that after every sign is pushed (save the equal sign). That way, when they press the "=" it will just take the value of the hidden box.

Later,
Greelmo
 
here's my two cents - i built this js calc quite some time ago just to see if i could. you can repeatedly enter commands like "2+3+4+5+6"

<html>
<head>
<title>JavaScript calculator</title>

<script type=&quot;text/javascript&quot;>

if (this.name != &quot;calc&quot;) {
foo = window.open(location.href, &quot;calc&quot;, &quot;height=175, width=175&quot;);
window.opener = self;
window.close();
}

var operator = &quot;&quot;;
var operand = &quot;&quot;;
var opClickedLast = false;

function numClick(args) {

if (opClickedLast) {
// reset the display if an operator was last clicked
display.value = args;
opClickedLast = false;
}
else if (args == '.') {
// make sure there can only be one decimal point
if (display.value.indexOf('.') > 0) {
return false;
}
else {
if (display.value == &quot;0&quot; || display.value == &quot;&quot; || opClickedLast) {
display.value = &quot;0&quot;;
}
display.value += args;
opClickedLast = false;
return true;
}
}
else {
display.value += args;
}
}

function operClick(args) {
if (operator != &quot;&quot;) {
// if there's an operation pending
doMath(operator);
}
operator = args;
operand = display.value;
opClickedLast = true;
}

function doMath(op) {
if (display.value) {
if (display.value == &quot;.&quot;) {return false;}
else {
switch(op) {
case '+':
display.value = eval(operand) + eval(display.value);
break;

case '-':
display.value = eval(operand) - eval(display.value);
break;

case '*':
display.value = eval(operand) * eval(display.value);
break;

case '/':
display.value = eval(operand) / eval(display.value);
break;
}
}
}
}

function doClear(args) {
switch(args) {
case 'c':
display.value = &quot;&quot;;
opClickedLast = false;
operator = &quot;&quot;;
operand = &quot;&quot;;
break;

case 'ce':
display.value = &quot;&quot;;
break;
}
}

</script>

<style type=&quot;text/css&quot;>
body {
background:threedhighlight;
border:none;
}
input.btn {
width:25px;
height:20px;
font:status-bar;
}
table {
background:threedface;
border:2px outset threedhighlight;
border-collapse:collapse;
}
td {
text-align:center;
padding:2px 3px;
}
.btnEquals {
width:65px;
height:20px;
font:status-bar;
}

</style>

</head>
<body>
<table align=&quot;center&quot;>
<tr>
<td colspan=&quot;4&quot; align=&quot;center&quot;>
<input type=&quot;text&quot; readonly name=&quot;display&quot; style=&quot;text-align:right; width:100%;&quot; />
</td>
</tr>
<tr>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;7&quot; onClick=&quot;numClick('7');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;8&quot; onClick=&quot;numClick('8');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;9&quot; onClick=&quot;numClick('9');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;+&quot; onClick=&quot;operClick('+');&quot; /></td>
</tr>
<tr>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;4&quot; onClick=&quot;numClick('4');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;5&quot; onClick=&quot;numClick('5');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;6&quot; onClick=&quot;numClick('6');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;-&quot; onClick=&quot;operClick('-');&quot; /></td>
</tr>
<tr>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;1&quot; onClick=&quot;numClick('1');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;2&quot; onClick=&quot;numClick('2');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;3&quot; onClick=&quot;numClick('3');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;x&quot; onClick=&quot;operClick('*');&quot; /></td>
</tr>
<tr>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;0&quot; onClick=&quot;numClick('0');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;.&quot; onClick=&quot;numClick('.');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;?&quot; onClick=&quot;alert('This is a calculator, Einstein.');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;/&quot; onClick=&quot;operClick('/');&quot; /></td>
</tr>
<tr>
<td colspan=&quot;2&quot;>
<input type=&quot;button&quot; class=&quot;btnEquals&quot; value=&quot;=&quot; onClick=&quot;operClick('=');&quot; />
</td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;C&quot; onClick=&quot;doClear('c');&quot; /></td>
<td><input type=&quot;button&quot; class=&quot;btn&quot; value=&quot;CE&quot; onClick=&quot;doClear('ce');&quot; /></td>
</tr>
</table>
</body>
</html>



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
just a comment on my calc - it doesn't handle order of operations correctly, so 1+2*3 will be 9 instead of 7. [hammer][cry]

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
My kingdom for the ability to use parenthesis!

Well, not actually. [smile]

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
grtfercho: nice function. however it froze my IE when trying to do 1^9000000000000000000000000000.

Try it now! Hey, who says I can't be responsive?

Cheers,



[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top