这是一个小提琴。http://fiddle.jshell.net/GF9nj/1/我要弄清楚的是为什么当我将对象数据推入randomAnswerArray时,即使在测试<p>中也没有显示。它大约在JavaScript部分的中间。
这是有问题的代码:
// incorrectAnswer1, incorrectAnswer2, incorrectAnswer3 are initialised elsewhere ...
randomAnswerArray = []; //resets the array to empty
randomAnswerArray.push(randomQuestion.correctAnswer);
document.getElementById('test3').innerHTML=randomAnswerArray[0]; //TESTING doesn't work
randomAnswerArray.push(randomQuestion.incorrectAnswer1);
randomAnswerArray.push(randomQuestion.incorrectanswer2);
randomAnswerArray.push(randomQuestion.incorrectanswer3);
document.getElementById('test1').innerHTML = randomAnswerArray.valueOf();
valueOf()调用仅显示randomAnswerArray中的0和1项,但我也同时推2和3。
的HTML
<p id='questionString'></p>
<p id='test1'></p>
<p id='test2'></p>
<p id='test3'></p>
<button onclick='generate()'>Click me to start!</button>
的JavaScript
var questionList = [];
var randomAnswerArray = [];
// this is the question object constructor
function quizQuestion(question, correctAnswer, incorrectAnswer1, incorrectAnswer2, incorrectAnswer3) {
this.question = question;
this.correctAnswer = correctAnswer;
this.incorrectAnswer1 = incorrectAnswer1;
this.incorrectAnswer2 = incorrectAnswer2;
this.incorrectAnswer3 = incorrectAnswer3;
}
// this constructs a question
var hairColor = new quizQuestion("What color is my hair?", "black", "blue", "red", "purple");
// this adds the question to the questionList
questionList.push(hairColor);
document.getElementById('test2').innerHTML = questionList[0].correctAnswer; //TESTING object constructor works
function generate() {
// this part picks a random question
var randomQuestion = questionList[Math.floor(Math.random() * questionList.length)];
// this part puts the question in the questionString
document.getElementById("questionString").innerHTML = randomQuestion.question;
// this part puts the answers in the array
// THIS IS WHAT WE ARE TESTING VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV
randomAnswerArray = []; //resets the array to empty
randomAnswerArray.push(randomQuestion.correctAnswer);
document.getElementById('test3').innerHTML=randomAnswerArray[0]; //TESTING doesn't work
randomAnswerArray.push(randomQuestion.incorrectAnswer1);
randomAnswerArray.push(randomQuestion.incorrectanswer2);
randomAnswerArray.push(randomQuestion.incorrectanswer3);
document.getElementById('test1').innerHTML = randomAnswerArray.valueOf(); //TESTING doesn't work
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// this part randomizes the array
var currentIndex = randomAnswerArray.length;
var temporaryValue;
var randomIndex;
while (0 !== currentIndex) {
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
temporaryValue = randomAnswerArray[currentIndex];
randomAnswerArray[currentIndex] = randomAnswerArray[randomIndex];
randomAnswerArray[randomIndex] = temporaryValue;
}
}
在您的推式语句中,您拼错了这些
incorrectAnswer2
incorrectAnswer3
作为
incorrectanswer2
incorrectanswer3
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句