I’m in the process of making a calculator. Right now it works okay. It can add, subtract, multiply, or divide up to three numbers.
But now i was needing to know how to take out certain characters when doing math functions.
This is the code I’m using right now. It’s really messy but it works.
JS File
[CODE]
// Clears the calculator
function Clear() {
document.form1.answer.value = “”;
}
function Calc(m) {
// Writes what you type
intm= parseInt(m);
if (document.form1.answer.value < ” “) {
document.form1.answer.value = m;}
else {document.form1.answer.value = document.form1.answer.value + m};
}
// Calculates
function Calculate () /*1*/{
equation = document.form1.answer.value;
parts = equation.split(” “);
//Divides it into parts
// Checks the first math function
if (parts[1]==”+”){number = parseInt(parts[0]) + parseInt(parts[2]);}
if (parts[1]==”-“){number = parseInt(parts[0]) – parseInt(parts[2]);}
if (parts[1]==”*”){number = parseInt(parts[0]) * parseInt(parts[2]);}
if (parts[1]==”/”){number = parseInt(parts[0]) / parseInt(parts[2]);}
// checks to see if there is a 2nd math function
if (parts[4])/*2*/ {
//Two multiplication or division stops here.
//Checks to see whether to add the 1st two numbers first or the last two first
if(parts[1]==”+” || parts[1]==”-“)/*3*/ {
if(parts[3]==”*” || parts[3]==”/”) /*4*/{
//Clears the number value
number = 0;
if(parts[3]==”*”) {number = parseInt(parts[2]) * parseInt(parts[4])}
if(parts[3]==”/”) {number = parseInt(parts[2]) / parseInt(parts[4])}
if(parts[1]==”+”) {number= number + parseInt(parts[0])}
if(parts[1]==”-“) {number= parseInt(parts[0]) – number}
}/*4*/
// Finishes the number if it calculates left to right
else/*5*/ {
if (parts[3]==”+”){number = number + parseInt(parts[4]);}
if (parts[3]==”-“){number = number – parseInt(parts[4]);}
}/*5*/}/*3*/
else /*6*/{
if(parts[3]==”*”){number = number * parseInt(parts[4]);}
if(parts[3]==”/”) {number = number / parseInt(parts[4]);}
}/*6*/}/*2*/
//Writes the value
document.form1.answer.value = document.form1.answer.value + ” = n” + number}/*1*/
//Gets keys pressed
function Key(e) {
if (e.keyCode) keycode=e.keyCode;
else keycode=e.which;
character = String.fromCharCode(keycode);
//Checks to see whether to add to the function or to equal the function
if (character != “=”) {
if (character == “+”) {Calc(” + “); return true;}
if (character == “-“) {Calc(” – “); return true;}
if (character == “*”) {Calc(” * “); return true;}
if (character == “/”) {Calc(” / “); return true;}
else {Calc(character);};
};
else {Calculate();}
}
// Clears the last thing entered
function BackSpace () {
answer = document.form1.answer.value;
parts = answer.split(“”);
pal = parseInt(parts.length) – 1;
pal2 = pal -1;
document.form1.answer.value = “”;
if (parts[pal] == ” “){
for (i=0; i<pal2; i++){
document.form1.answer.value = document.form1.answer.value + parts[i]}}
else {
for(i=0; i<pal; i++) {
document.form1.answer.value = document.form1.answer.value + parts[i]}}
}
And here’s the HTML file
[CODE]
<html>
<head>
<title>Calculator</title>
<script type=”text/javascript” src=”calculator.js”>
</script>
</head>
<body onKeyPress=”Key(event);”>
<center><h1>Calculator</h1><br /><br />
<form name=”form1″>
<table border=”1″ cellpadding=”0″ style=”border-collapse: collapse; border-width: 0″ bordercolor=”#111111″ width=”15%” id=”AutoNumber1″ height=”115″ cellspacing=”0″>
<tr>
<td width=”100%” height=”35″ style=”border-style: none; border-width: medium” colspan=”3″>
<p align=”center”>
<textarea rows=”2″ cols=”25″ style=”overflow:hidden” readonly=”readonly” name=”answer”></textarea></td>
</tr>
<tr>
<td width=”100%” height=”35″ style=”border-style: none; border-width: medium” colspan=”3″>
<p align=”center”><input type=”button” onClick=”Clear();” value=”Clear”>
<input type=”button” onClick=”BackSpace();” value=”BackSpace”></td>
</tr>
<tr>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” value=” – ” onClick=”Calc(‘ – ‘);”></td>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” value=” + ” onClick=”Calc(‘ + ‘);”></td>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” value=” * ” onClick = “Calc(‘ * ‘);”></td>
<td width = “25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” value=” / ” onClick=”Calc(‘ / ‘);”></td>
</tr>
<tr>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘1’);” value=” 1 “></td>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘2’);” value=” 2 “></td>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘3’);” value=” 3 “></td>
<td width=”25%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘-‘)” value=”+/-“></td>
</tr>
<tr>
<td width=”33%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘4’);” value=” 4 “></td>
<td width=”33%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘5’);” value=” 5 “></td>
<td width=”34%” height=”35″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘6’);” value=” 6 “></td>
</tr>
<tr>
<td width=”33%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘7’);” value=” 7 “></td>
<td width=”33%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘8’);” value=” 8 “></td>
<td width=”34%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘9’);” value=” 9 “></td>
</tr>
<tr>
<td width=”33%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘0’);” value=” 0 “></td>
<td width=”33%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calc(‘.’);” value=” . “></td>
<td width=”34%” height=”36″ style=”border-style: none; border-width: medium”>
<p align=”center”><input type=”button” onClick=”Calculate();” value=” = “></td>
</tr>
</table>
</form>
</center>
</body>
</html>
I was wondering to have it accomplish the task of parenthesis!
I was thinking of something like
howdy = “(5+5)*5”;
if(howdy.indexOf(“( “))
{
And then using a split function to get rid of the parenthesis and get the 3 things inside the parenthesis the 5 the + and the other 5.
I tried splitting strings that have already been splitted. But that didn’t work out at all. If you have any suggestions on how to do this as well as any suggestions to make the script easier to read and work with it would be of great help.
Thank you very much.