Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    if statement help

    So i don't seem to understand why this code is branching incorrectly, if somebody could tell me why it would be greatly appreciated..

    no matter what combination i put in
    (1-2, yes to AAA yes to room with view member) or
    (1-2, no to AAA, no to room with view

    it always returns 1-2 y y


    Code:
        var hotelGuests;
        var memberTripleA;
        var roomWithView;
        var priceOfRoom;
        
        hotelGuests = prompt("How many people will be in room?\n"
                + "\tMax 6 people");
        hotelGuests = Number(hotelGuests);
        
        memberTripleA = prompt("Are you a member of AAA\n"
                + "\tY for yes\n"
                + "\tN for no");
        memberTripleA = memberTripleA.toUpperCase();
        
        roomWithView = prompt("Would you like a room with a view?\n"
                + "\tY for yes\n"
                + "\tN for no");
        roomWithView = roomWithView.toUpperCase();
        
        if (hotelGuests == 1 || hotelGuests == 2
        && memberTripleA == "Y" && roomWithView == "Y") {
            document.write("1-2 y y");
            
        } else if (hotelGuests == 1 || hotelGuests == 2
        && memberTripleA == "Y" && roomWithView == "N") {
            document.write("1-2 y n");
            
        } else if (hotelGuests == 1 || hotelGuests == 2
        && memberTripleA == "N" && roomWithView == "N") {
            document.write("1-2 n n");
            
        } else if (hotelGuests == 1 || hotelGuests == 2
        && memberTripleA == "N" && roomWithView == "Y") {
            document.write("1-2 n Y");
        } else {
        alert("something went wrong, try agian")
        }

  • #2
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    You are playing with ancient code (document.write), and using prompt/alert, which I guess is okay if all you're doing is learning/experimenting. The more up-to-date way is to use a form and either a modal window for display/messages, or a div/span with an id.

    Also, you're logic for the conditionals is all over the map. All you need (after the prompts) is:

    Code:
    var result = "";
    
    result += hotelGuests + " " + memberTripleA + " " + roomWithView;
    
    alert(result);
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    opdun (03-08-2013)

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,762
    Thanks
    55
    Thanked 517 Times in 514 Posts
    apart from the fact that prompt and document.write are pretty much obsolete, it seems you're making this way more complicated than it need to be. Is there some reason why you don't just do this:
    Code:
    <script type="text/javascript">
        var hotelGuests;
        var memberTripleA;
        var roomWithView;
        var priceOfRoom;
        
        hotelGuests = prompt("How many people will be in room?\n"
                + "\tMax 6 people");
        hotelGuests = Number(hotelGuests);
        
        memberTripleA = prompt("Are you a member of AAA\n"
                + "\tY for yes\n"
                + "\tN for no");
        memberTripleA = memberTripleA.toLowerCase();
        
        roomWithView = prompt("Would you like a room with a view?\n"
                + "\tY for yes\n"
                + "\tN for no");
        roomWithView = roomWithView.toLowerCase();
        
        if (hotelGuests == 1 || hotelGuests == 2
        && memberTripleA == "Y" && roomWithView == "Y") {
            document.write("1-2 "+ memberTripleA +" "+ roomWithView);
        } else {
        alert("something went wrong, try agian")
        }
    </script>
    [EDIT:] aka what WolfShade said, only slower
    Last edited by xelawho; 03-08-2013 at 03:44 PM.

  • Users who have thanked xelawho for this post:

    opdun (03-08-2013)

  • #4
    New to the CF scene
    Join Date
    Mar 2013
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    heh, glad that my school is teaching us "obsolete" ways. This is how they gave us a example for a different problem and while trying to convert it for this one i couldn't get it to branch correctly. We haven't covered those techniques yet that you suggested Wolfshade so xelawho way makes more sense for me, anyways i seem to have figured it out enough that ill get a passing grade. heh

    i appreciate the help. thanks both of you

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by opdun View Post
    heh, glad that my school is teaching us "obsolete" ways.
    You are in good company. Hardly a week goes by here without some poor student seeking help with Javascript which became obsolete when Netscape 3 died.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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