0

I'm trying to access the first and second element of a multidimensional string array. Unsuccessfully.

Here is the code:

https://gist.github.com/anonymous/6868b2413a7220bf130a

If i replace the variables like this example:

var question = location[math[0]]

var question = location[math] 

It works as intended but displays the answers.

What kind of quiz would that be?!

Oren
  • 3
  • 3

3 Answers3

3

Try ...

var question = location[math][0];

... and ...

var question = location[math][1];

math per the referenced code is simply a number that is from zero to the length of location; this is the proper notation to get the array elements.

rfornal
  • 5,072
  • 5
  • 30
  • 42
0

First thought is that this isn't the best way of implementing a quiz, since the user can go right ahead and peak at the answers by inspecting the script?

That said, JS doesn't actually have 2D arrays, instead arrays of arrays! See here: JavaScript Multidimensional Arrays so you will need to change it to something like:

answer = location[math][1];

You should then find it works!

Community
  • 1
  • 1
saml
  • 794
  • 1
  • 9
  • 20
0

Solved it, had to use an object, here is the code.

https://gist.github.com/anonymous/319b88cd11a9a3958b6f

The quiz is for myself. Thank you for your time

Oren
  • 3
  • 3