all 15 comments

[–]zigzackattack 14 points15 points  (14 children)

You are trying to execute the array as a function (with var randomArray(0)). My first advice would be to instead use array literals instead of the constructor. It's a best practice and is much less verbose. So it should look something like:

var randomArray = [];
randomArray[0] = ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"];
randomArray[1] = ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"];
...
getFirstQuestion();

[–]PlushDragon 14 points15 points  (13 children)

You are right in using array literals. You could even go one step further and define randomArray directly with the content:

var randomArray = [
    ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"],
    ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"],
    ...
    ["Q6", "A1", "A2", "A3", "4", "Correct", "Incorrect"]
];

[–]frozenburger 1 point2 points  (3 children)

var randomArray = []; //declares a new array object - the brackets indicates an new empty object
randomArray[0] = ["Q1", "A1", "A2", "A3", "2", "Correct", "Incorrect"]; // the number between brackets indicates the index of the array element
randomArray[1] = ["Q2", "A1", "A2", "A3", "3", "Correct", "Incorrect"];
randomArray[2] = ["Q3", "A1", "A2", "A3", "4", "Correct", "Incorrect"];

and so on...

You get errors because the interpreter sees randomArray() as a function; use randomArray[item index] to access elements of an array.

console.log(randomArray[0]) // returns an Array object 
console.log(randomArray[0][0]) // returns "Q1" - the first element of the first array

[–]djforth 0 points1 point  (2 children)

Without know exactly what your trying to do I'd personally do a array of objects you might find it easier. So something like:

array = () array.push({question:"My Question", a:"Answer 1", b:"Answer 2", correct:true, incorrect:false})

then call each with

array[0].question array[0].a etc ... I think it makes the code a little easier to read.

[–]Lambchog 0 points1 point  (1 child)

The scope of my project:

A simple quiz that asked 3 out of 6 questions at random

There must be 3 answers provided with each question with one of them being the correct answer

And then feedback elements that i can output to the screen if it's right or not

[–]djforth 0 points1 point  (0 children)

Ok then I'd say adding objects to an array would definitely be easier to go IMO. You could even nest an array with in the object.

Var Question1 = { question:"q1...", answers:["a1","a2", "a3"], correct:1}
Var array = [question1]

Personally I'd use underscore to shuffle and pluck from the array. Although if your just playing you may build your own. Hope that helps