How to implement multidimensional arrays with JavaScript _javascript tips

Source: Internet
Author: User
Tags javascript multidimensional array

In C #, a multidimensional array can be defined in such a simple code:

Copy Code code as follows:

Int[,]myarray=new int[4,5];

How do you implement a multidimensional array without directly defining it in JavaScript?
First, define a one-dimensional array:
Copy Code code as follows:

var myarray=new Array ();

The members of a one-dimensional array are then defined as arrays (which can be redefined because JavaScript is a weak type):
Copy Code code as follows:

Myarray[0]=new Array ();

So far, you define a two-dimensional array with the first index of 0, and if you want to use the first two-dimensional array of 1, you still need to define it before you use it:
Copy Code code as follows:

<span style= "Font-family:calibri;" >myarray[1]=new Array ();</span>

Here is an example of a JavaScript multidimensional array application that uses multidimensional arrays to store multiple-choice questions and answers:

Copy Code code as follows:

<script type= "Text/javascript" >
To interpret the answer correctly
function Answercorrect (questionnumber, answer) {
var correct = false;
if (answer = = Answer[questionnumber])
correct = true;

return correct;
}

Defines an array of questions and answers to store topics and options
var questions = new Array ();
var answers = new Array ();

Define question 1, define the member with index 0 as a two-dimensional array
Questions[0] = new Array ();

Topics, defining two dimensional array members
Questions[0][0] = "The Beatles were:";
Answer
QUESTIONS[0][1] = "A sixties rock group from Liverpool";
QUESTIONS[0][2] = "Four musically gifted insected";
QUESTIONS[0][3] = "German Cars";
QUESTIONS[0][4] = "I don ' t know";

The answer to question 1
Answers[0] = "A"

Question 2
Definition Question 2
QUESTIONS[1] = new Array ();
Questions[1][0] = "Homer simpon ' s favorite food is:";
QUESTIONS[1][1] = "Fresd slead";
QUESTIONS[1][2] = "doughnuts";
QUESTIONS[1][3] = "sea food";
QUESTIONS[1][4] = "apples";

The answer to question 2
ANSWERS[1] = "B";

Prompt initialization complete
Alert ("Array initiallized");

</script>

PS: In the recent learning of JavaScript process, often use Notepad to write programs, and then changed to the. htm format, which is not as efficient as in VS or Dreamweaver, mainly without smart hints and highlighting. But you can remind yourself of every little detail, such as JavaScript sensitivity, how to write HTML tags and so on, hehe.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.