Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    3
  • comments
    0
  • views
    242

Calculator

MarklHarrison9

507 views

This is a simple Calculator

cow.png.5459a35579860256721ece486eb5b58d.png

You set up the first variable the operator you want to use and the 2nd variable. After that you hit Try it and it will run though a switch case to check which operator to use and plug in both variables to calculate.

 

switch(y){
	case "+":
	A= parseInt(x) + parseInt(z);
	break;

	case "-":
	A=x-z;
	break;

	case "*":
	A=x*z;
	break;

	case "%":
	A=x%z;
	break;
	
	case "/":
	A=x/z;
	break;

	default:
	A="Please insert a proper operator";
}

It will then change the element demo to the answer.

document.getElementById("demo").innerHTML = A;

See the whole code below

<!DOCTYPE html>
<html>
<body>

<h3>A Simple Cow</h3>

<input type="number" id="myText1" value="0">
<input type="text" id="myText2" value="Operator">
<input type="number" id="myText3" value="0">

<p>simple Cow.</p>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myText1").value;
    var y = document.getElementById("myText2").value;
    var z = document.getElementById("myText3").value;
    var A;


switch(y){
	case "+":
	A= parseInt(x) + parseInt(z);
	break;

	case "-":
	A=x-z;
	break;

	case "*":
	A=x*z;
	break;

	case "%":
	A=x%z;
	break;
	
	case "/":
	A=x/z;
	break;

	default:
	A="Please insert a proper operator";
}
	
    document.getElementById("demo").innerHTML = A;
}



</script>

</body>
</html>

 



0 Comments


Recommended Comments

There are no comments to display.

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
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!