0

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????

<!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>
2
Contributors
6
Replies
7
Views
6 Years
Discussion Span
Last Post by lm111
0

It depends on how you accept the answer? Assume that spelling of all combined words is correct and the number of words coming in are the same.

For example, the answer is 'body,hips,knees'
1)The incoming string must not be 'bidy,hips,knees' or 'body,hips,knee' or etc.
2)The incoming string must not be 'body,hips' or 'hips,knees' or etc.
2)The incoming string must not be 'body knees hips' or 'hips-knees-body' or etc.
3)The incoming string may be 'body, hips, knees' or 'body , hips,knees'.

From the assumption, you may check it as follows:

var ex = "body,hips,knees"
var ans = "hips, knees,body"
var ansArr = ans.split(/\s*,\s*/)
var matched = true
for (var i=0; i<ansArr.length; i++) {
  if (!ex.match(ansArr[i])) {
    matched = false
    break
  }
}
return matched
0

Thank you for your reply. How can I implement that piece of code in my script? Could you please give me some hints? Thank you so much!!

0

Between line 45 & 66 are your checking the answer portion. Currently, it checks for "exact" string. If you know that the 2nd question is going to be what I said, you could do as follows:

//################################################################################
      if (i==1) {  // this is hard-coded and will work if and only if you are checking question #2!
        // **add the check here for the 2nd question by splitting the resp**
        // **and check whether all substrings are matched with answ**
        var ansArr = resp.split(/\s*,\s*/)  // split whitespace if any + , + whitespace if any
        var matched = true
        for (var a=0; a<ansArr.length; a++) {
          if (!answ.match(ansArr[i])) {
            matched = false
            break
          }
        }
        // then check if the matched value is 'true'
        // if it is true, the user answers it correctly; otherwise, wrong answer.
        if (matched) { correct++ }
      }
      else {
        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;
            }
       }
//#################################################################################

I still think that the way the structure of answer is a bit awkward. But if it is what you want, it is fine. ;)

Edited by Taywin: n/a

0

Hmm... Just realize that the code I posted had certain issues... I copied and pasted without checking it... I'm sorry... I tried to condense it, and now it should be...

//################################################################################
  var matched = true
  if (i==1) {  // this is hard-coded and will work if and only if you are checking question #2!
    // **add the check here for the 2nd question by splitting the resp**
    // **and check whether all substrings are matched with answ**
    var ansArr = resp.split(/\s*,\s*/)  // split whitespace if any + , + whitespace if any
    for (var a=0; a<ansArr.length; a++) {
      if (!answ.match(ansArr[i])) {
        matched = false
        break
      }
    }
  }
  else if (resp != answ) { matched = false }

  // then check if the matched value is 'true'
  // if it is true, the user answers it correctly;
  if (matched) {
    correct++;
    document.forms[0].a1c.style.backgroundImage="url('correct.gif')";
    document.forms[0].a1c.value = "";
  }
  // otherwise, wrong answer.
  else {
    if(i==0) {
      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.value = " ANS: " + answ;
    }
    document.forms[0].a1d.style.backgroundImage = "url('incorrect.gif')";
  }
//#################################################################################
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.