/    Sign up×
Community /Pin to ProfileBookmark

Using Strings and Arrays

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]}}
}[/CODE]

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>[/CODE]

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.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@HaganeNoKokoroMar 25.2005 — I think the easiest way to accomplish your goal would be to use the eval() function. Example:var answer=eval("(1+2)*3");will correctly evaluate the parentheses
Copy linkTweet thisAlerts:
@CharlesMar 25.2005 — I remember my father's first calculator. It cost $200 back in the 70's and didn't have an "=" key much less parentheses. But they're still making that old style. It turns out that it is actually quicker and easier. Get hold of a HP calculator and become familiar with how you use its stack to do parentheses.
Copy linkTweet thisAlerts:
@ExuroMar 25.2005 — My suggestion would be to use [url=http://www.webreference.com/js/column5/]RegExp[/url] pattern matching to go through and evaluate your expression. An iterative structure would probably work well, and you could go through the string, match pieces that you want to evaluate, replace the piece with the resulting number, and then continue the process until the string is a single number. I hope that helps!
Copy linkTweet thisAlerts:
@ExuroMar 25.2005 — [i]Originally posted by Charles [/i]

[B]Get hold of a HP calculator and become familiar with how you use its stack to do parentheses. [/B][/QUOTE]

We covered Polish Notation and Reverse Polish Notation in both my IB HL Math class and my computer science class. I actually wrote up a pre/post-fix evaluation script in JavaScript to check my answers for math, and then a couple evaluators in C++ as assignments this year using both trees and stacks to evaluate. I actually almost suggested using Reverse Polish Notation because it would make things easier for coding, but we're all used to infix notation... :p
×

Success!

Help @TravisMath 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.19,
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,
)...