Here's a fiddle. http://fiddle.jshell.net/GF9nj/1/ What I'm trying to figure out is why when I .push my object data into the randomAnswerArray it doesn't show up even in the testing <p>. It's about halfway down the JavaScript section.
Here is the code in question:
// 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();
the valueOf() call displays only the 0 and 1 items from randomAnswerArray, but I'm pushing 2 & 3 as well.
Here is the full code of the fiddle:
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.