Skip to content

Commit 1f4a7b8

Browse files
committed
More examples
1 parent 582270f commit 1f4a7b8

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

JavaScript/a-matrix.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict';
2+
3+
const matrix = [
4+
[7, 10, 1, 5, 2],
5+
[6, -1, 7, 2, 3],
6+
[1, 2, 4, -8, 2],
7+
[-6, 4, 8, 2, 0],
8+
];
9+
10+
const max = (a, b) => a > b ? a : b;
11+
12+
const res = matrix
13+
.map(row => row.reduce(max))
14+
.reduce((acc, rowMax) => acc + rowMax);
15+
16+
console.log(res);
17+
18+
let i, j, row, col;
19+
for (i in matrix) {
20+
row = matrix[i];
21+
for (j in row) {
22+
col = row[j];
23+
console.log(i, j, col);
24+
}
25+
}

JavaScript/b-matrix-each.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
const matrix = [
4+
[7, 10, 1, 5, 2],
5+
[6, -1, 7, 2, 3],
6+
[1, 2, 4, -8, 2],
7+
[-6, 4, 8, 2, 0],
8+
];
9+
10+
matrix.forEach((row, i) => {
11+
row.forEach((col, j) => {
12+
console.log(i, j, col);
13+
});
14+
});

JavaScript/c-matrix-for-in.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const matrix = [
4+
[7, 10, 1, 5, 2],
5+
[6, -1, 7, 2, 3],
6+
[1, 2, 4, -8, 2],
7+
[-6, 4, 8, 2, 0],
8+
];
9+
10+
let i, j, row, col;
11+
for (i in matrix) {
12+
row = matrix[i];
13+
for (j in row) {
14+
col = row[j];
15+
console.log(i, j, col);
16+
}
17+
}

JavaScript/d-reduce.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
let count = 0;
4+
const arr = [7, 10, 1, 5, 2];
5+
const sum = (acc, val) => (count++, acc + val);
6+
const res = arr.reduce(sum);
7+
console.log({ res, count });

0 commit comments

Comments
 (0)