开发者

an array, and a variable to display as a string

开发者 https://www.devze.com 2023-04-13 07:30 出处:网络
I\'m using jquery and have two arrays with the names, question0AnswerTextArray and ques开发者_如何学Pythontion1AnswerTextArray.

I'm using jquery and have two arrays with the names, question0AnswerTextArray and ques开发者_如何学Pythontion1AnswerTextArray.

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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号