-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge19.js
48 lines (43 loc) · 1.15 KB
/
challenge19.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
/*
Points: 40
OPS/S : 1204
Cognitive complexity: 11
*/
function revealSabotage(store) {
const rows = store.length;
const cols = store[0].length;
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
if (store[i][j] === "*") {
for (let x = -1; x <= 1; x++) {
for (let y = -1; y <= 1; y++) {
const tempX = i + x;
const tempY = j + y;
if (tempX >= 0 && tempY >= 0 &&
tempX < rows && tempY < cols &&
store[tempX][tempY] !== "*") {
++store[tempX][tempY];
store[tempX][tempY] = store[tempX][tempY].toString();
}
}
}
}
}
}
return store;
}
const store = [
['*', ' ', ' ', ' '],
[' ', ' ', '*', ' '],
[' ', ' ', ' ', ' '],
['*', ' ', ' ', ' ']
]
console.log(revealSabotage(store))
/* Should display:
[
['*', '2', '1', '1'],
['1', '2', '*', '1'],
['1', '2', '1', '1'],
['*', '1', ' ', ' ']
]
*/