-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.js
More file actions
84 lines (74 loc) · 1.94 KB
/
Copy pathMyArray.js
File metadata and controls
84 lines (74 loc) · 1.94 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
class MyArray{
#elements;
#length;
#size;
constructor(size){
this.elements = [];
this.length = size
this.size = 0;
}
isEmpty() {
return this.size == 0;
}
add(item) {
if (this.size < this.length) {
this.elements += item
this.size+=1;
}
return "index out of bound"
}
length() {
return this.length;
}
size() {
return this.size;
}
getIndex(index) {
for(let index = 0; index < size; index++) {
if(this.elements[index].equals(element)) return index;
}
return "index out of bound";
}
getIndexOf(element) {
for(let index = 0; index < size; index++) {
if(this.elements[index].equals(element)) return index;
}
return "index out of bound";
}
contains(element) {
for(let count = 0; count < size; count++) {
if(this.elements[count].equals(element)) {
return true;
}
}
return false;
}
remove(element) {
if(this.contains(element)) {
for(let count = 0; count < size-1; count++) {
if(element.equals(this.elements[count]) && count != size-1 ) {
for(let index = count; index < size-1; index++) {
this.elements[index] = this.elements[index+1];
}
this.elements[size-1] = null;
this.size--;
return;
}
else if(count == size-1) {
this.elements[count] = null;
}
}
this.size--;
}
}
clear() {
for(let count = 0; count < size; count++) {
elements[count] = null;
this.size = 0;
}
}
setElement(index, element) {
this.elements[index] = element;
}
}
module.exports = (MyArray)