Click here to Skip to main content
15,789,698 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Since my Teacher is outdated, he asked me to create a traditional calculator, meaning i can't use the eval function.I have created this:

<form name="calculator" >
<input type="textfield" name="x" value="">
<input type="button" value="1" onClick="document.calculator.x.value+='1'">
<input type="button" value="2" onClick="document.calculator.x.value+='2'">
<input type="button" value="3" onClick="document.calculator.x.value+='3'">
<input type="button" value="+" onClick="document.calculator.x.value+='+'">
<input type="button" value="4" onClick="document.calculator.x.value+='4'">
<input type="button" value="5" onClick="document.calculator.x.value+='5'">
<input type="button" value="6" onClick="document.calculator.x.value+='6'">
<input type="button" value="-" onClick="document.calculator.x.value+='-'">
<input type="button" value="7" onClick="document.calculator.x.value+='7'">
<input type="button" value="8" onClick="document.calculator.x.value+='8'">
<input type="button" value="9" onClick="document.calculator.x.value+='9'">
<input type="button" value="*" onClick="document.calculator.x.value+='*'">
<input type="button" value="/" onClick="document.calculator.x.value+='/'">
<input type="button" value="0" onClick="document.calculator.x.value+='0'">
<input type="reset" value="Reset">
<input type="button" value="=" onClick="document.calculator.x.value=eval(document.calculator.x.value)">

he refused this and wanted me to create something like this instead:
<script type="text/javascript">

function Add(x,y){}
function Sub(x,y){}
function Mult(x,y){}
function Div(x,y){}


the problem i have is how can i separate first number, operator and second number? And is it possible to make?
Updated 19-Oct-14 7:36am
Kornfeld Eliyahu Peter 19-Oct-14 12:40pm    
JavaScript split function?
AlaaHalabi 19-Oct-14 12:52pm    
can you help me more on how to use it since i need to recognize the operator
Sergey Alexandrovich Kryukov 19-Oct-14 13:25pm    
It's hard to invent more destructive approach than this assignment. Not only a calculator based on eval is easy to create (it is almost implemented in Javascript itself), but, already having an easy-to-use calculator which can process expressions(!) but still implementing a calculator with buttons is just silly.

If someone tells me that this is just the exercise used to train developers, I'll object: this is a real moral hazard in programming, demonstrating students the emptiness of their work. It's possible to pose much more sensible assignments.

AlaaHalabi 19-Oct-14 15:06pm    
I can't agree more on that, but this is how outdated my university teachers are.I told him that the result is more important than the way it is done, but he disagreed.
Sergey Alexandrovich Kryukov 19-Oct-14 20:44pm    
My sympathy. It would be really good to take lessons from non-outdated teacher. (Frankly, a good teacher, by definition, cannot become outdated.) You can try to compensate it by self-education and having some club of companions interested in real education.

However, I should disagree with "result is more important than the way it is done". Any product should be highly maintainable.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900