ComparisonOperators
In this exercise, you will create a script that uses comparison
operators.
1. Create a new document in your text editor.
2. Type the <!DOCTYPE> declaration, <html> element, header
information, and the <body> element. Use the strict DTD and
“Comparison Operators” as the content of the <title> element.
3. Add the following <h1> element to the document body:
<h1>Comparison Operators</h1>
4. Add the following script section and paragraph elements to
the document body:
<script type="text/javascript">
/* <![CDATA[ */
/* ]]> */
</script>
5. Add the following statements to the script section that perform
various comparison operations on two variables. Notice
that the fi rst comparison is performed using the conditional
operator.
var conditionalValue;
var value1 = "Don";
var value2 = "Dave";
value1 == value2 ? document.write(
"<p>value1 equal to value2: true<br />")
: document.write(
"<p>value1 equal to value2: false<br />");
value1 = 37;
value2 = 26;
conditionalValue = value1 == value2;
document.write("value1 equal to value2: "
+ conditionalValue + "<br />");
conditionalValue = value1 != value2;
document.write("value1 not equal to value2: "
+ conditionalValue + "<br />");
conditionalValue = value1 > value2;
document.write("value1 greater than value2: "
+ conditionalValue + "<br />");
conditionalValue = value1 < value2;
document.write("value1 less than value2: "
+ conditionalValue + "<br />");
conditionalValue = value1 >= value2;
document.write("value1 greater than or equal to value2: "
+ conditionalValue + "<br />");
conditionalValue = value1 <= value2;
document.write("value1 less than or equal to value2: "
+ conditionalValue + "<br />");
value1 = 21;
value2 = 21;
conditionalValue = value1 === value2;
document.write(
"value1 equal to value2 AND the same data type: "
+ conditionalValue + "<br />");
conditionalValue = value1 !== value2;
document.write(
"value1 not equal to value2 AND not the same data type: "
+ conditionalValue + "</p>");
6. Save the document as ComparisonOperators.html in the
Exercises folder for Chapter 2.
7. Use the W3C Markup Validation Service to validate the
ComparisonOperators.html document and fix any errors
that the document contains. Once the document is valid,
close it in your text editor and then open it in your Web
browser and examine how the elements are rendered.
8. Close your Web browser window.
12 years ago
Purchase the answer to view it

- comparisonoperators.zip