Sie sind auf Seite 1von 3

<p>

Video Game Trivia


<br />
Asking question <span id="NumberAsked">1</span> of 10
with <spanid="NumberCorrect">0</span> answers correct</p>
<p>
<span id="TriviaQuestion">???</span>
</p>
<p>
&nbsp;
<input id="RadioTrue" type="radio" value="true" name="answer"checked="checke
d" /> True
&nbsp;&nbsp;&nbsp;
<input id="RadioFalse" type="radio" value="false" name="answer"/> False
</p>
<p>
<input id="ButtonContinue" onclick="checkAnswer();" type="button"value="cont
inue" />
</p>

<script language="javascript" type="text/javascript">


// Array of trivia data
var TriviaData = new Array(10)
createTwoDimensionalArray(3);

// Variables to track state of the game


// i.e. number questions asked, current correct total and current question
var questionsAsked = 0;
var totalCorrect = 0;
var currentQuestion = 0;
var selectionValid = false;

// Questions
TriviaData[0][0] = "A preposition is usually followed by a "noun".";
TriviaData[1][0] = "Phrasal verbs always consist of two words.";
TriviaData[2][0] = "The past tense of "must" is "musted";
TriviaData[3][0] = "The verb "to think" is never used in continuous
tenses.";
TriviaData[4][0] = "Questions always use an auxiliary verb.";
TriviaData[5][0] = ""Used to doing" and "used to do" mean approximately the
same thing.";
TriviaData[6][0] = "The word "people" is always uncountable.";
TriviaData[7][0] = "The shortest possible sentence contains a subject, a
verb and an object.";
TriviaData[8][0] = "The main verb and the direct object are not normally
separated.";
TriviaData[9][0] = "The auxiliary verb "to do" is never used in the present
simple affirmative.";

// Answers
TriviaData[0][1] = "true";
TriviaData[1][1] = "false";
TriviaData[2][1] = "false";
TriviaData[3][1] = "false";
TriviaData[4][1] = "false";
TriviaData[5][1] = "false";
TriviaData[6][1] = "false";
TriviaData[7][1] = "false";
TriviaData[8][1] = "true";
TriviaData[9][1] = "false";

// Has question been asked


// -- necessary because we are asking in random order
TriviaData[0][2] = "no";
TriviaData[1][2] = "no";
TriviaData[2][2] = "no";
TriviaData[3][2] = "no";
TriviaData[4][2] = "no";
TriviaData[5][2] = "no";
TriviaData[6][2] = "no";
TriviaData[7][2] = "no";
TriviaData[8][2] = "no";
TriviaData[9][2] = "no";

// Load up first question


setQuestion();

// Sets question text and indicator so that we know the question has been
displayed
function setQuestion() {
selectionValid = false; // Flag to make sure question has not been asked
yet
while (selectionValid == false) {
currentQuestion = Math.floor(Math.random() * 10); // randomly select
starting question
if (TriviaData[currentQuestion][2] == "no") {
selectionValid = true;
}
}
document.getElementById("TriviaQuestion").innerHTML =
TriviaData[currentQuestion][0];
TriviaData[currentQuestion][2] = "yes";
questionsAsked = questionsAsked + 1;
}

function processAnswer(myAnswer) {
if (TriviaData[currentQuestion][1] == myAnswer) {
// answer correct
totalCorrect = totalCorrect + 1;
}
}

// This function creates our two dimensional array


function createTwoDimensionalArray(arraySize) {
for (i = 0; i < TriviaData.length; ++i)
TriviaData[i] = new Array(arraySize);
}

// This function checks the answer, updates correct total


// and randomly selects the next question
function checkAnswer() {
if (document.getElementById("RadioTrue").checked) {
processAnswer("true");
}
else {
processAnswer("false");
}
// get next question if not asked all yet
if (questionsAsked < 10) {
setQuestion();
}
// final question asked - disable button and show final results
else {
alert("Quiz complete! You got " + totalCorrect + " correct out of
10.");
document.getElementById("ButtonContinue").disabled =true;
}
// update totals
document.getElementById("NumberAsked").innerHTML = questionsAsked;
document.getElementById("NumberCorrect").innerHTML = totalCorrect;
}

</script>

Das könnte Ihnen auch gefallen