hi I have an assignment for class where we are supposed to create a drop down list and when a selection is made from the list, execute a function. The function will decide if which item was selected in the drop down list and will change the image to that respective image and display a unique pop up alert message.
So far I got it so when you click on any of the list, the first decision where the first image(which is already there when the page loads) displays and the first alert message comes up, this happens when you select any of the options on the list. I got to figure out how to get the other selections on the list to work, is there something wrong with my global variable? Here is my code-
<!DOCTYPE html>
<html>
<head>
<title>Homework #4 Switch Sun Guy</title>
<script type="text/javascript">
var curSun= 0;
function changeSun(decision) {
// Clear the scene message
var message = "";
switch (curSun) {
case 0:
curSun = 0;
message = "Please make a selection or go back to bed.";
break;
case 1:
if (decision == 1) {
curSun = 1;
message = "I'm glad you are happy.";
}
break;
case 2:
if (decision == 2) {
curSun = 2;
message = "I'm sorry you are sad.";
}
break;
case 3:
if (decision == 3) {
curSun = 3;
message = "It's great you are feeling cool.";
}
break;
case 4:
if (decision == 4) {
curSun = 4;
message = "I hope you get past that soon!";
}
break;
}
The selectedIndex property means you don't have to hard code choices into a switch-case, so you can lose it in favour of arranging your messages as an array, which can be indexed by the parameter sent to the function. The parameter can also be used to build your image filename.
Where used, return should be executed unconditionally and always as the last statement in the function.
That's my signature, it's not part of the damn post!
Thanks! that is a lot of great information and I'm appreciative of your response. The assignment asks that we use a switch construct though so is that code applicable to the code using a switch construct?
Bookmarks