-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2dMatrix.js
77 lines (67 loc) · 1.62 KB
/
2dMatrix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//acces elements
let mathScore = [
["John Doe", 20, 60, "A"],
["Jane Doe", 10, 52, "B"],
["Petr Chess", 5, 24, "F"],
["Ling Jess", 28, 43, "A"],
["Ben Liard", 16, 51, "B"],
];
// accessing elements in 2D matrix
//arrayName[rowIndex][columnIndex]
// console.table(mathScore);
console.log(mathScore[2][2]);
console.log(mathScore[0][0]);
// adding elements in 2D matrix
let numberArr = [
[10, 20, 60],
[8, 10, 52],
[15, 5, 24],
[26, 28, 43],
[12, 16, 51],
];
let sum = 0;
numberArr.forEach((row) => {
row.forEach((element) => {
sum += element;
});
});
console.log(sum);
//adding elemnts into an 2D matrix
numberArr.push([5, 70, 45]);
numberArr.unshift([20, 13, 87]); //adds at the beginning
numberArr.splice(2, 0, [10, 10, 10]);
//remove elements at specified and add new one
numberArr.pop(); //remove last element
numberArr.shift(); //removes first element
console.table(numberArr);
// create 2D array
let arr = [];
let row = 4;
let columns = 3;
let value = 0;
for (let i = 0; i < row; i++) {
arr[i] = []; //means rows should match columns
for (let j = 0; j < columns; j++) {
arr[i][j] = value++;
}
}
// let array =[1,2,4]
// arr[1]=4
console.table(arr);
//search string in 2d matrix
//function to accept
// function TwoDmatrix(row,columns) {
// let arr = [];
// let value = 0;
// for (let i = 0; i < row; i++) {
// arr[i] = []; //means rows should match columns
// for (let j = 0; j < columns; j++) {
// arr[i][j] = value++;
// }
// console.log(arr)
// }
// }
// let row = 4;
// let columns = 3;
// const result =TwoDmatrix(row,columns)
// console.log(result)