...

View Full Version : Hi I need Help :(



Savard
07-30-2010, 06:10 AM
Hi
PLZ HELP ME :(
this code is for randomly showing questions of a quize, but it's not working??!!!


<html> <head> <script>


function DisplayQuestions() {
var MCQ = new Array(5);
MCQ[0] =document.getElementById("Q1");
MCQ[1] =document.getElementById( "Q2");
MCQ[2] =document.getElementById( "Q3");
MCQ[3] =document.getElementById( "Q4");
MCQ[4] = document.getElementById("Q5");

var i = 0;

for(i=0;i<5;i++) {
var Q = Math.floor(1+Math.random()*5);

if (MCQ[Q] !=null ) {
document.writeln("Question number " + Q + " is " + MCQ[Q]);
i++;}
MCQ[Q] = null; // Dump this question - it's been displayed

}
}


function CheckAns(){
var myQuiz = document.getElementById( "Quiz" );
var g=0;
// determine whether the answer is correct
if ( Quiz.elements[ 2 ].checked )
g++;
}

</script>
</head>
<body>
<form id = "Quiz" onsubmit = "CheckAns()" action = "">
<p id="Q1"> What is the only function all C++ programs must contain? <br/>

<input type="radio" name="c" value="A"/> <label>A.start()</label> <br/>
<input type="radio" name="c" value="B"/><label>B.system()</label> <br/>
<input type="radio" name="c" value="C"/> <label>C.main()</label><br/>
<input type="radio" name="c" value="D"/> <label>D.program()</label><br/>
<input type = "submit" name = "submit" value = "Submit" />
<input type = "reset" name = "reset" value = "Reset" />
</p>




<p id="Q2"> What punctuation is used to signal the beginning and end of code blocks? <br/>

<input type="radio" name="c" value="A"/> <label>A.{ }</label> <br/>
<input type="radio" name="c" value="B"/><label>B.-> and <-</label> <br/>
<input type="radio" name="c" value="C"/> <label>C.BEGIN and END</label><br/>
<input type="radio" name="c" value="D"/> <label>D.(and)</label><br/>
<input type = "submit" name = "submit" value = "Submit" />
<input type = "reset" name = "reset" value = "Reset" />
</p>



<p id="Q3"> Which of the following is a correct comment? <br/>

<input type="radio" name="c" value="A"/> <label>A.*/ Comments */</label> <br/>
<input type="radio" name="c" value="B"/><label>B.** Comment **</label> <br/>
<input type="radio" name="c" value="C"/> <label>C./* Comment */</label><br/>
<input type="radio" name="c" value="D"/> <label>D.{ Comment }</label><br/>
<input type = "submit" name = "submit" value = "Submit" />
<input type = "reset" name = "reset" value = "Reset" />
</p>



</form>

</body></html>

Old Pedant
07-30-2010, 07:21 AM
var Q = Math.floor(1+Math.random()*5);

Your questions are numbered starting at *ZERO* so you do *NOT* want to add 1 to the randomly generated value.

var Q = Math.floor(Math.random()*5);

Old Pedant
07-30-2010, 07:22 AM
You have other problems, too. But that will get you started.

Philip M
07-30-2010, 08:23 AM
Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.

Savard
07-30-2010, 03:28 PM
Now How can i find the score :(


<html> <head> <script>

var score=new Array();

var MCQ =new Array ("What is the only function all C++ programs must contain?<br/>",
"What punctuation is used to signal the beginning and end of code blocks?<br/>",
"What punctuation ends most lines of C++ code?<br/>",
"Which of the following is the correct operator to compare two variables?<br/>",
"Which of the following is not a correct variable type?<br/>");


var Q1=new Array("<form id=\"q1\"><input type=\"radio\" name=\"A\" value=\"A\"/> <label>start()</label><br/>",
"<input type=\"radio\" name=\"B\" value=\"B\"/><label>system()</label> <br/>",
"<input type=\"radio\" name=\"C\" value=\"C\" onClick=score[0]=1 /> <label>main()</label><br/>",
"<input type=\"radio\" name=\"D\" value=\"D\"/> <label>program()</label></form><br/> <br/>");

var Q2=new Array("<input type=\"radio\" name=\"c\" value=\"A\" onClick=\"score[1]=1\"/> <label>{ }</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"B\"/><label>-> and <-</label>",
"<br/><input type=\"radio\" name=\"c\" value=\"C\"/> <label>BEGIN and END</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"D\"/> <label>(and)</label> <br/> <br/>");




var Q3=new Array("<input type=\"radio\" name=\"c\" value=\"A\"/> <label>(dot)</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"B\" onClick=\"score[2]=1\" /><label>;(Semi colon)</label>",
"<br/><input type=\"radio\" name=\"c\" value=\"C\"/> <label>:(Colon)</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"D\"/> <label>'(Single quote)</label> <br/> <br/>");



var Q4=new Array("<input type=\"radio\" name=\"c\" value=\"A\"/> <label>:=</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"B\"/><label>=</label>",
"<br/><input type=\"radio\" name=\"c\" value=\"C\"/> <label>Equal</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"D\" onClick=\"score[3]=1\"/> <label>==</label> <br/> <br/>");




var Q5=new Array("<input type=\"radio\" name=\"c\" value=\"A\"/> <label>float</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"B\" onClick=\"score[4]=1\"/><label>real</label>",
"<br/><input type=\"radio\" name=\"c\" value=\"C\"/> <label>int</label><br/>",
"<input type=\"radio\" name=\"c\" value=\"D\"/> <label>double</label> <br/> <br/>");









function DisplayQuestions() {


dis: for(var i=0;i<100;i++) {
var Q = Math.floor(Math.random()*5);

if(MCQ[Q]==null)
continue dis;
else {

document.writeln( MCQ[Q]);

if(Q==0){

ans1: for(var j=0;j<=100;j++){
var r = Math.floor(Math.random()*4);
if(Q1[r]==null)
continue ans1;
else {document.writeln(Q1[r]);
Q1[r]=null;} document.write("<br/>") }
}

else if(Q==1){
ans2: for(var a=0;a<=100;a++){
r = Math.floor(Math.random()*4);
if(Q2[r]==null)
continue ans2;
else {document.writeln(Q2[r]);
Q2[r]=null;} document.write("<br/>");}
}//end if



if(Q==2){
ans3: for(var a=0;a<=100;a++){
r = Math.floor(Math.random()*4);
if(Q3[r]==null)
continue ans3;
else {document.writeln(Q3[r]);
Q3[r]=null;} document.write("<br/>");}
}//end if



if(Q==3){
ans4: for(var a=0;a<=100;a++){
r = Math.floor(Math.random()*4);
if(Q4[r]==null)
continue ans4;
else {document.writeln(Q4[r]);
Q4[r]=null;} document.write("<br/>");}
}//end if




if(Q==4){
ans5: for(var a=0;a<=100;a++){
r = Math.floor(Math.random()*4);
if(Q5[r]==null)
continue ans5;
else {document.writeln(Q5[r]);
Q5[r]=null;} document.write("<br/>");}
}//end if

} MCQ[Q]=null;

document.write("<br/>"); }}

DisplayQuestions()



function CheckAns(){

for(var i=0;i<5;i++)
var total=total+score[i];
document.write("the total is= "+total);
}

</script>
</head>
<body>






<input type = "submit" name = "submit" value = "Submit" onClick="CheckAns()" /> <input type = "reset" name = "reset" value = "Reset"
</body></html>

Old Pedant
07-30-2010, 08:54 PM
Lots and lots of problems in that code.

The funniest one is maybe this one: If the user clicks on every single radio button (all possible ones) on the page, he is guaranteed a perfect score.

You should *NOT* keep score "on the fly" as the buttons are pushed.

Only when the user finally pushes a "complete" button should you go calculate score.

Of course, nobody would use code like this in real life, as the user can simply view the code on the page to find all the correct answers.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum