ASDV-WebDev/Semester 1/Assignments/JavaScript/ch03/ex_3_5.html

46 lines
1.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Example 3.5</title>
<script>
function getResult()
{
var x = parseInt(prompt("Enter x"," "));
var y = parseInt(prompt("Enter y"," "));
var add = prompt("Do you want to add the numbers (yes or no)?"," ");
if (add == "yes")
{
var result = x + y;
document.write("<p>I added the numbers " + x + " and " + y + ".</p>");
document.write("<p>The result is " + result + ".</p>");
}
else
{
var subtracty = prompt("Do you want to subtract x from y (yes or no)?"," ");
if (subtracty == "yes")
{
var result = y - x;
document.write("<p>I subtracted " + x + " from " + y + ".</p>");
document.write("<p>The result is " + result + ".</p>");
}
else
{
result = x - y;
document.write("<p>I subtracted " + y + " from " + x + ".</p>");
document.write("<p>The result is " + result + ".</p>");
}
}
}
</script>
</<head>
<body>
<table align ="center" width ="70%"><tr><td>
<h1>&nbsp;</h2>
<h1>Add x and y or Subtract </h1>
<h3>Click the button! </h3>
<p><input type="button" id="numbers" value="Enter your numbers" onclick="getResult();" /></p>
</td></tr></table></body>
</html>