Skip to content

Commit f0ffd0e

Browse files
authored
Add files via upload
1 parent 3d33bcd commit f0ffd0e

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Create one dimensional array
2+
var arr = new Array(2);
3+
4+
document.write("Creating 2D array <br>");
5+
6+
// Loop to create 2D array using 1D array
7+
for (var i = 0; i < arr.length; i++) {
8+
arr[i] = new Array(2);
9+
}
10+
11+
var h = 0;
12+
13+
// Loop to initialize 2D array elements.
14+
for (var i = 0; i < 2; i++) {
15+
for (var j = 0; j < 2; j++) {
16+
arr[i][j] = h++;
17+
}
18+
}
19+
20+
// Loop to display the elements of 2D array.
21+
for (var i = 0; i < 2; i++) {
22+
for (var j = 0; j < 2; j++) {
23+
document.write(arr[i][j] + " ");
24+
}
25+
document.write("<br>");
26+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Two-dimensional array(L-B)
2+
3+
## Problem
4+
Write a program to construct a two-dimensional array using integer values.
5+
6+
## Solution
7+
8+
```javascript
9+
// Create one dimensional array
10+
var arr = new Array(2);
11+
12+
document.write("Creating 2D array <br>");
13+
14+
// Loop to create 2D array using 1D array
15+
for (var i = 0; i < arr.length; i++) {
16+
arr[i] = new Array(2);
17+
}
18+
19+
var h = 0;
20+
21+
// Loop to initialize 2D array elements.
22+
for (var i = 0; i < 2; i++) {
23+
for (var j = 0; j < 2; j++) {
24+
arr[i][j] = h++;
25+
}
26+
}
27+
28+
// Loop to display the elements of 2D array.
29+
for (var i = 0; i < 2; i++) {
30+
for (var j = 0; j < 2; j++) {
31+
document.write(arr[i][j] + " ");
32+
}
33+
document.write("<br>");
34+
}
35+
```
36+
37+
## How it works
38+
- Before you learn how to create 2D arrays in JavaScript, let’s first learn how to access elements in 2D arrays.
39+
40+
- You can access the elements of a two-dimensional array using two indices, one for the row and one for the column. Suppose you have the following two-dimensional array:
41+
```
42+
let MathScore = [
43+
['John Doe', 20, 60, 'A'],
44+
['Jane Doe', 10, 52, 'B'],
45+
['Petr Chess', 5, 24, 'F'],
46+
['Ling Jess', 28, 43, 'A'],
47+
['Ben Liard', 16, 51, 'B']
48+
];
49+
```
50+
- The above is a jagged array in which each element holds the student's name, test score, exam score, and grade. You can access specific elements using the row and column index as seen in the syntax below:
51+
52+
```
53+
arrayName[rowIndex][columnIndex]
54+
```
55+
- To better understand this, let’s convert the two-dimensional array above to a table using ```console.table(arrayName).```
56+
57+
- You will get an output like this, showing the row and column index. Remember that arrays are zero-indexed, meaning items are referenced from 0, not 1.
58+
59+
## References
60+
- [stackoverflow](https://stackoverflow.com/questions/966225/how-can-i-create-a-two-dimensional-array-in-javascript)
61+
- [GeeksforGeeks](https://www.geeksforgeeks.org/how-to-create-two-dimensional-array-in-javascript/)
62+
- [freecodecamp](https://www.freecodecamp.org/news/javascript-2d-arrays/#:~:text=let%20arr%20%3D%20%5B%5D%3B%20let,first%20loop%20through%20the%20rows.)
63+
64+
65+
## Problem Added By
66+
- [GitHub](https://www.github.com/rithik14)
67+
68+
69+
70+
## Contributing
71+
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
72+
73+
Please make sure to update tests as appropriate.

0 commit comments

Comments
 (0)