Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>
   <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css">
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <link rel="stylesheet" type="text/css" href="style_calc.css">

        <title>Calculator</title>
    </head>

    <body style="background-color:#F781F3">
    <div class="calcBorder">
		<table>
			<tr>
				<th colspan="4" id="display"></th>
			</tr>
			<tr>
				<td id="AC">AC</td>
				<td id="CE">CE</td>
				<td id="%">%</td>
				<td id="/">/</td>
				
			</tr>
			<tr>
				<td id="1">1</td>
				<td id="2">2</td>
				<td id="3">3</td>
				<td id="X">X</td>
			</tr>
			<tr>
				<td id="4">4</td>
				<td id="5">5</td>
				<td id="6">6</td>
				<td id="/">-</td>
			</tr>
			<tr>
				<td id="7">7</td>
				<td id="8">8</td>
				<td id="9">9</td>
				<td id="+">+</td>
			</tr>
			<tr>
				<td id="dot">.</td>
				<td id="0">0</td>
				<td id="ans">ANS</td>
				<td id="equals">=</td>
			</tr>	
			
			
		</table>
	</div> 

        <!-- jQuery library -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <!-- Latest compiled JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
        <script type="text/javascript" src="javascript_calc.js"></script>

    </body>
</html>


   
              
            
!

CSS

              
                .calcBorder{
	margin-left:500px;
	margin-top:100px;
	width:360px;
	/*background-color: lightblue;*/
	border-radius:8px;

}
body{
	background-color:#F781F3;
	
}

	
}
table{
	border:4px soild black;
}
th, td {
   border: 4px solid black;
   
}
tr th{
	height:100px;
	background-color:white;
	text-align:right;
	padding-right:10px;
	
}


td{
	width:90px;
	text-align:center;
	font-size:20px;
	font-family:cursive;
	background-color:#819FF7;
	font-style:bold;
}
td:hover{
	background-color:#FFBF00;
	
	
}
tr{
	height:80px;;	
}
th{
	
	text-align:center;
	width:100%;
	height:100%;
	font-size:20px;
	font-family:cursive;
}
              
            
!

JS

              
                $(document).ready(function(){
/*	var num1=0;			//// Attempt to find float operation's result
	num1=2;
	
	var num2=0;
	num2=0.12345;	
	var result=0;
	result=num1+num2;
	
	alert(result);
	*/
	var value="";
	var arr1=[];
	var arrNum1=[];
	var operator="";
	var result=0;
	var flag=0;							//to avoid 
	//var count=0;
	var flagInvalidOperator=0;			//to avoid invalid values(invalid '=' after operator)
	var countNum=0;						////to avoid invalid values(invalid '=' after single number)
	//var temp=[];			defined locally
	
	
	/**********NO USE OF FLAG VARIABLE, TRY TO ELIMINATE IT********************/
	$('td').click(function(){
		
		$('#display').empty();
		
		value=$(this).text();		// collecting values from html			
		
		
		var check=parseInt(value);		// converting collected (string) values to integer
		if(check>=0 || check<=9 || value=='.')
		{	
			if(flag==1)
			{
				$('this').off('click');
                $('#display').append("<p>"+result+"</p>");
			}
			else
				displayNum(value);
				flagInvalidOperator=0;
				//flagInvalidNum=1;
				countNum+=1;
				console.log("CountNUM: "+countNum);
		}
	
		else if(value=='+' || value=='-' || value=='X' || value=='/' || value=='%')
		{
			if(flagInvalidOperator==1)
			{
				$('this').off('click');
                $('#display').append("<p>"+arrNum1+"</p>");
				return;
			} 
			else 
			displayOperator(value);
			flag=0;
			flagInvalidOperator=1;
			//flagInvalidNum=0;
								
		}
		else if(value=='=')
		{
			if(flagInvalidOperator==1)
			{
				$('this').off('click');
				$('#display').append("<p>"+arrNum1+"</p>");			
				
			}
			else if(countNum==1)
			{
				$('this').off('click');
				$('#display').append("<p>"+arrNum1+"</p>");			
			}
			else
			{
				equals();
				flag=1;
			}
		}
		else if(value=='CE')
		{
			 CE();// to display blank
		}
		else if(value=='AC')
		{
			AC();
		}
		else if(value=='ANS')
		{
			if(flagInvalidOperator==1)
			{
				$('this').off('click');
				$('#display').append("<p>"+arrNum1+"</p>");			
				
			}
			else if(countNum==1)
			{
				$('this').off('click');
				$('#display').append("<p>"+arrNum1+"</p>");			
			}
			else
			{
				equals();
				flag=1;
			}
		}
	});
		function displayOperator(value)
		{
			console.log("Before operator arr1: "+arr1);
			console.log("Before operator arrNum1: "+arrNum1);
			arr1.push(value);
			arrNum1=arr1.join('');
			$('#display').append("<p>"+arrNum1+"</p>");
			
			operator=value;
			
		}
		
		function displayNum(value)
		{
			arr1.push(value);
			arrNum1=arr1.join('');	//to display together
			
			
			//console.log(arrNum1);		
			
			$('#display').append("<p>"+arrNum1+"</p>"); 
			//countNum=countNum+1;
			
		}
		function equals()
		{
			
				var num=[];						// to clear num for every cycle- for every successive opertaions.
				//console.log("num: "+num);
				//console.log("arrNum1: "+arrNum1);
				//console.log("arr1: "+arr1);
				num=arrNum1.split(operator);		//to get only numbers and not operators
				//console.log("num"+num);
				switch(operator)			// to perform operation
				{
					
					case '+': result=parseFloat(num[0])+parseFloat(num[1]);
							console.log(result);
							$('#display').empty();
							$('#display').append("<p>"+result+"</p>");
					break;
					case '-': result=parseFloat(num[0])-parseFloat(num[1]);
							console.log(result);
							$('#display').empty();
							$('#display').append("<p>"+result+"</p>");
					break;
					case '/': result=parseFloat(num[0])/parseFloat(num[1]);
							console.log(result);
							$('#display').empty();
							$('#display').append("<p>"+result+"</p>");
					break;
					case 'X': result=parseFloat(num[0])*parseFloat(num[1]);
							console.log(result);
							$('#display').empty();
							$('#display').append("<p>"+result+"</p>");
					break;
					case '%': result=parseFloat(num[0])%parseFloat(num[1]);
							console.log(result);
							$('#display').empty();
							$('#display').append("<p>"+result+"</p>");
					break;
				}
				arrNum1=0;
				console.log("afterResult arrNum1: "+arrNum1);	
				arr1=[];
				console.log("before result arr1: "+arr1);
				arr1.push(result);
				console.log("after result arr1: "+arr1);
				arrNum1=arr1.join('');
				flagInvalidOperator=0;
				//flagInvalidNum=0;
				countNum=1;
			
		}
		function AC()
		{
			flag=0;
			arrNum1=0;
			//console.log(arrNum1);
			arr1=[];
			//console.log(arr1);
			$('#display').empty();
		}
		function CE()
		{
			if(flag==1)
			{
				flag=0;
				arrNum1=0;
				arr1=[];
				//console.log(arr1);
				$('#display').empty();
			}
			else
			{
				arr1.pop();
				arrNum1=arr1.join('');
				$('#display').append("<p>"+arrNum1+"</p>");
			}
			
		}
	
});


              
            
!
999px

Console