jQuery测验应用程序如何实现基于点的答题系统? [英] jQuery quiz application how to implement a point based answering system?

查看:76
本文介绍了jQuery测验应用程序如何实现基于点的答题系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我是javascript和jquery的新手.我一直在看教程,这些教程是一个很好的起点,但是它们似乎都存在一个正确的问题,因此它们可以在函数内检查用户是否通过例如if等于语句来回答是否正确. /p>

So I am reasonably new to javascript and jquery. I have been looking at tutorials which have been a good starting point however they all seem to have one question that is right, so they are able to check within a function if the users answer is correct via a if equal to statement for example.

if (answers[i] == userAnswers[i]) { 
        flag = true; 
    } 

但是我想做的是,例如,每个可能的答案都有一个不同的值

What I would like to do however, is have a different value attached to each of the potential answers for example

var 1 = 50
var 2 = 0
var 3 = 10

问题1

var a = 1
var b = 2
var c = 3
var d = 3

问题2

var a = 3
var b = 1
var c = 2
var d = 3

做这样的事情的最好方法是什么?

What would be the best way to do something like this?

推荐答案

首先,一个假设:

  • answers[i]包含一个数字,这是用户为回答问题i所选择的(注意:如果answers[i]实际上是一个字符,则可以通过以下方式获取整数值:用'a'的ascii值减去answers[i].
  • answers[i] contains a number which is what the user picked as an answer to question i (Note: if answers[i] is in fact a character, you can get the integer value by subtracting answers[i] by the ascii value of 'a').

要做您想做的事情,只需定义一个二维数组score[i][j],其中i是问题的索引,而j是选项号(即,对问题i的回答) ),然后score[i][j]给出该问题的分数.因此,假设问题1的选项如您上面所述(即a =好,b =错误,...),那么您应该有

To do what you want to do, simply define a 2-dimensional array score[i][j], where i is the index of the question, and j is the option number (ie the asnwer to question i), then score[i][j] gives the score for this question. So let's say for question 1, the options are as you described above (ie a = good, b = wrong, ...), then you would have

//set the values for the answers
score[1][1] = good; score[1][2] = wrong; score[1][3] =  wrong; score[1][4] = okay
score[2][1] = okay; score[2][2] = good; score[2][3] = wrong;
//more score setting

//get the value of the answer
var score_q1 = score[1][answers[1]]
var score_q2 = score[2][answers[2]]

即,由于answers[i]以数字形式包含答案的值,因此通过调用score[question_nb][answers[question_nb]]可获得该问题的分数.在这种情况下,没有if语句.如果您想获得总分,只需遍历所有问题并总结该问题的得分即可.

ie, since answers[i] contains the value of the answer as a number, by calling score[question_nb][answers[question_nb]] you get the score for that question. In this case, there are no if statements. If you want to get the total score, just loop over all questions and sum up the score for that question.

这篇关于jQuery测验应用程序如何实现基于点的答题系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆