0

I'm using jquery and have two arrays with the names, question0AnswerTextArray and question1AnswerTextArray.

Inside these arrays are strings ["text for answer 1", "text for answer 2"]

I also have a variable named quizQuestion.

I'm trying to get text to display like below:

var tempBoxText = 'question'+quizQuestion+'AnswerTextArray['+answerNumber+']'; 
$('#quizTextBox').text(tempBoxText);

Any ideas? Or are multidimensional arrays possible in JavaScript/jQuery?

Thanks for suggestions..I just started JavaScript recently.

2 Answers 2

2

As you already mentioned, a better way would be to use an array of arrays:

var answers = [
    ["text for answer 1", "text for answer 2"],
    ["text for answer 1", "text for answer 2"]
];

$('#quizTextBox').text(answers[quizQuestion][answerNumber]);

I recommend to read the MDN JavaScript Guide.

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

1 Comment

Thank you for such quick responses! This help a lot!
0

Multi-dimensional arrays are possible in JavaScript. Setting them up is a little different (but is just an array of arrays), and the syntax for retrieving values is like:

var val = myArray[x][y];

x and y can be your question and answer indices.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.