Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: Match function

  1. #1
    New Coder
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Match function

    Code:
    
    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);
    }
    Code:
    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?

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    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!

  • #3
    New Coder
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Your first description is correct

  • #4
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Try this (but be careful ... you're counting up to 18 (saquestions) but your array only has 17 items)
    Code:
    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++;

  • Users who have thanked devnull69 for this post:

    bradz1993 (03-15-2012)

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #6
    New Coder
    Join Date
    Oct 2011
    Posts
    23
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by devnull69 View Post
    Try this (but be careful ... you're counting up to 18 (saquestions) but your array only has 17 items)
    Code:
    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

  • #7
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Quote Originally Posted by bradz1993 View Post
    Whenever I get 2 or more right it gives me a mark of 0 :S
    Yeah, exactly as you wanted it :-)

    You said
    Code:
    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
    Code:
    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.


  •  

    Posting Permissions

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