/    Sign up×
Community /Pin to ProfileBookmark

JavaScript Calculator

I was playing around building a JavaScript calculator, but am having a weird problem. If you end up with a negative number from doing a calculation, the JavaScript does not use it properly. Anyone got any ideas why?

[CODE]
<html><head>
<style type=”text/css”>
<!–
input.buttons {
height:28px;width:28px; margin:2px;
}
div#set2{
position:absolute;
left:150px;top:80px;
}
div#set1{
position:absolute;
left:20px;top:80px;}
input.bigbutts {height:28px;}
}
–>
</style>
<script type=”text/javascript”>
recallval= null;
tot=0;
operator=””;
valstore=0;
function addnum(totype) {
numbers=/[0-9.]/;
if(numbers.test(totype)){
if(document.getElementById(‘num1’).value==0)document.getElementById(‘num1’).value=totype;
else document.getElementById(‘num1′).value+=totype
}
}
function calc() {
if(operator==’plus’) tot=valstore+eval(document.getElementById(‘num1′).value);
else if(operator==’minus’) tot=valstore-eval(document.getElementById(‘num1′).value);
else if(operator==’multiply’) tot=valstore*eval(document.getElementById(‘num1′).value);
else if(operator==’divide’) tot=valstore/eval(document.getElementById(‘num1’).value);
else alert(“No answer to display”);
document.getElementById(‘num1’).value=tot;
valstore=eval(document.getElementById(‘num1’).value);
document.getElementById(‘num1’).focus();
}
function operate(symbol){
operator=symbol;
if(valstore!=0)calc();
valstore=eval(document.getElementById(‘num1’).value);
document.getElementById(‘num1’).value=””;
document.getElementById(‘num1’).focus();
}
function sr() {
valstore=null;
document.getElementById(‘num1’).value=Math.sqrt(document.getElementById(‘num1’).value);
valstore=document.getElementById(‘num1’).value;
document.getElementById(‘num1’).focus();
}
function sq() {
valstore=null;
document.getElementById(‘num1’).value=eval(document.getElementById(‘num1’).value)*eval(document.getElementById(‘num1’).value);
valstore=document.getElementById(‘num1’).value;
document.getElementById(‘num1’).focus();
}
function topower() {
valstore=null;
power=eval(prompt(“To the power of…”));
document.getElementById(‘num1’).value=Math.pow(document.getElementById(‘num1’).value,power)
valstore=document.getElementById(‘num1’).value;
document.getElementById(‘num1’).focus();
}
function cube() {
valstore=null;
document.getElementById(‘num1’).value=Math.pow(document.getElementById(‘num1’).value,3)
valstore=document.getElementById(‘num1’).value;
document.getElementById(‘num1’).focus();
}
function generator() {
topnum = eval(prompt(“To what maximum number?”));
valstore=null;
document.getElementById(‘num1’).value=Math.random()*topnum;
valstore=document.getElementById(‘num1’).value;
document.getElementById(‘num1’).focus();}
function solvequad() {
a = eval(prompt(‘What is the value of a?’))
b = eval(prompt(‘What is the value of b?’))
c = eval(prompt(‘What is the value of c?’))
rootpart = (b*b)-(4*a*c)
rootpart = +Math.sqrt(rootpart);
startpart = -b
x = (startpart + rootpart)/(2*a)
secx=(startpart – rootpart)/(2*a)
alert(x +”n”+ secx)
}
function mem() {
recallval = document.getElementById(‘num1’).value
}
function recall() {
if(recallval) document.getElementById(‘num1’).value = recallval;
else alert(‘No data in memory’)
}
function backspace() {
numbers=document.getElementById(‘num1’).value;
lnth=numbers.length-1;
newnum=numbers.substring(0,lnth);
document.getElementById(‘num1’).value=newnum;
}
function changesign() {
numbers=document.getElementById(‘num1’).value;
firstbit = numbers.substring(0,1);
if(firstbit==”-“) valstore=document.getElementById(‘num1’).value=0-eval(numbers);
else valstore=document.getElementById(‘num1’).value = “-” + numbers;
}
function trig(contype) {
var degtype = GetButtonValue(document.degform.degtypes);
if(degtype==”Degrees”) {endegs=document.getElementById(‘num1’).value*Math.PI/180;}
else
endegs=document.getElementById(‘num1’).value;
if(contype==”cosine”)result1=Math.cos(endegs);
if(contype==”sine”)result1=Math.sin(endegs);
if(contype==”tangent”)result1=Math.tan(endegs);
result1=Math.round(result1*1000)/1000;
document.getElementById(‘num1’).value=result1
}
function GetButtonValue(buttons)
{
var i;

for( i=0; i<buttons.length; ++i)
if(buttons[i].checked)
return buttons[i].value;
return -1;
}
</script>
</head><body>
<input type=”text” id=”num1″ size=”40″><br>
<form name=”degform”>
<label for=”degrees”>Degrees</label><input type=”radio” name=”degtypes” value=”Degrees” checked/>
<label for=”degrees”>Radians</label><input type=”radio” name=”degtypes” value=”Radians” />
</form>
<br />
<div id=”set1″>
<input type=”button” class=”buttons” onclick=”document.getElementById(‘num1’).value=”;valstore=0;recallval=null;” value=”CE” /><br />
<input type=”button” class=”buttons” onclick=”mem()” value=”M” /><input type=”button” class=”buttons” onclick=”recall()” value=”MR” /><input type=”button” class=”buttons” onclick=”recallval=null” value=”MC” />
<br />
<input type=”button” class=”buttons” onclick=”topower()” value=”x^y” /><input type=”button” class=”buttons” onclick=”document.getElementById(‘num1’).value=’3.1415926535897932384626433832795′” value=”pi”/><input type=”button” class=”buttons” onclick=”document.getElementById(‘num1’).value=1/eval(document.getElementById(‘num1’).value)” value=”1/x” /><br />
<input style=”font-size:12px” class=”buttons” type=”button” value=”Sin” onclick=”trig(‘sine’)”/><input style=”font-size:12px” class=”buttons” type=”button” value=”Cos” onclick=”trig(‘cosine’)”/><input style=”font-size:12px” class=”buttons” type=”button” value=”Tan” onclick=”trig(‘tangent’)”/><br />
<input type=”button” class=”bigbutts” onclick=”generator()” value=”Rand”>
</div>
<div id=”set2″><input type=”button” class=”buttons” onclick=”backspace()” value=”<-“><input type=”button” class=”buttons” onclick=”document.getElementById(‘num1’).value=”;valstore=0;” value=”C”><br /><input type=”button” class=”buttons” onclick=”addnum(7)” value=”7″/><input type=”button” class=”buttons” onclick=”addnum(8)” value=”8″/><input type=”button” class=”buttons” onclick=”addnum(9)” value=”9″/><input type=”button” class=”buttons” onclick=”operate(‘divide’)” value=”/”><input type=”button” class=”buttons” onclick=”sq()” value=”&#178;”><br />
<input type=”button” class=”buttons” onclick=”addnum(4)” value=”4″/><input type=”button” class=”buttons” onclick=”addnum(5)” value=”5″/><input type=”button” class=”buttons” onclick=”addnum(6)” value=”6″/><input type=”button” class=”buttons” onclick=”operate(‘multiply’)” value=”x”><input type=”button” class=”buttons” onclick=”cube()” value=”&#179;”><br />
<input type=”button” class=”buttons” onclick=”addnum(1)” value=”1″/><input type=”button” class=”buttons” onclick=”addnum(2)” value=”2″/><input type=”button” class=”buttons” onclick=”addnum(3)” value=”3″/><input type=”button” class=”buttons” onclick=”operate(‘minus’)” value=”-“><input type=”button” class=”buttons” onclick=”sr()” value=”&#8730;”><br />
<input type=”button” class=”buttons” onclick=”changesign()” value=”+/-“/><input type=”button” class=”buttons” onclick=”addnum(0)” value=”0″/><input type=”button” class=”buttons” onclick=”addnum(‘.’)” value=”.”/><input type=”button” class=”buttons” onclick=”operate(‘plus’)” value=”+”><input type=”button” class=”buttons” onclick=”calc()” value=”=”><br>
<span style=”text-align:center;”>&nbsp;&nbsp;<input class=”bigbutts” type=”button” onclick=”solvequad()” value=”Solve Quadratic”></span>
</div>
</body></html>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliJun 29.2003 — you might want to re-visit your logic..mainly if you have to do some calculations using a negative number..

it seems like the

function operate(symbol) needs some tweeking..
Copy linkTweet thisAlerts:
@NevermoreauthorJun 30.2003 — Any idea what I need to change?
×

Success!

Help @Nevermore spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...