Javascript 随机数组,或者类似的东西
还有什么要做的吗Javascript 随机数组,或者类似的东西,javascript,jquery,arrays,random,Javascript,Jquery,Arrays,Random,还有什么要做的吗 var init = { 'questions': [ { 'question': 'jQuery is a...', 'answers': ['JavaScript library','Ruby Gem','PHP Framework','None of the above'], 'correctAnswer': 1 }, { 'question': 'X comes after?',
var init = {
'questions': [
{
'question': 'jQuery is a...',
'answers': ['JavaScript library','Ruby Gem','PHP Framework','None of the above'],
'correctAnswer': 1
},
{
'question': 'X comes after?',
'answers': ['P','W','Y','v'],
'correctAnswer': 2
},
{
'question': 'Meh stands for..',
'answers': ['Eh','/Sigh','What?','Whatever'],
'correctAnswer': 4
}
] };
现在,系统以它的书写方式向我展示了这个问题。有没有办法让这些问题随机出现?谢谢。您似乎想要这个:
var questions = init.questions;
var question = questions[Math.floor(Math.random()*questions.length)]
这将为您提供一个
问题
数组的随机元素。您似乎希望这样:
var questions = init.questions;
var question = questions[Math.floor(Math.random()*questions.length)]
这将为您提供一个
问题
数组的随机元素。这个问题有两个可能的角度;最初的问题不清楚是哪一个意思(或者可能两者都是),所以我将同时讨论这两个问题
{
'question': 'X comes after?',
'answers': [
{'answer':'P','correct':false},
{'answer':'W','correct':true},
{'answer':'Y','correct':false},
{'answer':'v','correct':false}
]
},
现在很容易对答案和问题使用相同的洗牌技巧,而不会忘记哪个答案是正确的
希望能有所帮助。这个问题有两个可能的角度;最初的问题不清楚是哪一个意思(或者可能两者都是),所以我将同时讨论这两个问题
{
'question': 'X comes after?',
'answers': [
{'answer':'P','correct':false},
{'answer':'W','correct':true},
{'answer':'Y','correct':false},
{'answer':'v','correct':false}
]
},
现在很容易对答案和问题使用相同的洗牌技巧,而不会忘记哪个答案是正确的
希望这能有所帮助。你能解释更多什么是需求吗?你能解释更多什么是需求吗?嗯,我对此很在行,所以我应该如何实现你在处理var init并显示代码的代码中提供的代码?pastebin.com/2SE0VWDC这完全取决于您当前的代码。如果你的代码有一个问题索引,那么它就是
Math.floor(Math.random()*questions.length)
.pastebin.com/2se0vwDC好吧,这是代码,非常混乱,但也许你能弄明白?我真的被卡住了。从第34行开始有一个for循环。在循环开始时,添加var index=Math.floor(Math.random()*questions.length)
并将所有问题[questionsIteratorIndex]
替换为问题[index]
。哦,我的天啊。。。谢谢你,先生!嗯,我在这方面很在行,那么我应该如何实现您在代码中提供的代码,谁处理var init并显示代码?pastebin.com/2SE0VWDC这完全取决于您当前的代码。如果你的代码有一个问题索引,那么它就是Math.floor(Math.random()*questions.length)
.pastebin.com/2se0vwDC好吧,这是代码,非常混乱,但也许你能弄明白?我真的被卡住了。从第34行开始有一个for循环。在循环开始时,添加var index=Math.floor(Math.random()*questions.length)
并将所有问题[questionsIteratorIndex]
替换为问题[index]
。哦,我的天啊。。。谢谢你,先生!