Problem with a basic JavaScript quiz, please help!

Discussion in 'JavaScript' started by badnmad, Jun 15, 2008.

  1. #1
    hey, I have created a quiz with drop down menu's and if selected right when the button at the bottom of the page is clicked the user should get a result but it isn't working.

    Please check out the source and show me where the mistakes are. I am sure it's a simple mistake but i just can't find it.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml"> <!-- Start html -->
    <head> <!-- Start head -->
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Pseudocode Tutorials</title>
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <link href="../style.css" rel="stylesheet" type="text/css" />
    </head> <!-- End head -->
    <body> <!-- Start body -->

    <div id="content"> <!-- Start content -->
    <div id="header"> <!-- Start header -->

    </div> <!-- End header -->
    <div id="menu"> <!-- Start menu -->
    <ul>
    <li><a href="../index.html" accesskey="1" title="">Home</a></li>
    <li><a href="../tutorials.html" accesskey="2" title="">Tutorials</a></li>
    <li class="active"><a href="../quiz.html" accesskey="3" title="">Quiz</a></li>
    <li><a href="../about.html" accesskey="4" title="about">About Us</a></li>
    </ul>
    </div> <!-- End menu -->

    <div id="colOne"> <!-- Start colOne -->
    <div class="contentHeader"> <!-- Start contentHeader -->
    <h2>Introduction to Pseudocode!</h2>
    </div> <!-- End contentHeader -->

    <script type="Javascript">
    <!--
    var score = 0; // Total of correct answers


    if (document.formone.question1.value = "TRUE"){
    score += 1;
    else
    score = score;
    }
    if (document.formone.question2.value = "TRUE"){
    score += 1;
    else
    score = score;
    }
    if (document.formone.question3.value = "A"){
    score += 1;
    else
    score = score;
    }
    if (document.formone.question4.value = "C"){
    score += 1;
    else
    score = score;
    }
    if (document.formone.question5.value = "B"){
    score += 1;
    else
    score = score;
    }

    function result(){
    document.formone.result.value = score;
    }
    // -->
    </script>

    <form name="formone">

    <!-- Question 1 Start -->
    <p>1. An algorithm is a set of statements or instructions that help to show an answer to a problem.
    </p>

    <p>Please Select True or False:&nbsp;
    <select name="question1">
    <option value=""> - - - - - - </option>
    <option value="TRUE">True</option>
    <option value="FALSE">False</option>
    </select></p>
    <!-- Question 1 End -->


    <!-- Question 2 Start -->
    <p>2. An algorithm can be represented in structured English or pseudocode.
    </p>

    <p>Please Select True or False:
    <select name="question2">
    <option value=""> - - - - - - </option>
    <option value="TRUE">True</option>
    <option value="FALSE">False</option>
    </select></p>
    <!-- Question 2 End -->


    <!-- Question 3 Start -->
    <p>3. The instructions must be placed between what 2 keywords?
    </p>

    <p>Please Select The Correct Answer:
    <select name="question3">
    <option value=""> - - - - - - </option>
    <option value="A">Begin and End</option>
    <option value="B">If and Else</option>
    <option value="C">Repeat and Until</option>
    <option value="D">Start and Finish</option>
    </select></p>
    <!-- Question 3 End -->


    <!-- Question 4 Start -->
    <p>4. What is the Correct way in Pseudocode of the following:
    <br><b>BEGIN</b><br>
    &nbsp;&nbsp;&nbsp;Dial friend's number<br>
    &nbsp;&nbsp;&nbsp;Lift up phone<br>
    &nbsp;&nbsp;&nbsp;Talk to friend<br>
    <b>END</b><br>
    &nbsp;&nbsp;&nbsp;Hang up phone
    </p>

    <p>Please Select The Correct Answer:
    <select name="question4">
    <option value=""> - - - - - - </option>
    <option value="A">BEGIN, Lift up phone, Talk to friend, Dial Friend's number, Hang up phone, END</option>
    <option value="B">Lift up phone, Hang up phone, Talk to friend, BEGIN, END, Dial Friend's number</option>
    <option value="C">BEGIN, Lift up phone, Dial Friend's number, Talk to friend, Hang up phone, END</option>
    <option value="D">BEGIN, Lift up phone, Dial Friend's number, Talk to friend, END, Hang up phone, </option>
    </select></p>
    <!-- Question 4 End -->


    <!-- Question 5 Start -->
    <p>5. What does the keyword BEGIN do?
    </p>

    <p>Please Select The Correct Answer:&nbsp;
    <select name="question5">
    <option value=""> - - - - - - </option>
    <option value="A">Start a loop</option>
    <option value="B">Tell the Pseudocode to start</option>
    <option value="C">Begin an action</option>
    <option value="D">Start a new line</option>
    </select></p>
    <!-- Question 5 End -->


    <center>
    <p>
    <input type="button" value="Score" onclick="Result()" name="Button">

    <input type="text" name="result" size="10">
    </p>
    </center>
    </form>


    <p align="right"><a href="#"><u>Top</u></a></p>
    </div> <!-- End colOne -->

    <div style="clear: both;">&nbsp;</div>
    </div> <!-- End content -->
    <div id="footer"> <!-- Start footer -->
    <p>Copyright &copy; 2008 Pseudocode.</p>
    </div> <!-- End footer -->
    </body> <!-- End body -->
    </html> <!-- End html -->
     
    badnmad, Jun 15, 2008 IP
  2. crath

    crath Well-Known Member

    Messages:
    661
    Likes Received:
    33
    Best Answers:
    0
    Trophy Points:
    100
    #2
    do you think you could post this on a page somewhere, or tell us any errors you get?
     
    crath, Jun 15, 2008 IP
  3. MMJ

    MMJ Guest

    Messages:
    460
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Wow, you had so many errors in your code I think you deserve a prize.

    Also, like crath said, it would be easier for us if you went into more detail about your problem.

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"> <!-- Start html -->
    <head> <!-- Start head -->
    	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
    	<title>Pseudocode Tutorials</title>
    	<meta name="keywords" content="" />
    	<meta name="description" content="" />
    	<link href="../style.css" rel="stylesheet" type="text/css" />
    	<script type="text/javascript">
    	//<![CDATA[
    		var score = 0; // Total of correct answers
    		
    		function showscore(){
    			if (document.formone.question1.value == "TRUE")
    				score += 1;
    			if (document.formone.question2.value == "TRUE")
    				score += 1;
    			if (document.formone.question3.value == "A")
    				score += 1;
    			if (document.formone.question4.value == "C")
    				score += 1;
    			if (document.formone.question5.value == "B")
    				score += 1;
    			document.formone.result.value = score;
    			return;
    		}
    	//]]>
    	</script>
    </head> <!-- End head -->
    <body> <!-- Start body -->
    
    <div id="content"> <!-- Start content -->
    	<div id="header"> <!-- Start header -->
    
    	</div> <!-- End header -->
    	<div id="menu"> <!-- Start menu -->
    		<ul>
    		<li><a href="../index.html" accesskey="1" title="">Home</a></li>
    		<li><a href="../tutorials.html" accesskey="2" title="">Tutorials</a></li>
    		<li class="active"><a href="../quiz.html" accesskey="3" title="">Quiz</a></li>
    		<li><a href="../about.html" accesskey="4" title="about">About Us</a></li>
    		</ul>
    	</div> <!-- End menu -->
    
    	<div id="colOne"> <!-- Start colOne -->
    	<div class="contentHeader"> <!-- Start contentHeader -->
    		<h2>Introduction to Pseudocode!</h2>
    	</div> <!-- End contentHeader -->
    
    
    <form name="formone" action="#" onsubmit="return false;">
    	<!-- Question 1 Start -->
    	<p>1. An algorithm is a set of statements or instructions that help to show an answer to a problem.
    	</p>
    
    	<p>Please Select True or False:&nbsp;
    	<select name="question1">
    	<option value=""> - - - - - - </option>
    	<option value="TRUE">True</option>
    	<option value="FALSE">False</option>
    	</select></p>
    	<!-- Question 1 End -->
    
    
    	<!-- Question 2 Start -->
    	<p>2. An algorithm can be represented in structured English or pseudocode.
    	</p>
    
    	<p>Please Select True or False:
    	<select name="question2">
    	<option value=""> - - - - - - </option>
    	<option value="TRUE">True</option>
    	<option value="FALSE">False</option>
    	</select></p>
    	<!-- Question 2 End -->
    
    
    	<!-- Question 3 Start -->
    	<p>3. The instructions must be placed between what 2 keywords?
    	</p>
    
    	<p>Please Select The Correct Answer:
    	<select name="question3">
    	<option value=""> - - - - - - </option>
    	<option value="A">Begin and End</option>
    	<option value="B">If and Else</option>
    	<option value="C">Repeat and Until</option>
    	<option value="D">Start and Finish</option>
    	</select></p>
    	<!-- Question 3 End -->
    
    
    	<!-- Question 4 Start -->
    	<p>4. What is the Correct way in Pseudocode of the following:
    	<br><b>BEGIN</b><br>
    	&nbsp;&nbsp;&nbsp;Dial friend's number<br>
    	&nbsp;&nbsp;&nbsp;Lift up phone<br>
    	&nbsp;&nbsp;&nbsp;Talk to friend<br>
    	<b>END</b><br>
    	&nbsp;&nbsp;&nbsp;Hang up phone
    	</p>
    
    	<p>Please Select The Correct Answer:
    	<select name="question4">
    	<option value=""> - - - - - - </option>
    	<option value="A">BEGIN, Lift up phone, Talk to friend, Dial Friend's number, Hang up phone, END</option>
    	<option value="B">Lift up phone, Hang up phone, Talk to friend, BEGIN, END, Dial Friend's number</option>
    	<option value="C">BEGIN, Lift up phone, Dial Friend's number, Talk to friend, Hang up phone, END</option>
    	<option value="D">BEGIN, Lift up phone, Dial Friend's number, Talk to friend, END, Hang up phone, </option>
    	</select></p>
    	<!-- Question 4 End -->
    
    
    	<!-- Question 5 Start -->
    	<p>5. What does the keyword BEGIN do?
    	</p>
    
    	<p>Please Select The Correct Answer:&nbsp;
    	<select name="question5">
    	<option value=""> - - - - - - </option>
    	<option value="A">Start a loop</option>
    	<option value="B">Tell the Pseudocode to start</option>
    	<option value="C">Begin an action</option>
    	<option value="D">Start a new line</option>
    	</select></p>
    	<!-- Question 5 End -->
    
    
    	<center>
    	<p>
    	<input type="button" value="Score" onclick="showscore();" name="Button">
    	<input type="text" name="result" size="10">
    	</p>
    	</center>
    </form>
    
    
    <p align="right"><a href="#"><u>Top</u></a></p>
    </div> <!-- End colOne -->
    
    <div style="clear: both;">&nbsp;</div>
    </div> <!-- End content -->
    <div id="footer"> <!-- Start footer -->
    <p>Copyright &copy; 2008 Pseudocode.</p>
    </div> <!-- End footer -->
    </body> <!-- End body -->
    </html> <!-- End html -->
    HTML:
     
    MMJ, Jun 15, 2008 IP
  4. badnmad

    badnmad Peon

    Messages:
    4
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for your replies.

    First off the problem I had was getting the value from the <option> tag. I wanted the code to find out if the correct answer was selected from the drop-down menu.

    This problem was solved after a couple more hours of 'googling'. The solution was rather simple. Here is the finished javascript code:
    <script type="text/javascript"> 
    function calc(){ 
    frm = document.form1; // shorten document.form1
    q1 = frm.question1.options[frm.question1.selectedIndex].value;  // check question1
    q2 = frm.question2.options[frm.question2.selectedIndex].value; 	// check question2
    q3 = frm.question3.options[frm.question3.selectedIndex].value; 	// check question3
    q4 = frm.question4.options[frm.question4.selectedIndex].value; 	// check question4
    q5 = frm.question5.options[frm.question5.selectedIndex].value; 	// check qquestion5
    res = parseFloat(q1) + parseFloat(q2) + parseFloat(q3) + parseFloat(q4) + parseFloat(q5); // add total score
    frm.result.value = res; // make the value of result the value of score
    } 
    </script> 
    Code (markup):
    Thanks for all your help and responses. Sorry if I wasn't too clear, first time on a forum.:)
     
    badnmad, Jun 15, 2008 IP