I'm trying to make a calculator in javascript. The buttons are already working, but I don't know how to separate the first input from the second input. We're not allowed to use global variables so I can't use the "flag" thing. please help, here's my code so far. thanks!

<html>

	<head>
		<title>JAVASCRIPT
		</title>
		
		<script type = "text/javascript">
		
				function getValue(value){
				
				
					var x = value;
					
					switch(value){
					
						case '+': 
							clearScreen();
							break;
						
						case '-' :
							break;
							
						case '/':
							break;
						
						case '*':
							break;
							
						case '+/-':
							break;
						
						case 'sqrt#':
							break;
						
						case '.':
							break;
						
						case 'C':
							break;
							
						case '=':
							break;
						
						default:
								document.calc.screen.value +=x;
							break;
					}
					
					
					
				}
				
				function clearScreen(button){
					document.calc.screen.value ="";
				}
				
				
				
		</script>
	</head>
	
	<body>
	
		<form name ="calc">
		
			<input type ="text" name ="screen" id="screen" value="1"/>
			<br/>
			<input type ="button" name ="seven" id="sev" value="7" onClick="getValue(7)"/>
			<input type ="button" name ="eight" id="eig" value="8" onClick="getValue(8)"/>
			<input type ="button" name ="nine" id="nin" value="9" onClick="getValue(9)"/>
			<input type ="button" name ="division" id="div" value="/" onClick="getValue(/)"/>
			<input type ="button" name ="claer" id="clr" value="C" onClick="clearScreen(this)"/>
			<br/>
			<input type ="button" name ="four" id="for" value="4" onClick="getValue(4)"/>
			<input type ="button" name ="five" id="fiv" value="5" onClick="getValue(5)"/>
			<input type ="button" name ="six" id="sx" value="6" onClick="getValue(6)"/>
			<input type ="button" name ="multiplication" id="multi" value="*" onClick="getValue('*')"/>
			<input type ="button" name ="squareroot" id="sqrt" value="sqrt#" onClick="getValue('sqrt#')"/>
			<br/>
			<input type ="button" name ="one" id="on" value="1" onClick="getValue(1)"/>
			<input type ="button" name ="two" id="tw" value="2" onClick="getValue(2)"/>
			<input type ="button" name ="three" id="tri" value="3" onClick="getValue(3)"/>
			<input type ="button" name ="subtraction" id="subtract" value="-" onClick="getValue(-)"/>
			<input type ="button" name ="addition" id="add" value="+" onClick="getValue('+')"/>
			<br/>
			<input type ="button" name ="posneg" id="posneg" value="+/-" onClick="getValue(+/-)"/>
			<input type ="button" name ="zero" id="zer" value="0" onClick="getValue(0)"/>
			<input type ="button" name ="period" id="dot" value="." onClick="getValue(.)"/>
			<input type ="button" name ="equal" id="eql" value="=" onClick="getValue(=)"/>
		</form>
	</body>
</html>

Recommended Answers

All 2 Replies

If you cant use global javascript variables than how about using a input element of type hidden. Something like

<input type ="hidden" name ="firstvalue" id="firstvalue" value=""/>
<input type ="hidden" name ="operator" id="operator" value=""/>

So when you click on any operator like '+' than you can store the screen value and the operator clicked there like,

document.calc.firstvalue.value=document.calc.screen.value;
document.calc.operator.value="+";
document.calc.screen.value="";

And when '=' button is clicked you can use the values of elements firstvalue, operator and screen to compute and display the result

thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.