-
Notifications
You must be signed in to change notification settings - Fork 6
/
deCodeString.js
68 lines (57 loc) · 1.46 KB
/
deCodeString.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
// Given a grid of characters output a decoded message.
// The message for the following would be IROCLED.
// (diagonally down right and diagonally up right if you can't go further
// .. you continue doing this)
// I B C A L K A
// D R F C A E A
// G H O E L A D
const board = [
["H", "B", "C", "A", "O", "A", "W", "X", "O", "L", "K"],
["D", "E", "F", "L", "A", " ", "A", "O", "T", "F", "Q"],
["G", "H", "L", "E", "O", "E", "O", "Z", "R", "M", "D"],
["G", "H", "L", "L", "X", "E", "O", "S", "F", "L", "V"]
];
function decode(row, col, matrix, direction, solution) {
solution = solution.concat(matrix[row][col]);
if (col === matrix[row].length - 1) {
return solution;
}
if (row + 1 === matrix.length) {
direction = false;
} else if (row + 1 === 1) {
direction = true;
}
if (direction) {
row++;
col++;
} else {
row--;
col++;
}
return decode(row, col, matrix, direction, solution);
}
function decodeNonRecursive(matrix, direction) {
let row = 0;
let col = 0;
let solution = "";
while (col < matrix[row].length) {
solution = solution.concat(matrix[row][col]);
if (row + 1 === matrix.length) {
direction = false;
} else if (row + 1 === 1) {
direction = true;
}
if (direction) {
row++;
col++;
} else {
row--;
col++;
}
}
return solution;
}
let row = 0;
let col = 0;
console.log(decodeNonRecursive(board, true));
console.log(decode(row, col, board, true, ""));