I'm trying to make a basic online javascript calculator. I've got the css/html down, and I've created my own images in paint! Only problem is that, when you click the calculate button it says "NaN" for the result for everything i try, and when I do the delete last character image button, it changes the document.getElementById("numbers").innerHTML value to "NaN" as well.
I've tried everything but I just don't get why this is happening..
code:
calculator.html
<html>
<head>
<title>Online Calculator - Please refresh the page before you calculate something new</title>
<script type="text/javascript">
//<!--
function addSomething(something){ /* Currently working */
var numField = document.getElementById("numbers");
numField.innerHTML = numField.innerHTML + something;
}
function clearLastChar(){ /* Currently not working */
var numField = document.getElementById("numbers");
if (numField !== null){
var value = numField.innerHTML;
var valueList = value.split(" ");
numField.innerHTML = numField.innerHTML - valueList[valueList.length-2]; //should work because all its doing is minusing the last arrays value from the innerHTML
} else {
return;
}
}
function clear(){ /* Currently working */
var numField = document.getElementById("numbers").innerHTML = "";
var answerField = document.getElementById("answer").innerHTML = "";
}
function calculate(){ /* Currently not working */
var numField = document.getElementById("numbers");
var itsValue = numField.innerHTML;
var useableValue = itsValue.replace(/ /,""); //convert the clearLastChar()-form string to one without spaces so we can calculate
var answerField = document.getElementById("answer");
var result = useableValue * 1; //convert innerHTML to int? - not working because its an array
answerField.innerHTML = result;
}
//-->
</script>
<style type="text/css">
body { margin: auto; }
#main_wrapper {
border: 2px solid black;
width: 15em;
height: 25em;
margin: 10px 15px 10px; /* space between wrapper and images */
}
#numbers_area {
}
#number_area_text { font-family: sans-serif; }
#numbers {
font-size: 130%;
}
a { text-decoration: none; }
img { border: none; }
#answer { font-size: 130%; }
</style>
</head>
<body>
<center>
<div id="main_wrapper">
<div id="numbers_area">
<h5 id="number_area_text">Do: </h5><span id="numbers">3 + 2 / 1</span>
</div>
<div id="buttons_area">
<a href="javascript:addSomething('7 ')"><img src="http://i43.tinypic.com/1675eub.png" alt="" /></a><a href="javascript:addSomething('8 ')"><img src="http://www.freeimagehosting.net/uploads/7038538ae4.png" alt="" /></a><a href="javascript:addSomething('9 ')"><img src="http://www.freeimagehosting.net/uploads/4df713eaa2.png" alt="" /></a><a href="javascript:addSomething('/ ')"><img src="http://www.freeimagehosting.net/uploads/4eae532216.png" alt="Divide" /></a><br />
<a href="javascript:addSomething('4 ')"><img src="http://i44.tinypic.com/2vs4zsh.png" alt="" /></a><a href="javascript:addSomething('5 ')"><img src="http://i43.tinypic.com/213kswj.png" alt="" /></a><a href="javascript:addSomething('6 ')"><img src="http://i40.tinypic.com/2ms3fwk.png" alt="" /></a><a href="javascript:addSomething('* ')"><img src="http://i42.tinypic.com/or42ux.png" alt="Multiply" /></a><br />
<a href="javascript:addSomething('1 ')"><img src="http://i40.tinypic.com/bja4pg.png" alt="" /></a><a href="javascript:addSomething('2 ')"><img src="http://i40.tinypic.com/2cmkg8n.png" alt="" /></a><a href="javascript:addSomething('3 ')"><img src="http://i42.tinypic.com/21bodif.png" alt="" /></a><a href="javascript:addSomething('- ')"><img src="http://i40.tinypic.com/eu2dzq.png" alt="Minus" /></a><br />
<a href="javascript:addSomething('0 ')"><img src="http://i44.tinypic.com/103ip6o.png" alt="" /></a><a href="javascript:clear();"><img src="http://i40.tinypic.com/155p9js.png" alt="Clear" /></a><a href="javascript:addSomething('. ')"><img src="http://i41.tinypic.com/nf5ow9.png" alt="decimal point" /></a><a href="javascript:addSomething('+ ')"><img src="http://i41.tinypic.com/fcr6dd.png" alt="Add" /></a><a href="javascript:clearLastChar();"><img src="http://i43.tinypic.com/mb7kb6.png" alt="Clear last number" /></a>
<br /><input type="button" value=" Calculate " onclick="javascript:calculate();" />
<div id="answer_area">
<span id="answer"></span>
</div>
</div>
</div>
</center>
</body>
</html>