0

I'm trying to make a trivia game thing, it'll have nice CSS layouts later. But I need to write the result on the page (easy) but I can't think of a good way to process the result. Here's my code:

function testResults (form) {
var answer = form.inputbox.value;
if (answer == /*This is where I'm having troubles at! */) {
document.getElementById("result").innerHTML = "Right!"
} else {
    document.getElementById("result").innerHTML = "Wrong!"
}
var triviaQ = [];
triviaQ[0] = {que: "Answer true.", ans: true}
triviaQ[1] = {que: "Answer false.", ans: false}
triviaQ[2] = {que: "Answer John.", ans: "John"}
triviaQ[3] = {que: "Answer herp.", ans: "herp"}

var Q = triviaQ.length;
var currentTrivia=Math.round(Math.random()*(Q-1));
function showtriviaQ(){document.getElementById("question").innerHTML = triviaQ[currentTrivia];}

As you can see, I have most of it complete except for where the comment is. The "if" statement starts with a check of YOUR answer (from a text box) to the current question's property.

The questions are completely random as a test to see if it would work, but I can't figure out a way to write the properties in the "if" statement check.

Z91www
  • 21
  • 1
  • I'm not sure I understand the question; if you know the current question, you need to convert the value into something comparable with the question's `ans` property. – Dave Newton Feb 15 '12 at 01:21
  • Beware that you should use Math.floor(Math.random()*Q). Math.random() returns a number in the interval [0,1[. When you multiply it by Q, you get a number in the interval [0,Q[, and when you use floor, you get an equiprobable integer in the interval [0, Q-1]. Your way, using round, you're getting 0 and Q-1 with half probability of the rest of the numbers. – djjeck Feb 15 '12 at 22:25

1 Answers1

0

I think this would work.

if(answer == trivia[currentTrivia].ans)

You'll need to parse the boolean values so that they'll really be equal:

var isTrueSet = (myValue === 'true');

from here

Community
  • 1
  • 1
JustMaier
  • 2,101
  • 21
  • 23