如何使用javascript随机添加表单输入值?

时间:2014-08-28 23:18:45

标签: javascript

比方说,我在数组中有一个问题答案池。

如何在表格输入值中呈现3个随机答案和1个正确答案?

让我试试这个半伪代码真的很快..

var answerList = [1, 2, 3, ,4 ,5 ,6 ,7, 8, 9, 10];

var questionIdArrays = [1]

var question1 = {

   questionId: 1,
   question: "What is 5 + 5?",
   correctAnswer: 5

  }

get random question from questionIdArrays

because questionIdArray is 1, get questionId 1

display question1's question in <h1> element

randomly display correctAnswer in 1 of the 4 input values
randomly grab 3 answers from answerList and place them randomly in the remaining 3 input values

Hopefuly这是有道理的!困难的部分是答案的随机输出,我很困惑。提前谢谢!

1 个答案:

答案 0 :(得分:0)

Zero the correct answer from the answerList
create loop
use Math.random() to get a number from the list
test if result is zero
  if it is then get another number
store value 
zero out result