Jump to content

Recommended Posts

Posted (edited)

Hey guys,

 

I just finished my basic calculator in HTML/Javascript and I was wondering if anyone would be able to test it out.

 

The coding isn't going to be 100% perfect since I'm fairly new at it and the teacher hasn't taught us all the ways to do things. So right now, I'm mostly looking to see if it works.

 

Valid entries include #operator#, then hit =. Please don't enter like -4*+8. Both numbers can be negative, as well as decimals, but with one operator in between or in the case of a negative (-#*-#) is okay.

 

I attached a notepad file of the code.

 

Thanks!

theADOLESCENT

 

Edit:

I can't find where you guys would see the attached file so I'll use the code snippet tool.

 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!--
*************************************************
*												*
*				Assignment 6 - Exercise 2					
*												*
*	Author:								*
*	Course:				COM SCI 201				*
*	Created On:			11/16/2012					
*	Last Modified On:	11/16/2012				
*												*
*************************************************
-->

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Assignment 6 - Exercise 2</title>
</head>
<body ><center><h1>Assignment 6 - Exercise 2</h1></center><hr style="width:90%"/>
<script type="text/javascript">
var dblNum1, dblNum2, dblResult, intOp, intNum1Over, intY;
intNum1Over = 0;
intY = 0;
intOp = 0;
dblNum1 = 0;
dblNum2 = 0; 
var intNum1Used;
var strNum1, strNum2, strPre;
intNum1Used = -1;
strNum1 = "";
strNum2 = "";

function plus()
{
	with(document.frmCalculator)
	{
		intOp = 1;
		intNum1Used = 0;
	}	
} 

function minus()
{
	if(intOp != 0)
	{
			strNum2 = "-";
	}
		if ((intNum1Over == 1) && (intOp == 0))
		{ //intnum1 = done
			intOp = 2;
			intNum1Used = 0;
		}
		else
		{
			if(intOp == 0)
			{
			strNum1 = "-";
			}
			else
			{
			}
		}

} 

function times()
{
	intOp = 3;
	intNum1Used = 0;
} 

function divide()
{
	intOp = 4;
	intNum1Used = 0;
} 

function percent()
{
	intOp = 5;
	intNum1Used = 0;
}	


function result()
{
	with (document.frmCalculator)
	{	//txaResult.value = strNum1 + "," + intOp + "," + strNum2;

		dblNum1 = parseFloat(strNum1);
		dblNum2 = parseFloat(strNum2);
		if(intOp == 1)
		{	

			dblResult = dblNum1 + dblNum2;
			dblResult = dblResult.toFixed(5);
			txaResult.value = dblResult;
		}	
		else
		{
			if(intOp == 2)
			{
				dblResult = dblNum1 - dblNum2;
				dblResult = dblResult.toFixed(5);
				txaResult.value = dblResult;

			}
			else
			{
				if(intOp == 3)
				{
					dblResult = dblNum1 * dblNum2;
					dblResult = dblResult.toFixed(5);
					txaResult.value = dblResult;

				}
				else
				{
					if(intOp == 4)
					{
						dblResult = dblNum1 / dblNum2;
						dblResult = dblResult.toFixed(5);
						txaResult.value = dblResult;

					}
					else
					{
						if(intOp == 5)
						{	
							dblResult = (dblNum1/dblNum2)*100;
							dblResult = dblResult.toFixed(5);
							txaResult.value = dblResult + "%";
						}
					}
				} 
			} // end if op = 2
		} // end if op = 1 
	} // end with
}	// end result

function zero()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "0";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "0";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function one()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "1";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "1";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function two()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "2";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "2";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function three()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "3";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "3";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function four()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "4";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "4";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function five()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "5";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "5";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function six()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "6";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "6";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function seven()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "7";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "7";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}

function eight()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "8";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 =strNum1 + "8";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}

	}
}

function nine()
{

	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 = strNum2 + "9";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + "9";
			txaResult.value = strNum1;
			intNum1Over = 1;
		}
	}
}


function point()
{
	with(document.frmCalculator)
	{
		if(intNum1Used == 0)
		{
			strNum2 =strNum2 + ".";
			txaResult.value = strNum2;
		}
		else
		{
			strNum1 = strNum1 + ".";
			txaResult.value = strNum1;
		}
	}
}

function reset1()
{
	with(document.frmCalculator)
	{
		strNum1 = "";
		strNum2 = "";
		strPre = "";
		dblNum1 = 0;
		dblNum2 = 0;
		intNum1Used = 1;
		dblResult = 0;
		intNum1Over = 0;
		intOp = 0;
	}
}

</script>

<form name ="frmCalculator" id="frmCalculator" onsubmit="">
<table border="2">
	<tr><td align="center"><textarea name="txaResult" rows="1" cols="45"></textarea></td></tr>	
	<tr><td colspan="1"><input type="reset"  onclick="reset1()" value="  		   Clear 		    "/></td></tr>
	<tr>
		<td>
			<input type="button" value="       7       " onclick="seven()"/>
			<input type="button" value="       8       " onclick="eight()"/>
			<input type="button" value="       9       " onclick="nine()"/>
			<input type="button" value="       +       " onclick="plus()"/>
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="       4       " onclick="four()"/>
			<input type="button" value="       5       " onclick="five()"/>
			<input type="button" value="       6       " onclick="six()"/>
			<input type="button" value="       -       " onclick="minus()"/>
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="       1       " onclick="one()"/>
			<input type="button" value="       2       " onclick="two()"/>
			<input type="button" value="       3       " onclick="three()"/>
			<input type="button" value="       *       " onclick="times()"/>
		</td>
	</tr>
	<tr>
		<td>
			<input type="button" value="       0       " onclick="zero()"/>
			<input type="button" value="       .       " onclick="point()"/>
			<input type="button" value="       =       " onclick="result()"/>
			<input type="button" value="   /   " onclick="divide()"/>
			<input type="button" value="   %   " onclick="percent()"/>
		</td>
	</tr>

</table>
</form>


</body>
</html>

 

Edited by theADOLESCENT

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.