【发布时间】:2014-07-25 15:05:58
【问题描述】:
这是一个小提琴。 http://fiddle.jshell.net/GF9nj/1/ 我想弄清楚的是为什么当我将我的对象数据 .push 到 randomAnswerArray 时,它甚至在测试
中也没有出现。它大约是 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;
}
}
【问题讨论】:
-
你的小提琴有一个脚本错误
generate is not defined。 -
怎么没定义?我很确定我正确设置了函数,对吧?
-
詹姆斯问,詹姆斯回答。
-
詹姆斯*是对的。为了能够测试,我需要添加 $('button').on('click', generate);
-
我在 下的 html 部分有它,它是根据 w3schools.com/tags/ev_onclick.asp 设置的
标签: javascript arrays html object