...

View Full Version : problems with switch statement ...



JSbeginner1
12-09-2009, 02:00 AM
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");
}

jmrker
12-09-2009, 02:30 AM
Couple of problems: :eek:
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. :thumbsup:



<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!
:)

mrhoo
12-09-2009, 02:30 AM
Your write quotes are on the wrong side of the end tags, for one.

jmrker
12-09-2009, 02:37 AM
Your write quotes are on the wrong side of the end tags, for one.

Who ... me or him/her?

mrhoo
12-09-2009, 03:45 AM
Sorry- my mistake! Quotes are all right



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum