-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patharray.js
More file actions
46 lines (43 loc) · 1.05 KB
/
Copy patharray.js
File metadata and controls
46 lines (43 loc) · 1.05 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
function MyArray() {
this.array = [];
}
MyArray.prototype.add = function(data) {
this.array.push(data);
};
MyArray.prototype.remove = function(data) {
this.array = this.array.filter(function(current) {
return current !== data;
});
};
MyArray.prototype.search = function(data) {
var foundIndex = this.array.indexOf(data);
if(~foundIndex) {
return foundIndex;
}
return null;
};
MyArray.prototype.getAtIndex = function(index) {
return this.array[index];
};
MyArray.prototype.length = function() {
return this.array.length;
};
MyArray.prototype.print = function() {
console.log(this.array.join(' '));
};
var array = new MyArray();
array.add(1);
array.add(2);
array.add(3);
array.add(4);
array.print(); // => 1 2 3 4
console.log('search 3 gives index 2:', array.search(3)); // => 2
console.log('getAtIndex 2 gives 3:', array.getAtIndex(2)); // => 3
console.log('length is 4:', array.length()); // => 4
array.remove(3);
array.print(); // => 1 2 4
array.add(5);
array.add(5);
array.print(); // => 1 2 4 5 5
array.remove(5);
array.print(); // => 1 2 4