3 Replies - 3796 Views - Last Post: 28 February 2009 - 09:53 AM

#1 webdesigngrl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-February 09

Help with "&&" logical operator and if statement

Posted 27 February 2009 - 07:42 PM

The assignment I am having trouble with gives us this code first:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Oil Prices</title>

<script type="text/javascript">
/* <![CDATA[ */
var oilPrice = 52.85;
if (oilPrice > 50) {
	if (oilPrice < 60)
		document.write("<p>Oil prices are between $50.00 and $60.00 a barrel.</p>");
}
/* ]]> */
</script>
</head>

<body>
</body>
</html>




it then says to modify the nested if statement so it uses a single if statement with a compound conditional expression to determine whether oil prices are between 50 and 60 dollars. The book says that we will need to use the && logical operator. So, what I got is:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Oil Prices</title>

<script type="text/javascript">
/* <![CDATA[ */
var oilPrice = 52.85;
if (oilPrice > 50 && <60) 
	 document.write("<p>Oil prices are between $50.00 and $60.00 a barrel.</p>");
}
/* ]]> */
</script>
</head>

<body>
</body>
</html>




Is this right? Am I even on the right track??? Thank you to anyone that helps!

Is This A Good Question/Topic? 0
  • +

Replies To: Help with "&&" logical operator and if statement

#2 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Help with "&&" logical operator and if statement

Posted 27 February 2009 - 07:54 PM

You are on the right track, but in all languages (well, at least all that I have used) you have to re-specify the variable you are checking with each condition. You also need to have an opening bracket if you are going to have a closing bracket. Like so:

if (oilPrice > 50 &&  oilPrice < 60) {


Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#3 webdesigngrl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-February 09

Re: Help with "&&" logical operator and if statement

Posted 27 February 2009 - 08:22 PM

Thank you that worked!
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Help with "&&" logical operator and if statement

Posted 28 February 2009 - 09:53 AM

No problem. Just glad to help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1