Skip to content

Commit 9c7edda

Browse files
committed
Add nested loops task
1 parent 9442132 commit 9c7edda

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

Exercises/6-matrix.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
const max = matrix => {
4+
// Use nested for loop to find max value in 2d matrix
5+
// For example max([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
6+
// should return 9
7+
};
8+
9+
module.exports = { max };

Exercises/6-matrix.test

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
({
2+
name: 'max',
3+
length: [220, 300],
4+
cases: [
5+
[[[10]], 10],
6+
[[[1, 2], [3, 4], [5, 6]], 6],
7+
[[[-1, 1], [2, -1], [-1, 0]], 2],
8+
],
9+
test: max => {
10+
const src = max.toString();
11+
if (!src.includes('for (')) throw new Error('Use for loop');
12+
}
13+
})

Solutions/6-matrix.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
const max = matrix => {
4+
let value = matrix[0][0];
5+
for (let i = 0; i < matrix.length; i++) {
6+
const row = matrix[i];
7+
for (let j = 0; j < row.length; j++) {
8+
const cell = row[j];
9+
if (value < cell) value = cell;
10+
}
11+
}
12+
return value;
13+
};
14+
15+
module.exports = { max };

0 commit comments

Comments
 (0)