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?!

3
  • Should be location[math][0] and location[math][1] respectively. Commented Apr 10, 2015 at 17:41
  • stackoverflow.com/questions/966225/… Commented Apr 10, 2015 at 17:42
  • Yes, but that would display the answer. location[[0 1]]; Where 0 is the question and 1 is the answer. Commented Apr 10, 2015 at 17:48

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.