Results 1 to 2 of 2

Thread: Simple JS Question

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2002
    Location
    Michigan
    Posts
    143

    Unhappy Simple JS Question

    I cant get this to work. Any help!?

    Code:
    <HTML>
    
    <HEAD>
    <TITLE>Web Exercise 2</TITLE>
    <script type="text/javascript" Language="JavaScript">
    <!--
    
    var correctAnswer = new Array(10);
    correctAnswer[0] = "B";
    correctAnswer[1] = "A";
    correctAnswer[2] = "B";
    correctAnswer[3] = "B";
    correctAnswer[4] = "B";
    correctAnswer[5] = "B";
    correctAnswer[6] = "B";
    correctAnswer[7] = "A";
    correctAnswer[8] = "A";
    correctAnswer[9] = "A";
    
    function getSelectValue(webex2,selectname){
    	var theMenu = document[webex2][selectname];
    	var selecteditem = theMenu.selectedIndex;
    	return theMenu.options[selecteditem].value;}
    
    function checkAnswers() {
                    var score = 0;
                    for (i= 1; i<11; i++) {
                    answer = getSelectValue("webex2","q" + i);
    			if (I NEED HELP HERE!!!! == correctAnswer[i]) {
    			       score++;
                   }
    	}
    	        document.webex2.scorefield.value = score;
    }
    
    //-->
    </script>
    </HEAD>
    <BODY bgcolor="white">
    <HR>
    </HR>
    <H1>MIS 428 WEBEX2</H1>
    <form name="webex2">
    <h4>
    1.The width of all data cells in a HTML table must be the same.
    <select name="q1">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    2.The body of a for structure must be enclosed in curly braces ({ and }).
    <select name="q2">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    3.The default case in a switch structure is always executed.
    <select name="q3">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    4. A switch structure begins execution immediately after executing the statements 
    in the body of a case.
    <select name="q4">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    5. When a do/while structure is encountered in a program, its body is guaranteed to 
    execute twice.
    <select name = "q5">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    6. While using the STYLE tags, and describing an attribute, the equal (=) sign means the same thing as a colon (:) sign.    
    <select name = "q6">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    7. The right side of a logical AND (&&) operator is always evaluated regardless of whether 
    the condition on the left side of the operator is true or false.
    <select name = "q7">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    8. All variables declared in the body of a function and the parameters to a function are 
    considered to be local variables by a JavaScript.
    <select name = "q8">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    9. Most functions have a list of parameters that provide the means for communicating 
    information between functions via function calls.
    <select name = "q9">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    10. The fifth element of an array can be accessed with the expression a[ 4 ].
    <select name = "q10">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select><BR></BR>
    <hr><p><input type="button" value="Check Answers" onclick=checkAnswers();></P>
    <p>Your score is:<input type="text" name="scorefield" value=" " size="10"></p>
    </form>
    </BODY>
    </HTML>

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090

    Re: Simple JS Question

    This should work:
    HTML Code:
    <HTML>
    
    <HEAD>
    <TITLE>Web Exercise 2</TITLE>
    <script type="text/javascript" Language="JavaScript">
    <!--
    
    var correctAnswer = new Array(10);
    correctAnswer[0] = "B";
    correctAnswer[1] = "A";
    correctAnswer[2] = "B";
    correctAnswer[3] = "B";
    correctAnswer[4] = "B";
    correctAnswer[5] = "B";
    correctAnswer[6] = "B";
    correctAnswer[7] = "A";
    correctAnswer[8] = "A";
    correctAnswer[9] = "A";
    
    function getSelectValue(webex2,selectname){
    	var theMenu = document[webex2][selectname];
    	var selecteditem = theMenu.selectedIndex;
    	return theMenu.options[selecteditem].value;}
    
    function checkAnswers() {
                    var score = 0;
                    for (i= 0; i<correctAnswer.length; i++) {
                    answer = getSelectValue("webex2","q" + (i));
    			if (answer == correctAnswer[i]) {
    			       score++;
                   }
    	}
    	        document.webex2.scorefield.value = score;
    }
    
    //-->
    </script>
    </HEAD>
    <BODY bgcolor="white">
    <HR>
    </HR>
    <H1>MIS 428 WEBEX2</H1>
    <form name="webex2">
    <h4>
    1.The width of all data cells in a HTML table must be the same.
    <select name="q0">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    2.The body of a for structure must be enclosed in curly braces ({ and }).
    <select name="q1">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    3.The default case in a switch structure is always executed.
    <select name="q2">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    4. A switch structure begins execution immediately after executing the statements 
    in the body of a case.
    <select name="q3">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    5. When a do/while structure is encountered in a program, its body is guaranteed to 
    execute twice.
    <select name = "q4">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    6. While using the STYLE tags, and describing an attribute, the equal (=) sign means the same thing as a colon (:) sign.    
    <select name = "q5">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    7. The right side of a logical AND (&&) operator is always evaluated regardless of whether 
    the condition on the left side of the operator is true or false.
    <select name = "q6">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    8. All variables declared in the body of a function and the parameters to a function are 
    considered to be local variables by a JavaScript.
    <select name = "q7">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    9. Most functions have a list of parameters that provide the means for communicating 
    information between functions via function calls.
    <select name = "q8">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select>
    <BR></BR>
    10. The fifth element of an array can be accessed with the expression a[ 4 ].
    <select name = "q9">
    <option selected>Select your answer</option>
    <option value="A">True</option>
    <option value="B">False</option>
    </select><BR></BR>
    <hr><p><input type="button" value="Check Answers" onclick=checkAnswers();></P>
    <p>Your score is:<input type="text" name="scorefield" value=" " size="10"></p>
    </form>
    </BODY>
    </HTML>
    I'll say what I edited. I change the 11 at the start of the for loop to correctAnswer.length. I changed the if like you wanted. Lastly, I lowered the name of all the selects by one to make it work better.

    Edit: now grab the code again, earlier it was erasing all the [i ]'s changing them to make my text italic.
    Last edited by Acidic; Feb 15th, 2005 at 09:18 PM.
    Have I helped you? Please Rate my posts.

Posting Permissions

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



Click Here to Expand Forum to Full Width