Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-09-2009, 01:00 AM   PM User | #1
JSbeginner1
New to the CF scene

 
Join Date: Dec 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
JSbeginner1 is an unknown quantity at this point
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");
}
JSbeginner1 is offline   Reply With Quote
Old 12-09-2009, 01:30 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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!
jmrker is offline   Reply With Quote
Old 12-09-2009, 01:30 AM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Your write quotes are on the wrong side of the end tags, for one.
mrhoo is offline   Reply With Quote
Old 12-09-2009, 01:37 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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?
jmrker is offline   Reply With Quote
Old 12-09-2009, 02:45 AM   PM User | #5
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
Sorry- my mistake! Quotes are all right
mrhoo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:57 AM.


Advertisement
Log in to turn off these ads.