PDA

View Full Version : Comparison operators help



gamer12223
Sep 27th, 2009, 01:04 AM
Hi everyone, could someone please tell me what I am doing wrong with this. I am doing a exercise in a javascript book and it shows me exactly what to type but I am not getting an output when I preview it. could someone please help me with this, thanks.


<script type="text/javascript">
/* <![CDATA[ */

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>");


/* ]]> */
</script>

ckeyrouz
Sep 27th, 2009, 01:06 AM
You should put your code in the onload event of the window like this:

window.onload= function()
{
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>");

}