In this project, you will modify a nested if statement so it instead uses a compound 
conditional expression. You use logical operators such as the || (OR) and && (AND) 
operators to execute a conditional or looping statement based on nmltiple criteria. 
1. Create a new document in your text editor. 
2. Type the <!DOCTYPE> declaration, <html> element, docmnent head, and 
<body> element. Use the Strict DTD and "Gas Prices" as the content of tile 
<title> element. 
3. Create a script section in the document head that includes the following variable 
declaration and nested if statement: 
<script type="text/javascript" > 
<!-- HIDE FROM INCOMPATIBLE BROWSERS 
var gasPrice = 1.57; 
if (gasPrice > 1) { 
if (gasPrice < 2) 
document.write("<p>Gas prices are between 
$1.00 and $2.00.</p>"); 

// STOP HIDING FROM INCOMPATIBLE BROWSERS --> 
</script> 
4. Modify the nested if statement you created in the previous step so it uses a single if statement with a compound conditional expression. You will need to use 
the && (AND) logical operator. 
5. Save the document as GasPrices.htm in the Projects folder for Chapter3. 
6. Open the GasPrices.htm document in your Web browser and see if the compound conditional expression works. 
7. Close your Web browser window. 

解决方案 »

  1.   

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>Gas Prices</title>
    <script language="javascript">var gasPrice = 1.57; 
    if (gasPrice > 1&&gasPrice <2) { 
    document.write("<P>Gas prices are between $1.00 and $2.00.</P>"); 
    } </script> 
    <script language="javascript">var gasPrice = 1.57; 
    if (gasPrice > 1) { 
    if (gasPrice <2){
    document.write("<P>Gas prices are between $1.00 and $2.00.</P>"); 

    }
    </script></head><body>
    </body>
    </html>