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
    Dec 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    problems with switch statement ...

    Hi, im new to this so i was wondering if anyone could tell me where im going wrong? im trying to get my switch statement to work and give me a specific reply for 4 names and if one of these names isnt typed into the promt box then i want an alert box to appear but i cant seem to get it to work. any ideas?




    <body>
    <script type="text/javascript">
    //<![CDATA[
    var Card_Suit;
    Card_Suit=window.prompt("Think of a card suit? What suit are you thinking of?","????");
    //]]>
    </script>
    <h1 style='color: blue'>Your Future In The Cards</h1>
    <script type="text/javascript">
    //<![CDATA[
    switch(Card_Suit) {
    case "clubs": {document.write("<p style='color: black'>");
    document.write("Beware of three legged dogs crossing your path</p>");
    }
    break;
    case "spades": {document.write("<p style='color: black'>");
    document.write("Wear brown with pink spots for luck</p>");
    }
    break
    case "hearts": {document.write("<p style='color: red'>");
    document.write("You are going to meet a stranger with two heads</p>");
    }
    break;
    case "diamonds": {document.write("<p style='color: red'>");
    document.write("Your lucky number for today is 13,254,297</p>");
    }
    break;
    }
    else
    window.alert("this is not a card");
    }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb Consider this ...

    Couple of problems:
    1. Cannot write to the page after it has been rendered.
    2. You did not complete the tag pairs
    3. Switch statement does not end in an 'else' part
    4. If the user does not enter an exact match (club vs. clubs), they might become frustrated.

    Evaluate following against your code and note the differences.

    Code:
    <html>
    <head>
    <title>Card Suit</title>
    <script type="text/javascript">
    //<![CDATA[
    function Future() {
      var Card_Suit;
      Card_Suit = window.prompt("Think of a card suit? What suit are you thinking of?","????");
    
      var str = '';
      switch(Card_Suit) {
        case "clubs": 
          str = "<p style='color: black'>";
          str+= "Beware of three legged dogs crossing your path</p>";
          break;
        case "spades": 
          str = "<p style='color: black'>";
          str+= "Wear brown with pink spots for luck</p>";
          break
        case "hearts": 
          str = "<p style='color: red'>";
          str+= "You are going to meet a stranger with two heads</p>";
          break;
        case "diamonds": 
          str = "<p style='color: red'>";
          str+= "Your lucky number for today is 13,254,297</p>";
          break;
        default : 
          str = "This is not a card";
          break;
      } 
      document.getElementById('choice').innerHTML = str;
    }
    //]];
    </script>
    </head>
    <body>
    <h1 style='color: blue'>Your Future In The Cards</h1>
    <button onclick="Future()">Play</button>
    <div id="choice"></div>
    </body>
    </html>
    Good Luck!

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Your write quotes are on the wrong side of the end tags, for one.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,086
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question Huh ???

    Quote Originally Posted by mrhoo View Post
    Your write quotes are on the wrong side of the end tags, for one.
    Who ... me or him/her?

  • #5
    Regular Coder
    Join Date
    Mar 2006
    Posts
    728
    Thanks
    35
    Thanked 132 Times in 123 Posts
    Sorry- my mistake! Quotes are all right


  •  

    Posting Permissions

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