Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-15-2011, 04:18 PM   PM User | #1
lm111
New Coder

 
Join Date: Sep 2011
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
lm111 is an unknown quantity at this point
Fill in the blank and multiple choice quiz

I was wondering if someone could help me? In my code below what if answers in question 2 were allowed to be in any order? Like: hips,body,knees or knees,hips,body and so on. How should I modify my code? Anyone please????

Code:
<!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" xml:lang="en" lang="en">
<head>
<title>Quiz</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<script type="text/javascript">
var answer_list = [
    ['False'],
    ['body,hips,knees']

// Note: No comma after final entry
];

var response = [];

function getCheckedValue(radioObj) {
    if(!radioObj)
        return "";
    var radioLength = radioObj.length;
    if(radioLength == undefined)
        if(radioObj.checked)
            return radioObj.value;
        else
            return "";
    for(var i = 0; i < radioLength; i++) {
        if(radioObj[i].checked) {
            return radioObj[i].value;
        }
    }
    return "";
}

function setAnswer(question, answer) {
    response[question] = answer;
}

function CheckAnswers() {
    var correct = 0;
    var resp, answ;
    for (var i = 0; i < answer_list.length; i++) {
        resp = response[i].toString();
        resp = resp.toLowerCase();
        answ = answer_list[i].toString();
        answ = answ.toLowerCase();
//#################################################################################################
        if (resp == answ) {
            correct++;
            if(i==0){
                document.forms[0].a1c.style.backgroundImage="url('correct.gif')";
                document.forms[0].a1c.value = "";
            }
            else{
                document.forms[0].a1d.style.backgroundImage="url('correct.gif')";
                document.forms[0].a1d.value = "";
            }
        }
        else{
            if(i==0){
                document.forms[0].a1c.style.backgroundImage = "url('incorrect.gif')";
                document.forms[0].a1c.value = " ANS: False. Position the head snugly against the top bar of the frame and then bring the foot board to the infant's feet.";
            }
            else{
                document.forms[0].a1d.style.backgroundImage = "url('incorrect.gif')";
                document.forms[0].a1d.value = " ANS: " + answ;
            }
//###################################################################################################
        }
    }
    document.writeln("You got " + correct + " of " + answer_list.length + " questions correct!");
}
</script>
</head>
<body>
<form action="" method="post">
<div>
<b>1. When measuring height/length of a child who cannot securely stand, place the infant such that his or her feet are flat against the foot board.</b><br />
<label><input type="radio" name="question0" value="True" />True</label>
<label><input type="radio" name="question0" value="False" />False</label>
<br />
<textarea rows="2" cols="85" name="a1c" style="background-repeat:no-repeat"></textarea>
<br />
<b>2. When taking a supine length measurement, straighten the infant's
<input type="text" name="question1_a" size="10" />, 
<input type="text" name="question1_b" size="10" />, and 
<input type="text" name="question1_c" size="10" />.</b>
<br />
<textarea rows="2" cols="85" name="a1d" style="background-repeat:no-repeat"></textarea>
<br />
<input type="button" name="check" value="Check Answers" onclick="setAnswer(0,getCheckedValue(document.forms[0].question0));setAnswer(1,[document.forms[0].question1_a.value,document.forms[0].question1_b.value,document.forms[0].question1_c.value]);CheckAnswers();" />
</div>
</form>
</body>
</html>
lm111 is offline   Reply With Quote
Old 09-15-2011, 05:04 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, fill in the blank quizzes don't work well with relatively dumb computer programs, because what happens if you are expecting the answer "hips" and the user enters "hip" or maybe "shoulders" vs. "clavicles" or or or??

You almost surely need to allow for different spellings and synonyms, and that complicates your life.

So...how far do you want to take this?
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 09-15-2011, 05:09 PM   PM User | #3
lm111
New Coder

 
Join Date: Sep 2011
Posts: 25
Thanks: 8
Thanked 0 Times in 0 Posts
lm111 is an unknown quantity at this point
Let's say that the only acceptable answer is a combination of these 3 words
body, hips, knees

If they type in 'hip' then we'll just mark it as wrong.
Thank you.
lm111 is offline   Reply With Quote
Old 09-16-2011, 01:44 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb

One possibility ...
Code:
<script type="text/javascript">

function checkAnswers(cArr,response) {
  var flag = false;
  for (var i=0; i<cArr.length; i++) {
    if (cArr[i] == response) { flag = true; }
// following for partial match of responses
//    if (cArr[i].indexOf(response) != -1) { flag = true; }
  }
  return flag;
}

// following can be part of question/response/answer array
var correctAnswers = ['body','hips','knees'];  

// following are simulated responses from an HTML <input> tag
var responses = ['body','bod','hips','hip','knees','knee','legs','hands'];

// simulation
for (var i=0; i<responses.length; i++) {
  if (checkAnswers(correctAnswers,responses[i])) {
    alert(correctAnswers+'\n\n'+responses[i]+' is correct');
  } else {
    alert(correctAnswers+'\n\n'+responses[i]+' was NOT found');
  }
}
</script>
... of many!

Good Luck!
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:52 PM.


Advertisement
Log in to turn off these ads.