Javascript for a prompt which accepts simple math as a value and then returns the answer

Posted by user1139403 on Stack Overflow See other posts from Stack Overflow or by user1139403
Published on 2012-06-26T09:13:19Z Indexed on 2012/06/26 9:15 UTC
Read the original article Hit count: 195

Filed under:

I have written a snippet of code in javascript, for a prompt to appear when clicked. I want to be able to enter a simple math problem (i.e. 230/2) and have it output the answer, rather than the math problem I just entered. Your help will be much appreciated.

<!DOCTYPE html>
<html>
<body>


<button onclick="myFunction()">Click me</button>

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

<script type="text/javascript">
function myFunction()
{
var x;

var mathProblem=prompt("Enter your math problem","");

if (name!=null)
  {
  x = mathProblem;
  document.getElementById("demo").innerHTML = x;
  }
}
</script>

</body>
</html>

© Stack Overflow or respective owner

Related posts about JavaScript