forked from TamimEhsan/AlgorithmVisualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_helpers.js
More file actions
60 lines (55 loc) · 1.67 KB
/
Copy patharray_helpers.js
File metadata and controls
60 lines (55 loc) · 1.67 KB
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
/**
* A set of monkey-patched Array helpers.
* Probably a bad idea to use these in a real production environment.
* Monkey-patching is confusing to anyone seeing the project for the first
* time (or coming back to it after a few weeks).
* That said, this is a simple demo project, so what the hell, let's break
* some rules =D
*/
/**
* Array.prototype.swap
* Rearrange an array to swap the positions of two elements.
* @param {Number} a - the index of the first element to swap.
* @param {Number} b - the index of the second element to swap.
* @returns {Array}
* @example
* // returns [ 'a', 'c', 'b' ]
* [ 'a', 'b', 'c' ].swap(1, 2)
*/
Array.prototype.swap = function (a, b) {
if ( b >= this.length || b < 0 ) return this;
// Temporary variable to hold data while we juggle
let temp = this[a];
this[a] = this[b];
this[b] = temp;
return this;
};
/**
* Array.range
* Create a new array of length n, where the elements are numbers
* from 0 to n - 1.
* @param {Number} n - the desired length of the range.
* @returns {Array}
* @example
* // returns [ 0, 1, 2, 3 ]
* Array.range(4);
*/
Array.range = n => Array.from(new Array(n), (x,i) => i);
/**
* Array.matrix
* Create a new two-dimensional array, where each element is its own index.
* @param {Number} x - the desired number of columns (possible x values)
* @param {Number} y - the desired number of rows (possible y values)
* @returns {Array}
* @example
* // returns [
* // [ 0, 1, 2 ],
* // [ 0, 1, 2 ]
* // ]
* Array.matrix(3, 2);
*/
Array.matrix = (x, y) => {
const rows = Array.range(y);
const columns = Array.range(x);
return rows.map( (row, i) => columns.slice() );
}