Write a script that displays the numbers 1 to 4 on the same line, with each pair of adjacent numbers separated by one space.
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"html://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!-- JavaScript by Eduardo Lauro P. Abad -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Web 2 HW: ch 6.16 pg 231</title>
</head>
<body>
<script type="text/javascript">
var x, y, ans;
var xval, yval;
xval = window.prompt("Please enter your first integer: ", "0");
x = parseInt(xval);
if(isNaN(x))
{
window.alert("You did not enter an integer.\nThis may result in an error.");
}
yval = window.prompt("Please enter your second integer: ", "0");
y = parseInt(yval);
if(isNaN(y))
{
window.alert("You did not enter an integer.\nThis may result in an error.");
}
// Addition
ans = x + y;
document.writeln("<p>Addition: " + x + " +" + y + " = " + ans + "</p>");
// Substration
ans = x - y;
document.writeln("<p>Substration " + x + " - " + y + " = " + ans + "</p>");
// Multiplication
ans = x * y;
document.writeln("<p>Multiplication " + x + " * " + y + " = " + ans + "</p>");
// Division
ans = x / y;
document.writeln("<p>Division " + x + " / " + y + " = " + ans + "</p>");
</script>
<p><a href="javascript: window.location.reload()">Click here to reload the page.</a></p>
</body>
</html>
Please note that this is in no way the best possible way of doing it. I’m only posting this in my blog so that they are not lost in the future unless something happens to my dedicated server. Plus it is a good way to look back at how you used to write code.