i made a simple calculator but i need a calculator that compute using MDAS, hmm. in a textbox i need to compute multiple number and multiple operation and it will echo the computation?
MDAS calculator
Collapse
X
-
Tags: None
-
this is my reference but i need to compute like thisCode:<script language="javascript"> var av,first,sc; function displayone() { var e; e=document.forms[0].one.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displaytwo() { var e; e=document.forms[0].two.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displaythree() { var e; e=document.forms[0].three.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displayfour() { var e; e=document.forms[0].four.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displayfive() { var e; e=document.forms[0].five.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displaysix() { var e; e=document.forms[0].six.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displayseven() { var e; e=document.forms[0].seven.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displayeight() { var e; e=document.forms[0].eight.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displaynine() { var e; e=document.forms[0].nine.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function displayzero() { e=document.forms[0].zero.value; document.forms[0].dis.value=document.forms[0].dis.value+e; } function add() { first=parseInt(document.forms[0].dis.value); document.forms[0].dis.value="" av=document.forms[0].plus.value; return av; return first; } function m() { first=parseInt(document.forms[0].dis.value); document.forms[0].dis.value="" av=document.forms[0].minus.value; return av; return first; } function mu() { first=parseInt(document.forms[0].dis.value); document.forms[0].dis.value="" av=document.forms[0].mult.value; return av; return first; } function di() { first=parseInt(document.forms[0].dis.value); document.forms[0].dis.value="" av=document.forms[0].div.value; return av; return first; } function cal() { if(av=="+"){ sc=parseInt(document.forms[0].dis.value); document.forms[0].dis.value=first+sc; } if(av=="-"){ sc=parseInt(document.forms[0].dis.value); document.forms[0].dis.value=first-sc; } if(av=="*"){ sc=parseInt(document.forms[0].dis.value); document.forms[0].dis.value=first*sc; } if(av=="/"){ sc=parseInt(document.forms[0].dis.value); document.forms[0].dis.value=first/sc; } } function ceo() { document.forms[0].dis.value=""; } </script>
Comment
-
as far as i can see from the code you posted, at the moment you have some form-elements that you use to claculate something. note that you cannot return multiple from a function like this:
this function never returns first since it already exits by returning av.Code:function di() { first=parseInt(document.forms[0].dis.value); document.forms[0].dis.value="" av=document.forms[0].div.value; return av; return first; }
btw. what is really needed? just computing the input like:
or even display the steps from the calculation? getting the steps make it much more complex.Code:20-8+3*4/2
Comment
-
-
then its easy - you might use something like this:
be aware that this will evaluate everything that you put into the first textfield (you could for example type in alert('foo')), so you probably want some input-validation so that only numbers and operators and may be brackets could be typed in.Code:<html> <head> <script type="text/javascript"> function calc() { var term = document.getElementById('term').value; var result = eval(term); document.getElementById('result').value = result; } </script> </head> <body> <input type="text" id="term"/> <input type="button" value="calculate" onclick="calc();"/> <br/> <input type="text" id="result"/> </body> </html>Last edited by gits; Jul 17 '12, 02:20 PM.Comment
-
thanks, but its not follow the MDAS(multiply divide add subtract ) procedure, it's read the first operation you put and then read the followings i wander how to follow the procedure, btw sorry about my english .Comment
-
sir thank i did it, thank for your help thank you so mach . im just a novice in this but i try my best to gadder information.Comment
-
it calculates the term mathematicly correct (since it has to - otherwise the language would be completly messed up). i'm not aware what MDAS should mean - from my point of view it just means that multiplication/division is to be calculated before addition/subtraction and may be only those operations are allowed at all. but i might be wrong here - since i don't know that abbreviation (MDAS) and cannot really find something that really defines it - the best explaination i found is mentioned above.thanks, but its not follow the MDAS(multiply divide add subtract ) procedureLast edited by gits; Jul 18 '12, 01:59 PM.Comment
Comment