-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.js
More file actions
67 lines (55 loc) · 1.26 KB
/
Copy pathArray.js
File metadata and controls
67 lines (55 loc) · 1.26 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
//create a class
class ImplementingArray{
constructor(){
this.array={};
this.length=0;
}
append(value){
this.array[this.length]=value;
this.length++;
//return this
}
search(index){
const value=this.array[index];
if (value){
return value;}
else{
//return "enter the correct index"
}
}
insert(index,value){
let current_value=this.array[index]
for (let i=index;i<this.length;i++){
let next_value=this.array[i+1]
this.array[i+1]=current_value
current_value=next_value
}
this.array[index]=value
this.length++;
return this;
}
pop(){
delete this.array[this.length-1];
this.length--;
return this
}
remove(index)
{
delete this.array[index];
for (let i=index;i<this.length;i++){
console.log(i)
let next_value=this.array[i+1]
this.array[i]=next_value;
}
delete this.array[this.length-1]
this.length--;
return this
}
}
const implement=new ImplementingArray()
//implement.append(0)
//implement.append(2)
//implement.append(3)
//implement.insert(1,1)
//implement.remove(2)
//console.log(implement.search(1))