|
18 | 18 | * |
19 | 19 | * @private |
20 | 20 | * @param {array} inputGraph The input matrix of the graph |
21 | | - * @param {number} destination The destination |
| 21 | + * @param {array} destination The destination |
22 | 22 | */ |
23 | 23 | function init(inputGraph, destination) { |
24 | 24 | graph = inputGraph; |
|
27 | 27 | visited = {}; |
28 | 28 | } |
29 | 29 |
|
| 30 | + /** |
| 31 | + * Adds a valid node to the queue |
| 32 | + * @param {array} node Node to be added to the queue |
| 33 | + */ |
30 | 34 | function addNode(node) { |
31 | 35 | if (visited[node] || |
32 | 36 | node[0] < 0 || node[1] < 0 || |
|
40 | 44 | /** |
41 | 45 | * Process given node |
42 | 46 | * |
43 | | - * @param {number} destination The destionation, which should be reached |
44 | | - * @param {number} current The current node |
45 | | - * @param {number} node Neighbour node |
| 47 | + * @param {array} destination The destionation, which should be reached |
| 48 | + * @param {array} current The current node |
46 | 49 | */ |
47 | 50 | function processNode(destination, current) { |
48 | 51 | if (destination.toString() === current.toString()) { |
|
57 | 60 | } |
58 | 61 |
|
59 | 62 | /** |
60 | | - * Validates the graph |
| 63 | + * Validates the params |
61 | 64 | * |
62 | 65 | * @param {array} graph A matrix representation of the graph |
63 | | - * @param {number} source The source node |
64 | | - * @param {number} destination The destination node |
65 | | - * @returns {boolean} true/false depending whether the params are valid |
| 66 | + * @param {array} source The source node |
| 67 | + * @param {array} destination The destination node |
66 | 68 | */ |
67 | 69 | function validateParams(graph, source, destination) { |
68 | 70 | if (!graph) { |
69 | | - throw 'The graph should be represented as a matrix'; |
| 71 | + throw new Error('The graph should be represented as a matrix'); |
70 | 72 | } |
71 | | - if (!graph[0]) { |
72 | | - throw 'The graph should be represented as ' + |
73 | | - 'a matrix, with size at least 1x1'; |
| 73 | + if (graph[0] === undefined) { |
| 74 | + throw new Error('The graph should be represented as ' + |
| 75 | + 'a matrix, with size at least 1x1'); |
74 | 76 | } |
75 | 77 | var width = graph[0].length; |
76 | | - for (var i = 0; i < graph.length; i += 1) { |
77 | | - if (graph[i] !== width) { |
78 | | - throw 'The graph should be represented as a matrix'; |
| 78 | + for (var i = 1; i < graph.length; i += 1) { |
| 79 | + if (graph[i].length !== width) { |
| 80 | + throw new Error('The graph should be represented as a matrix'); |
79 | 81 | } |
80 | 82 | } |
81 | 83 | source.concat(destination).filter(function (c, i) { |
82 | 84 | if (c < 0) { |
83 | | - throw 'The source and destination coordinates should be above zero'; |
| 85 | + throw new Error('The source and destination coordinates ' + |
| 86 | + 'should be above zero'); |
84 | 87 | } |
85 | 88 | if (i % 2 === 0) { |
86 | 89 | if (c >= graph.length) { |
87 | | - throw 'The source and destination coordinates ' + |
88 | | - 'should not be above graph\'s size'; |
| 90 | + throw new Error('The source and destination coordinates ' + |
| 91 | + 'should not be above graph\'s size'); |
89 | 92 | } |
90 | 93 | } else { |
91 | 94 | if (c >= graph[0].length) { |
92 | | - throw 'The source and destination coordinates ' + |
93 | | - 'should not be above graph\'s size'; |
| 95 | + throw new Error('The source and destination coordinates ' + |
| 96 | + 'should not be above graph\'s size'); |
94 | 97 | } |
95 | 98 | } |
96 | 99 | }); |
|
0 commit comments