我有以下数组:
{
quiztitle: "Name the US Presidents",
presidents: [
{
president_name: "George Washington",
number: "1",
imgurl: "c-1gw.jpg",
answer_variations: [
"Washington"
]
},
{
president_name: "John Quincy Adams",
number: "6",
imgurl: "c-6ja.jpg",
answer_variations: [
"Adams",
"John Adams"
]
}, .... ect
如何访问答案变体数组并将内容与提交的值进行比较?
我目前有这个代码来检查总统:
var answer = $('.presidentRow input').val().toLowerCase();
var correct = false;
$.each(data.presidents, function(i, president) {
if ( president.number == questionNumber && president.president_name.toLowerCase() === answer) {
correct = true;
return false;
}
});
此代码采用问题编号并根据总裁名称检查用户提交的值。如果它是正确的,它将变量设置为true,我可以使用它来生成结果。如何根据变化检查提交的答案?
由于
答案 0 :(得分:1)
另一种比较方式你可以试试〜
var answer = $('.presidentRow input').val().toLowerCase();
var correct = false;
$.each(data.presidents, function(i, president) {
if ( president.number == questionNumber) {
if(president.president_name.toLowerCase() === answer){
correct = true;
}else if((','+ president.answer_variations.join().toLowerCase()+',').indexOf(','+answer+',')!=-1){
correct = true;
}
return false;
}
});
修改强>
使用Array.join()
制作变体作为昏迷分隔字符串,您可以简单地使用String.indexOf()
来检查答案是否存在。
添加','
使答案符合正确的变体选项。
答案 1 :(得分:0)
如果答案是其中任何一个,答案是正确的。使用$.inArray查找索引,如果索引> = 0,答案就在列表中,所以它是正确的。
var answer = $('.presidentRow input').val().toLowerCase();
var questionNumber = $('#qNum').val();
var correct = false;
$.each(data.presidents, function(i, president) {
if ( president.number == questionNumber) {
// Put all possible name answers into an array.
var nameVairations =
$.merge([president.president_name], president.answer_variations);
// .toLowercase all answers
nameVairations = $.map(nameVairations, function(name) {
return name.toLowerCase();
});
// Check if the answer appears in the list
if ($.inArray(answer, nameVairations) >= 0) {
correct = true;
return false;
}
}
});
获得相同的结果
答案 2 :(得分:0)
所以基本上,迭代总统数组,直到我们发现其总统的数量等于问题数。然后根据总统名称测试答案,如果不是...尝试在answer_variations arrray中找到它。
body.setTransform(body.getWorldCenter(), angle - ((float)Math.PI)/2.0f);
我不知道你是怎么得到questionNumber的。
小测试
var answer = $('.presidentRow input').val().toLowerCase();
var presidents = data.presidents;
var correct = presidents.some(function(president) {
if (president.number == questionNumber) {
if (answer == president.president_name.toLowerCase()) {
return true;
} else {
return president.answer_variations.some(function(validAnswer) {
console.log(validAnswer.toLowerCase());
if (answer == validAnswer.toLowerCase()) {
return true;
}
});
}
}
});

var data = {
quiztitle: "Name the US Presidents",
presidents: [{
president_name: "George Washington",
number: "1",
imgurl: "c-1gw.jpg",
answer_variations: [
"Washington"
]
}, {
president_name: "John Quincy Adams",
number: "6",
imgurl: "c-6ja.jpg",
answer_variations: [
"Adams",
"John Adams"
]
}]
};
function presidentTest(answer, questionNumber) {
var answer = answer.toLowerCase();
return data.presidents.some(function(president) {
if (president.number == questionNumber) {
if (answer == president.president_name.toLowerCase()) {
return true;
} else {
return president.answer_variations.some(function(validAnswer) {
console.log(validAnswer.toLowerCase());
if (answer == validAnswer.toLowerCase()) {
return true;
}
});
}
}
});
}
/**** TEST *****/
document.getElementById('results').innerHTML += '\n' + presidentTest('george Washington', 1);
document.getElementById('results').innerHTML += '\n' + presidentTest('wachington', 1);
document.getElementById('results').innerHTML += '\n' + presidentTest('adams', 6);
document.getElementById('results').innerHTML += '\n' + presidentTest('john adams', 6);

答案 3 :(得分:0)
var answer = answer.toLowerCase().replace(/\s+/," ") // replace multiple spaces by single space and lower the case
var correct = false;
var president = data.presidents.find(function(president){return president.number == questionNumber;})
if(president){
var variationsArr = president.answer_variations.concat([president.president_name])
correct = ("|" + variationsArr.join("|").toLowerCase() + "|").indexOf("|" + answer + "|") > -1;
}