-
Notifications
You must be signed in to change notification settings - Fork 509
/
Copy pathmatrix.js
43 lines (36 loc) · 889 Bytes
/
matrix.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
export class Matrix {
constructor (inMatrix) {
this.data = inMatrix
}
get (row, column) {
if (row >= this.data.length || column >= this.data[row].length) {
throw new RangeError('Out of bounds')
}
return this.data[row][column]
}
set (row, column, value) {
if (row >= this.data.length || column >= this.data[row].length) {
throw new RangeError('Out of bounds')
}
this.data[row][column] = value
}
[Symbol.iterator] () {
let nextRow = 0
let nextCol = 0
return {
next: () => {
if (nextRow === this.data.length) {
return { done: true }
}
const currVal = this.data[nextRow][nextCol]
if (nextCol === this.data[nextRow].length - 1) {
nextRow++
nextCol = 0
} else {
nextCol++
}
return { value: currVal }
}
}
}
}