PDA

View Full Version : Match function



bradz1993
03-13-2012, 05:46 PM
function displayMessage(text) {

// Open a new window
var msgWindow = window.open('', 'Message');

// Write message in the new Window
msgWindow.document.write(text);

// Raise this window, in case it's not visible
msgWindow.focus();

}

//Enter total number of questions:
var totalquestions=31
var mcquestions=13
var saquestions=18

var correctchoices=new Array("a","a","b","c","b","c","a","a","b","c","a","b","b");

var correctanswers=new Array("memory locations","_ underscore","x=5+5;","20","Else","lose win","ticker = ticker + 1;","Ticker = ticker + 1;","300.000","Jonathon \b","var","var counter;","var mark = 50, 70");

var shortanswers=new Array("identifier","assignment","false","true","false","reverse of the boolean result","the variable will be once the line is executed","var","value","2","4","2","3","no semi colon","no space/between else and if","missing curly bracket/no curly bracket","need else instead of else if");


function gradeit(){
var actualchoices=new Array()
var msg1=new Array()
var correctanswersno=0
var t=0
var sa=0
var displaycorrectanswers=""
for (q=1;q<=mcquestions;q++){
var thequestion=eval("document.Questionform.q"+q)

for (c=0;c<thequestion.length;c++){
if (thequestion[c].checked==true)
actualchoices[q]=thequestion[c].value
}

if (actualchoices[q]==correctchoices[q]){
msg1[t]=q+")"+" Correct";
correctanswersno = correctanswersno + 1;
t = t + 1;
} else {
msg1[t]=q+")"+" Incorrect: <br>Correct Answer: "+correctchoices[q]+") "+correctanswers[q];
t = t + 1;
}
}

for (s=1;s<=saquestions;s++){
var saanswers=("document.Questionform.sa"+s).match(shortanswers[s-1]);
if (saanswers.length==1){
sa = sa + 1;
}
}


for (p=0;p<=12;p++){
displaycorrectanswers+="<br>"+msg1[p];
}

var msg ="Multiple Choice Mark: "+correctanswersno+"/"+mcquestions+" <br><br>Individual Marks: "+displaycorrectanswers+"<br>Short answer Mark"+sa;

displayMessage(msg);
}






for (s=1;s<=saquestions;s++){
var saanswers=("document.Questionform.sa"+s).match(shortanswers[s-1]);
if (saanswers.length==1){
sa = sa + 1;
}
}


Every loop it looks through an individual textbox and if a word matches the word in the array, shortanswers, then this word is put into another array called saanswers and if this array has length 1 e.g. a word has been matched then 1 is added to sa. However it doesnt seem to work. any ideas?

devnull69
03-13-2012, 08:56 PM
The description of what you want to do is quite nebulous. Basically you want to find out if
exactly one of the following is true

- the content of the field with name sa1 is equal to shortanswers[0] OR
- the content of the field with name sa2 is equal to shortanswers[1] OR
- the content of the field with name sa3 is equal to shortanswers[2] OR
...
- the content of the field with name sa18 is equal to shortanswers[17]

Correct?

Or do you want to find out for each of the fields sa1 to sa18 if it matches ANY word of the array? Please be more specific!

bradz1993
03-13-2012, 10:50 PM
Your first description is correct

devnull69
03-14-2012, 07:56 AM
Try this (but be careful ... you're counting up to 18 (saquestions) but your array only has 17 items)


var sa = 0;
var saquestions = 18;
var saanswers = [];
var shortanswers = ["identifier","assignment","false","true","false","reverse of the boolean result","the variable will be once the line is executed","var","value","2","4","2","3","no semi colon","no space/between else and if","missing curly bracket/no curly bracket","need else instead of else if"];

var foundMatches = 0;
for (s=1;s<saquestions;s++){
if(document.Questionform["sa" + s].value == shortanswers[s-1]) {
saanswers.push(shortanswers[s-1]);
foundMatches++;
}
}
if(foundMatches === 1) sa++;

Philip M
03-14-2012, 08:27 AM
I see difficulties if the user is to enter his answer into a textbox.. What about case? Assignment != assignment. no semi colon != no semi-colon etc. curly bracket is usually called a brace. etc.

bradz1993
03-14-2012, 01:25 PM
Try this (but be careful ... you're counting up to 18 (saquestions) but your array only has 17 items)


var sa = 0;
var saquestions = 18;
var saanswers = [];
var shortanswers = ["identifier","assignment","false","true","false","reverse of the boolean result","the variable will be once the line is executed","var","value","2","4","2","3","no semi colon","no space/between else and if","missing curly bracket/no curly bracket","need else instead of else if"];

var foundMatches = 0;
for (s=1;s<saquestions;s++){
if(document.Questionform["sa" + s].value == shortanswers[s-1]) {
saanswers.push(shortanswers[s-1]);
foundMatches++;
}
}
if(foundMatches === 1) sa++;


Whenever I get 2 or more right it gives me a mark of 0 :S

devnull69
03-14-2012, 03:34 PM
Whenever I get 2 or more right it gives me a mark of 0 :S

Yeah, exactly as you wanted it :-)

You said


and if this array has length 1 e.g. a word has been matched then 1 is added to sa.

which implicates that if there is more or less than one match, nothing will be added to sa. But I expected this to be wrong in the first place, therefore I asked


Basically you want to find out if
exactly one of the following is true

- the content of the field with name sa1 is equal to shortanswers[0] OR
- the content of the field with name sa2 is equal to shortanswers[1] OR

and you agreed to that. Mind the word "exactly".

Please make sure that you state your requirements as exactly as possible. Any ambiguity will lead to erraneous code.