...

View Full Version : First Post - Basics of switch statement



Urbanmyth
10-06-2011, 05:05 PM
Hello all,

This is my first post here, so I apologize if I didn't format this correctly. I am trying to figure out where I went wrong in my code. The objective is to display all lines of the song using a switch statement. I thought I had my head wrapped around it, but apparently not. Would anyone mind pointing me in the right direction, or telling me what I did wrong here? I would be greatly appreciative! Thank you for your consideration folks.



<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Excercise 8.11</title>
<script type = "text/javascript">
for (var day = 1; day <=5; day++) {

switch (day) {

case 1: document.write("On the "+day+"st of Christmas, my true love gave to me, A partridge in a pear tree.");

case 2: doucment.write("On the "+day+"th day of Christmas, my true love gave to me, Two turtle doves,<br>");

case 3: document.write("On the "+day+"th day of Christmas, my true love gave to me, Three French hens,<br>");

case 4: document.write("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds,<br>");

case 5: document.write("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings,<br>");
break;
}
}
</script>
</head>


<body>
</body>
</html>

ironboy
10-06-2011, 06:22 PM
You've misspelt document.write once and you didn't grasp the concept that you have to have a break after each case, in order not to continue executing the code in the next case.

Apart from that your code would work, but using document.write is a horrible practice. ;)

So it would be better to do:
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Excercise 8.11</title>
<script type="text/javascript">
onload = function(){
var docWrite = function(text){
var p = document.createElement('p');
p.innerHTML = text;
document.body.appendChild(p);
};
for (var day = 1; day <= 5; day++) {
switch (day) {
case 1:
docWrite("On the "+day+"st day of Christmas, my true love gave to me, A partridge in a pear tree.");
break
case 2:
docWrite("On the "+day+"nd day of Christmas, my true love gave to me, Two turtle doves.");
break
case 3:
docWrite("On the "+day+"th day of Christmas, my true love gave to me, Three French hens.");
break
case 4:
docWrite("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds.");
break
case 5:
docWrite("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings.");
break
};
};
};
</script>
</head>
<body>
</body>
</html>

However, this in not a really a good example of where to use the switch statement either, since it would be far easier to do something like this:
<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Excercise 8.11</title>
<script type="text/javascript">
onload = function(){
var docWrite = function(text){
var p = document.createElement('p');
p.innerHTML = text;
document.body.appendChild(p);
};
var things = [
'A partridge in a pear tree',
'Two turtle doves',
'Three French hens',
'Four calling birds',
'Five golden rings'
];
for (var day = 1; day <= 5; day++) {
docWrite("On the "+day+ (["st", "nd"][day-1] || "th")
+ " day of Christmas, my true love gave to me, " + things[day-1] + ".");
};
};
</script>
</head>
<body>
</body>
</html>

Philip M
10-06-2011, 07:11 PM
document.write is indeed horrible :( but if the DOM methods of display suggested by ironboy are too advanced for you (or your teacher), then you can use another method innerHTML. I understand that the object of the exercise is to use a switch statement - as ironboy says, there are in fact better ways.

Note that the script must be contained in a function called by body onload, as the <div> must exist before the script attempts to use it.


<html xmlns = "http://www.w3.org/1999/xhtml">
<head>
<title>Exercise 8.11</title>

<script type = "text/javascript">
function showSong() {
var message = "";
for (var day = 1; day <=5; day++) {
switch (day) {

case 1: message+=("On the "+day+"st day of Christmas, my true love gave to me, A partridge in a pear tree.<br>"); break;

case 2: message+=("On the "+day+"nd day of Christmas, my true love gave to me, Two turtle doves,<br>"); break;

case 3:message+=("On the "+day+"rd day of Christmas, my true love gave to me, Three French hens,<br>"); break;

case 4: message+=("On the "+day+"th day of Christmas, my true love gave to me, Four calling birds,<br>"); break;

case 5: message+=("On the "+day+"th day of Christmas, my true love gave to me, Five golden rings,<br>"); break;

}
}

document.getElementById("song").innerHTML = message;
}

</script>
</head>

<body onload = "showSong()">

<div id ="song"></div>

</body>
</html>

"If you can't explain it simply, you don't understand it well enough”
“Everything should be as simple as it is, but not simpler.”
- both quotes Albert Einstein (German born American Physicist who developed the special and general theories of relativity. Nobel Prize for Physics in 1921. 1879-1955)

Urbanmyth
10-06-2011, 09:48 PM
You guys are my heroes. Thank you so much, seriously appreciated! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum