在做javascript测试时完全迷失了方向?

在做javascript测试时完全迷失了方向?,javascript,Javascript,我正在尝试用Javascript做一个测验,使用单选按钮。我希望能够显示分数,并向用户显示他们回答错误的问题 以下是我目前的脚本: var numQues = 4; var numChoices = 4; var answers = new Array(numQues) answers[0] = "b"; answers[1] = "c"; answers[2] = "b"; answers[3] = "a"; function getScore(form) { var score

我正在尝试用Javascript做一个测验,使用单选按钮。我希望能够显示分数,并向用户显示他们回答错误的问题

以下是我目前的脚本:

var numQues = 4;
var numChoices = 4;

var answers = new Array(numQues)
answers[0] = "b";
answers[1] = "c";
answers[2] = "b";
answers[3] = "a";

function getScore(form) {
    var score = 0;
    var currQ;
    var currChoice;


    if(currChoice.checked) {
        if(currChoice.value == answers[i]) {
            score++;
            break;
        }
    }
}

form.scoreOutofFour.value = score + "/4";
这是我的HTML:

<body>
  <div id="content">
    <form name="quiz">
      <table>
        <tr>
          <td> 
            <b>1.</b> Example Question 1  <br />
            <input type="radio" name="q1" value="a" /> a) Example answer 1 <br />
            <input type="radio" name="q1" value="b" /> b) Example answer 2 <br />
            <input type="radio" name="q1" value="c" /> c) Example answer 3<br />
            <input type="radio" name="q1" value="d" /> d) Example answer 4<br />
          </td>
        </tr>
        <tr>
          <td> 
            <b>2.</b>Example Question 2 <br />
            <input type="radio" name="q2" value="a" /> a) Example answer 1 <br />
            <input type="radio" name="q2" value="b" /> b) Example answer 2 <br />
            <input type="radio" name="q2" value="c" /> c) Example answer 3 <br />
            <input type="radio" name="q2" value="d" /> d) Example answer 4 <br />
          </td>
        </tr>
        <tr>
          <td> 
            <b>3.</b> Example Question 3  <br />
            <input type="radio" name="q3" value="a" /> a) Example answer 1 <br />
            <input type="radio" name="q3" value="b" /> b) Example answer 2<br />
            <input type="radio" name="q3" value="c" /> c) Example answer 3  <br />
            <input type="radio" name="q3" value="d" /> d) Example answer 4 <br />
          </td>
        </tr>
        <tr>
          <td> 
            <b>4.</b>Example Question 4<br />
            <input type="radio" name="q4" value="a" /> a) Example answer 1  <br />
            <input type="radio" name="q4" value="b" /> b) Example answer 2<br />
            <input type="radio" name="q4" value="c" /> c) Example answer 3 <br />
            <input type="radio" name="q4" value="d" /> d) Example answer 4   <br />
          </td>
        </tr>
        <tr>
          <td colspan="2">
            <b><input type="button" value="Get score" onClick="getScore(this.form); readtheCookie();" /> 
            <input type="reset" value="Clear" /></b> 
            <p>
              <b>
            <div id="cookie12"></div></b><br />
            <textarea name="scoreOutofFour" size="15"></textarea>
            <br />  
          </td>
        </tr>
      </table>
    </form>
  </div>
</body>
</html>

1.示例问题1
a) 示例答案1
b) 示例答案2
c) 示例答案3
d) 示例答案4
2.示例问题2
a) 示例答案1
b) 示例答案2
c) 示例答案3
d) 示例答案4
3.示例问题3
a) 示例答案1
b) 示例答案2
c) 示例答案3
d) 示例答案4
4.示例问题4
a) 示例答案1
b) 示例答案2
c) 示例答案3
d) 示例答案4



您离解决方案的距离并不像您想象的那么远

看看这个可能的解决方案,希望它能帮助你走上正确的道路

var numQues = 4;
var numChoices = 4;

var answers = new Array(numQues);
answers[0] = "b";
answers[1] = "c";
answers[2] = "b";
answers[3] = "a";


function getScore(form) {
    var score = 0;
    var currQ;
    var currChoice;
    var i, j;

    // loop through the number of questions
    for (i = 0; i < numQues; i ++) {

        // build the name of the form element we want to look at by
        // appending the loop number to 'q', on the first loop the value
        // will be 'q1', on the second 'q2' and so on
        var eleName = 'q' + (i + 1);

        // get all the form elements with the name we built above
        // this should get 4 elements (one for each of the selectable answers)
        var answerRadios = form[eleName];

        // loop through the 4 answer radio buttons
        for (j = 0; j < answerRadios.length; j++) {

            // create a variable to store the current radio button, so
            // it is easier to refernce
            var radio = answerRadios[j];

            // now we check that is this radio button is checked (selected)
            // and the value of this radio is the answer (as defined at the top)
            // then we add 1 tot he score
            if (radio.checked === true && radio.value === answers[i]) {
                score += 1;
            }
        }
    }

    // write the score to the html
    form.scoreOutofFour.value = score + "/4";
}
var numQues=4;
变量numChoices=4;
var answers=新数组(numQues);
答案[0]=“b”;
答案[1]=“c”;
答案[2]=“b”;
答案[3]=“a”;
函数getScore(表格){
var得分=0;
var-currQ;
风险价值选择;
varⅠ,j;
//循环检查问题的数量
对于(i=0;i
您离解决方案的距离并不像您想象的那么远

看看这个可能的解决方案,希望它能帮助你走上正确的道路

var numQues = 4;
var numChoices = 4;

var answers = new Array(numQues);
answers[0] = "b";
answers[1] = "c";
answers[2] = "b";
answers[3] = "a";


function getScore(form) {
    var score = 0;
    var currQ;
    var currChoice;
    var i, j;

    // loop through the number of questions
    for (i = 0; i < numQues; i ++) {

        // build the name of the form element we want to look at by
        // appending the loop number to 'q', on the first loop the value
        // will be 'q1', on the second 'q2' and so on
        var eleName = 'q' + (i + 1);

        // get all the form elements with the name we built above
        // this should get 4 elements (one for each of the selectable answers)
        var answerRadios = form[eleName];

        // loop through the 4 answer radio buttons
        for (j = 0; j < answerRadios.length; j++) {

            // create a variable to store the current radio button, so
            // it is easier to refernce
            var radio = answerRadios[j];

            // now we check that is this radio button is checked (selected)
            // and the value of this radio is the answer (as defined at the top)
            // then we add 1 tot he score
            if (radio.checked === true && radio.value === answers[i]) {
                score += 1;
            }
        }
    }

    // write the score to the html
    form.scoreOutofFour.value = score + "/4";
}
var numQues=4;
变量numChoices=4;
var answers=新数组(numQues);
答案[0]=“b”;
答案[1]=“c”;
答案[2]=“b”;
答案[3]=“a”;
函数getScore(表格){
var得分=0;
var-currQ;
风险价值选择;
varⅠ,j;
//循环检查问题的数量
对于(i=0;i
那么问题出在哪里?很难不知道出了什么问题。欢迎来到堆栈溢出!Stack Overflow是一个问答网站,所以你需要问一个问题才能得到有用的反馈。那么问题是什么呢?很难不知道出了什么问题。欢迎来到堆栈溢出!Stack Overflow是一个问答网站,因此您需要实际提问以获得任何有用的反馈。