SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast fullphaser's Avatar
    Join Date
    Jun 2006
    Location
    Chattanooga TN
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    != logic with if statements

    alright I had always thought that != meant that it was not equal to
    you see in this very simple code
    Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--
    color = prompt("What color do you like","red or blue?"); 
    sentance = "you like " + color;
    addon = "? the monkey thinks you're cool";	
    if ((color == "red") || (color == "blue")) {
    	color = "green";
    	addon = "? thats not a color but ok";
    }
    sentances = sentance + addon; 
    //-->
    </script>
    </head>
    <body>
    <script type="text/javascript">
    <!--
    document.writeln(sentances.fontcolor(color)); 
    //-->
    </script>
    the == works, and whenever it is red or blue, it switches it over to green, which would be great but that's not what I need from JS
    I was trying something like this
    Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--
    color = prompt("What color do you like","red or blue?"); 
    sentance = "you like " + color;
    addon = "? the monkey thinks you're cool";	
    if ((color != "red") || (color != "blue")) {
    	color = "green";
    	addon = "? thats not a color but ok";
    }
    sentances = sentance + addon; 
    //-->
    </script>
    </head>
    <body>
    <script type="text/javascript">
    <!--
    document.writeln(sentances.fontcolor(color)); 
    //-->
    </script>
    which should tell the browser (in theory) that whenever it is not equal to those two values to go ahead with the if statment, yet even if the value is red or blue it goes through the statement
    Phaser Labs
    Looking For Affiliates, and members always

  2. #2
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think about the logic for a second:

    if ((color != "red") || (color != "blue")) {

    I enter in blue, "Is the color not red?" Yes, I've found a true value, so continue the statement.

    I enter in red. "Is the color not red", no. Is the color not blue" Yes, I found a true value, continue.

    The problem has nothing to do with the operator, it has to do with the logic. What you want is &&, not ||

  3. #3
    SitePoint Enthusiast fullphaser's Avatar
    Join Date
    Jun 2006
    Location
    Chattanooga TN
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, that does make sence now that I think about it because ah heh, sorry about that, the thought never even occured to me that if it wasn't one then the other would have to be true thanks for the logic update
    Phaser Labs
    Looking For Affiliates, and members always


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •